dwyl / learn-elixir

:droplet: Learn the Elixir programming language to build functional, fast, scalable and maintainable web applications!
1.6k stars 107 forks source link

How does calc_product/0 work in the spawn function? #180

Closed LuchoTurtle closed 1 year ago

LuchoTurtle commented 1 year ago

🚨 Dumb question alert 🚨

I'm a complete Elixir newbie and I'm following through the tutorial and it's really well-written, huge thank you 🎉 . I have a question regarding the Concurrency chapter, though.

My problem is in the commented line of code:

def spawn(n) do
    1..n
    |> Enum.chunk_every(4)
    |> Enum.map(fn(list) ->
      spawn(Factorial, :_spawn_function, [list])
      |> send(self())

      receive do
        n -> n
      end
    end)
    |> calc_product().     # ---> this is not defined, how does it work?
  end

The tutorial says that Once each process in the map function has received a result, we then call the calc_product once more to turn the list of results from map into a single integer, the factorial.. But how/where is this behavior defined?

LuchoTurtle commented 1 year ago

Nevermind, forgot the operation was piped, hence why there's no need to pass the argument, as the value returned from the previous expression is passed down as the first argument in the following one.

Cheers!

nelsonic commented 1 year ago

Good question. Next time don't be concerned about being "dumb". 👍 I ask n00b questions all-the-time and I'm doing OK. 😜