Skip to content
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

Oglesbee A3 #30

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
54 changes: 35 additions & 19 deletions ex01.exs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,22 @@ defmodule Ex01 do
"""

def counter(value \\ 0) do
receive do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

10, 10

{:next, from} ->
send from, {:next_is, value}
counter value+1
end
end

def new_counter(value) do
spawn Ex01, :counter, [value]
end

def next_value(counter) do
send counter, { :next, self}
receive do
{:next_is, value} -> value
end
end

end
Expand All @@ -41,30 +57,30 @@ 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

Expand Down
61 changes: 40 additions & 21 deletions ex02.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@

defmodule Ex02 do

@global_counter Ex02
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

10*3


def new_global_counter(value \\ 0) do
{:ok, counter} = Agent.start_link(fn -> value end, name: @global_counter)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically, you don't have to return the counter pid here, but it doesn't hurt.

counter
end

def global_next_value do
Agent.get_and_update(@global_counter, fn state -> {state, state+1} end)
end

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 state -> {state, state + 1} end)
end

end

ExUnit.start()
Expand All @@ -23,7 +43,6 @@ 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 +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 state -> {state, state+1} end)
assert value == 0

value = Agent.get_and_update(counter, fn state -> {state, state+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
Expand All @@ -60,12 +79,12 @@ 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


Expand Down
13 changes: 12 additions & 1 deletion ex03.exs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,18 @@ defmodule Ex03 do
"""

def pmap(collection, process_count, function) do
« your code here »
# The max process count is one process per item in the collection
# (Not very efficient, but it will work)
safe_process_count = min(Enum.count(collection), process_count)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see what you're doing, but the spec explicitly says to divide the collection into n chunks, where n is the number of processors.

-5


collection
# Split into even chunks, being sure to handle leftovers
|> Enum.chunk(safe_process_count, safe_process_count, [])
# Create a seperate map task for each chunk
|> Enum.map(&Task.async(fn -> Enum.map(&1, function) end))
# Await the results...
|> Enum.map(&Task.await(&1))
|> Enum.concat
end

end
Expand Down