diff --git a/ex01.exs b/ex01.exs index 11cbdac..04e4c5c 100644 --- a/ex01.exs +++ b/ex01.exs @@ -26,50 +26,60 @@ defmodule Ex01 do 2 is the program well laid out, appropriately using indentation, blank lines, vertical alignment """ - + def counter(value \\ 0) do + receive do + {:next, from}-> + send from, {:next_is, value} + counter(value+1) + end + end + def new_counter(value \\ 0) do + spawn Ex01, :counter, [value] end + def next_value(pid) do + send pid, {:next, self} + receive do + {:next_is, value} -> value + end + end end + ExUnit.start() defmodule Test do use ExUnit.Case + # Start by uncommenting this test and getting it to pass # This test assumes you have a function `counter` that can be spawned # and which handles the `{:next, from}` message - # test "basic message interface" do - # count = spawn Ex01, :counter, [] - # send count, { :next, self } - # receive do - # { :next_is, value } -> - # assert value == 0 - # end - # - # send count, { :next, self } - # receive do - # { :next_is, value } -> - # assert value == 1 - # end - # end + test "basic message interface" do + count = spawn Ex01, :counter, [] + send count, { :next, self } + receive do + { :next_is, value } -> + assert value == 0 + end + + send count, { :next, self } + receive do + { :next_is, value } -> + assert value == 1 + end + end # then uncomment this one # Now we add two new functions to Ex01 that wrap the use of # that counter function, making the overall API cleaner - # test "higher level API interface" do - # count = Ex01.new_counter(5) - # assert Ex01.next_value(count) == 5 - # assert Ex01.next_value(count) == 6 - # end + test "higher level API interface" do + count = Ex01.new_counter(5) + assert Ex01.next_value(count) == 5 + assert Ex01.next_value(count) == 6 + end end - - - - - - diff --git a/ex02.exs b/ex02.exs index 9e3deb9..d5dd15e 100644 --- a/ex02.exs +++ b/ex02.exs @@ -1,6 +1,25 @@ defmodule Ex02 do + @name __MODULE__ + + def new_counter(value \\ 0) do + {:ok, counter} = Agent.start_link fn -> value end + counter + end + + def next_value(counter) do + Agent.get_and_update(counter, fn val -> {val, val+1} end) + end + + + def new_global_counter(value \\ 0) do + Agent.start_link(fn -> value end, name: @name) + end + + def global_next_value do + Agent.get_and_update(@name, fn val -> {val, val+1} end) + end end ExUnit.start() @@ -23,7 +42,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 +51,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_link fn -> 0 end + + value = Agent.get_and_update(counter, fn val -> {val, val+1} end) + assert value == 0 + + value = Agent.get_and_update(counter, fn val -> {val, val+1} end) + 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 +79,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 - - - - - - diff --git a/ex03.exs b/ex03.exs index 5d451a0..506ad64 100644 --- a/ex03.exs +++ b/ex03.exs @@ -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,12 @@ defmodule Ex03 do """ def pmap(collection, process_count, function) do - « your code here » + chunk_size = div(Enum.count(collection) , process_count) + Enum.chunk(collection, chunk_size, chunk_size, []) + |> Enum.map(&(Task.async(fn -> Enum.map(&1, fn val -> val+1 end) end))) + |> Enum.map(&(Task.await(&1))) + |> Enum.concat + end end @@ -96,5 +101,5 @@ defmodule TestEx03 do assert result2 == result1 assert time2 < time1 * 0.8 end - + end