dwyl / learn-elixir

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

Mistake explanation of `chunk_every`? #158

Closed kai-tub closed 3 years ago

kai-tub commented 3 years ago

The code uses chunk_every which returns a list of lists with n elements per list. (The last chunk may contain fewer elements).

It starts by converting and integer into a range which it then 'chunks' into a list of 4 lists (unless n < 4, in which case it just runs on a single process). The number 4 itself is not important, it could have been 5, 10 or 1000. What is important about it, is that it is the number of Processes we will be spawning. (Yes we could have spawned 1000 processes if we wanted to) [...]

So AFAIK, this block should be changed. The number of lists/resulting processes will be n % 4 if n % 4 == 0 else n % 4 + 1.

Otherwise, thanks for the great introduction! :)

PS: I am happy to rewrite it and make a PR if you like.

iteles commented 3 years ago

Good catch. That would be grand @kai-tub, thanks!

kai-tub commented 3 years ago

One question, should we remove the code for fewer than 4 elements?

  def spawn(n) when n < 4 do
    calc_product(n)
  end

I don't think there is any reason to keep the function, as chunk_every will just return a list containing a single list with fewer than 4 elements. Or do we want to change the number to something like 10 to show of the when keyword and argue that splitting creates more overhead?

nelsonic commented 3 years ago

Thanks again @kai-tub 🥇