-
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
Omar dev Assignment3 #33
base: master
Are you sure you want to change the base?
Changes from all commits
c407b03
7c84c09
622e848
7259816
60d5b83
c761723
6333305
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,24 @@ | ||
|
||
defmodule Ex02 do | ||
|
||
@name __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. -4You have a couple of "unused variable" warnings here. You need to clear these up before submitting. |
||
|
||
def new_counter(start_value \\ 0) do | ||
{:ok, counter} = Agent.start_link(fn-> start_value end) | ||
end | ||
|
||
def next_value({ :ok, counter }) do | ||
Agent.get_and_update(counter, &{&1, (&1+1) } ) | ||
end | ||
|
||
def new_global_counter(start_value \\ 0) do | ||
{:ok, counter} = Agent.start_link(fn -> start_value end, name: @name) | ||
end | ||
|
||
def global_next_value do | ||
Agent.get_and_update(@name, &{&1, (&1+1) } ) | ||
end | ||
|
||
end | ||
|
||
ExUnit.start() | ||
|
@@ -23,7 +41,7 @@ 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 +50,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 +78,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 | ||
|
||
|
||
|
||
|
||
|
||
|
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,7 +60,22 @@ 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. 30(although I'd rather not have the pipeline with comments and blank lines in it—for some reason the convention is that they should all be adjacent. |
||
# calculate number of chunks | ||
num_chunks = collection |> Enum.count |> div(process_count) | ||
|
||
# split collection into n chunks | ||
collection |> Enum.chunk(num_chunks, num_chunks, []) | ||
|
||
# process chunks | ||
|> Enum.map(&Task.async(fn -> Enum.map(&1, function) end)) | ||
|
||
# wait for all the chunks to get processed | ||
|> Enum.map(&Task.await/1) | ||
|
||
# put chunks back together | ||
|> Enum.concat | ||
|
||
end | ||
|
||
end | ||
|
@@ -96,5 +111,5 @@ defmodule TestEx03 do | |
assert result2 == result1 | ||
assert time2 < time1 * 0.8 | ||
end | ||
|
||
end |
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