-
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
AssignmentFinal #36
base: master
Are you sure you want to change the base?
AssignmentFinal #36
Conversation
send from, { :next_is, value} | ||
counter(value+1) | ||
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.
-2 layout
It really is important. I know it sounds trivial, but nice looking code is well maintained code.
@@ -1,6 +1,28 @@ | |||
|
|||
defmodule Ex02 do | |||
def new_counter(value) do |
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.
0
this really doesn't work. You're supposed to be storing the value in a single agent, and then updating that value on each call to next_value
. Instead, you're keeping the count in the client code, and this code is just a complicated way of adding 1.
Here's what I'd like to see:
def new_counter(initial_value \\ 0) do
{ :ok, agent } = Agent.start_link(fn -> initial_value end)
agent
end
def next_value(agent) do
Agent.get_and_update(agent, &{ &1, &1 + 1})
end
# assert value == 1 | ||
# end | ||
test "counter using an agent" do | ||
{ :ok, counter } = Agent.start(fn -> 0 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.
-5
This is just setting the value of zero into an agent.
num | ||
end | ||
|
||
def new_global_counter do |
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.
-5
This code contains a race condition. Because you do the get
and update
as two separate calls, it is possible for two processes accessing this agent to both get the same value.
@@ -59,11 +59,21 @@ defmodule Ex03 do | |||
|
|||
""" | |||
|
|||
#Ref - Programming Elixir Book |
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.
what a fine source :)
46884868
Rahul Khandalkar