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

AssignmentFinal #36

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

rahulit
Copy link

@rahulit rahulit commented Oct 13, 2016

46884868
Rahul Khandalkar

send from, { :next_is, value}
counter(value+1)
end
end
Copy link
Contributor

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
Copy link
Contributor

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)
Copy link
Contributor

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
Copy link
Contributor

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
Copy link
Contributor

Choose a reason for hiding this comment

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

what a fine source :)

30

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants