Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 36 additions & 26 deletions ex01.exs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,24 @@ defmodule Ex01 do
2 is the program well laid out, appropriately using indentation,
blank lines, vertical alignment
"""

def counter(value \\ 0) do
send(self(), {:next, from})
receive do
{:next, value} IO.puts(value)
end
end
def new_counter(value \\ 0) do
send(self(), {:next, from})
receive do
{:next, value} IO.puts(value)
end
end
def next_value(value) do
send(self(), {:next, from})
receive do
{:next, value} IO.puts(value + 1)
end
end

end
Expand All @@ -41,35 +57,29 @@ defmodule Test do
# 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






115 changes: 88 additions & 27 deletions ex02.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,73 @@

defmodule Ex02 do

use Agent

@@doc """

start a counter with an initial value of 0

"""
def new_counter(value \\ 0) do

Agent.start_link(fn -> value end, name: counter)

end

@@doc """

get a conter value from the counter

"""
def get(counter) do

Agent.get(counter, fn count -> 0 end)

end

@@doc """

update the conter with an incremenent

"""
def new_counter(value \\ 0) do

Agent.start_link(fn -> value end, name: counter)

end

count = Ex02.new_counter()

def next_value(count) do

receive do

count -> IO.puts count

new_value(count + 1)

end

end

def new_global_counter(value \\ 0) do

Agent.start_link(fn -> value end, name: counter)

end

def global_next_value(count \\ 0) do

new_count = receive do

count -> IO.puts count

new_counter(count + 1)

end

global_next_value(new_count)

end

ExUnit.start()
Expand All @@ -23,7 +90,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
Expand All @@ -32,26 +99,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(counter, fn -> counter end)
assert value == 0
Agent.update(counter, fn -> 0 + 1 end)
value = Agent.get(counter, fn -> counter 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
Expand All @@ -60,16 +127,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






21 changes: 14 additions & 7 deletions ex03.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -58,29 +58,36 @@ defmodule Ex03 do
5 elegant use of language features or libraries

"""
get_request = fn(id) ->
"{process_id:{id: #{id}}}"
end
async_get_request = fn(id) ->
spawn(fn -> IO.puts(get_request.(id)) end)
end

def pmap(collection, process_count, function) do
« your code here »
Enum.map(process_count, async_get_request)
Enum.chunk(collection, 2)
|> Enum.count
end

end


ExUnit.start
defmodule TestEx03 do
use ExUnit.Case
import Ex03

test "pmap with 1 process" do
assert pmap(1..10, 1, &(&1+1)) == 2..11 |> Enum.into([])
assert pmap([1, 2, 3, 4, 5, 6],1..1, async_get_request) == 1
end

test "pmap with 2 processes" do
assert pmap(1..10, 2, &(&1+1)) == 2..11 |> Enum.into([])
assert pmap([1, 2, 3, 4, 5, 6],1..2, async_get_request) == 1
end

test "pmap with 3 processes (doesn't evenly divide data)" do
assert pmap(1..10, 3, &(&1+1)) == 2..11 |> Enum.into([])
assert pmap([1, 2, 3, 4, 5, 6],1..3, async_get_request) == 1
end

# The following test will only pass if your computer has
Expand All @@ -96,5 +103,5 @@ defmodule TestEx03 do
assert result2 == result1
assert time2 < time1 * 0.8
end

end