Skip to content

Conversation

@euphoricpoptarts
Copy link

@euphoricpoptarts euphoricpoptarts commented Oct 16, 2017

Michael Gilbert 44823653

test "counter using an agent" do
{ :ok, counter } = Agent.start(fn -> 0 end)

value = Agent.get_and_update(counter,fn state -> {state, state + 1} end)
Copy link
Contributor

Choose a reason for hiding this comment

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

No deduction, but watch out for layout—I'd like to see a space after commas.


defmodule Ex02 do

@server CounterGlobal
Copy link
Contributor

Choose a reason for hiding this comment

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

-6

I'm being a little tough here, but the instructions asked you to use an agent for this, rather than writing your own GenServer.

+2

Then I'm being a softie because you wrote your own GenServer

chunk_size = ( Enum.count( collection) / process_count ) |> round
chunks = Enum.chunk_every( collection, chunk_size)
fmap = &( Task.async( fn -> Enum.map( &1, function) end ) )
tasks = Enum.map( chunks, fmap )
Copy link
Contributor

Choose a reason for hiding this comment

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

-5 for style/complexity

I'm uncomfortable with the number of variables, and the general linear nature of this code.

I'd rather have seen something like:

  defp into_chunks(collection, count) do
    chunk_size = div(Enum.count( collection), count )
    Enum.chunk_every( collection, chunk_size)
  end

  def pmap(collection, process_count, function) do
    chunk_mapper = &( Task.async( fn -> Enum.map( &1, function) end ) )

    collection
    |> into_chunks(process_count)
    |> Enum.map( chunk_mapper )
    |> Enum.map( &( Task.await( &1 ) ) )
    |> Enum.concat()
  end

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