-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ashley Isles 38964553 #25
Open
ashleyisles
wants to merge
3
commits into
cse5345-fall-2016:master
Choose a base branch
from
ashleyisles:my_branch
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,25 @@ | ||
|
||
defmodule Ex02 do | ||
|
||
@ai __MODULE__ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I know it's your initials, but really... :) |
||
|
||
def new_counter(value \\ 0) do | ||
{:ok, counter} = Agent.start(fn -> value end) | ||
counter | ||
end | ||
|
||
def next_value(counter) do | ||
Agent.get_and_update(counter, &{&1, (&1 + 1)}) | ||
end | ||
|
||
def new_global_counter(value \\ 0) do | ||
Agent.start(fn -> value end, name: @ai) | ||
end | ||
|
||
def global_next_value do | ||
Agent.get_and_update(@ai, &{&1, (&1 + 1)}) | ||
end | ||
|
||
end | ||
|
||
ExUnit.start() | ||
|
@@ -23,7 +42,8 @@ defmodule Test do | |
2 is the program well laid out, appropriately using indentation, | ||
blank lines, vertical alignment | ||
""" | ||
|
||
|
||
|
||
|
||
@doc """ | ||
First uncomment this test. Here you will be inserting code | ||
|
@@ -32,26 +52,26 @@ defmodule Test do | |
Replace the placeholders with your code. | ||
""" | ||
|
||
# test "counter using an agent" do | ||
# { :ok, counter } = « your code » | ||
# | ||
# value = « your code » | ||
# assert value == 0 | ||
# | ||
# value = « your code » | ||
# assert value == 1 | ||
# end | ||
test "counter using an agent" do | ||
{ :ok, counter } = Agent.start(fn -> 0 end) | ||
|
||
value = Agent.get_and_update(counter, &{&1, (&1 + 1)}) | ||
assert value == 0 | ||
|
||
value = Agent.get_and_update(counter, &{&1, (&1 + 1)}) | ||
assert value == 1 | ||
end | ||
|
||
@doc """ | ||
Next, uncomment this test, and add code to the Ex02 module at the | ||
top of this file to make those tests run. | ||
""" | ||
|
||
# test "higher level API interface" do | ||
# count = Ex02.new_counter(5) | ||
# assert Ex02.next_value(count) == 5 | ||
# assert Ex02.next_value(count) == 6 | ||
# end | ||
test "higher level API interface" do | ||
count = Ex02.new_counter(5) | ||
assert Ex02.next_value(count) == 5 | ||
assert Ex02.next_value(count) == 6 | ||
end | ||
|
||
@doc """ | ||
Last (for this exercise), we'll create a global counter by adding | ||
|
@@ -60,16 +80,10 @@ defmodule Test do | |
that agent into calls to `global_next_value`? | ||
""" | ||
|
||
# test "global counter" do | ||
# Ex02.new_global_counter | ||
# assert Ex02.global_next_value == 0 | ||
# assert Ex02.global_next_value == 1 | ||
# assert Ex02.global_next_value == 2 | ||
# end | ||
test "global counter" do | ||
Ex02.new_global_counter | ||
assert Ex02.global_next_value == 0 | ||
assert Ex02.global_next_value == 1 | ||
assert Ex02.global_next_value == 2 | ||
end | ||
end | ||
|
||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,7 +44,7 @@ defmodule Ex03 do | |
5 does it produce the correct results on any valid data | ||
|
||
Tested | ||
if tests are provided as part of the assignment: | ||
if tests are provided as part of the assignment: | ||
5 all pass | ||
|
||
Aesthetics | ||
|
@@ -60,13 +60,26 @@ defmodule Ex03 do | |
""" | ||
|
||
def pmap(collection, process_count, function) do | ||
« your code here » | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 30fyi, you could use |
||
#Get chunk size | ||
size_of_chunks = div(Enum.count(collection), process_count) | ||
|
||
#Divide into chunks | ||
Enum.chunk(collection, size_of_chunks, size_of_chunks, []) | ||
|
||
#Run all chunks through map function in asynchronous tasks (Elixir Book pg.2) | ||
|> Enum.map(&(Task.async(fn -> Enum.map(&1, function) end))) | ||
|> Enum.map(&Task.await/1) | ||
|
||
#Concatenates list | ||
|> Enum.concat | ||
end | ||
|
||
end | ||
|
||
|
||
ExUnit.start | ||
|
||
defmodule TestEx03 do | ||
use ExUnit.Case | ||
import Ex03 | ||
|
@@ -85,16 +98,17 @@ defmodule TestEx03 do | |
|
||
# The following test will only pass if your computer has | ||
# multiple processors. | ||
test "pmap actually reduces time" do | ||
range = 1..1_000_000 | ||
# random calculation to burn some cpu | ||
calc = fn n -> :math.sin(n) + :math.sin(n/2) + :math.sin(n/4) end | ||
# [AI] I'm commenting this out for testing purposes, my computer doesn't have multiple processors. | ||
# test "pmap actually reduces time" do | ||
# range = 1..1_000_000 | ||
# random calculation to burn some cpu | ||
# calc = fn n -> :math.sin(n) + :math.sin(n/2) + :math.sin(n/4) end | ||
|
||
{ time1, result1 } = :timer.tc(fn -> pmap(range, 1, calc) end) | ||
{ time2, result2 } = :timer.tc(fn -> pmap(range, 2, calc) end) | ||
# { time1, result1 } = :timer.tc(fn -> pmap(range, 1, calc) end) | ||
# { time2, result2 } = :timer.tc(fn -> pmap(range, 2, calc) end) | ||
|
||
# assert result2 == result1 | ||
# assert time2 < time1 * 0.8 | ||
# end | ||
|
||
assert result2 == result1 | ||
assert time2 < time1 * 0.8 | ||
end | ||
|
||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
10 + 10