6
Refactoring with Loops and Collection Pipelines (www.martinfowler.com)
popcorny 積分 3
我一直覺得Collection Pipeline的Map跟MapReduce的Map很容易讓大家搞混,很多人常常認為同一件事情。例如文章中。
With these two operations on the menu, calculating the total word count is a two-step pipeline.

some_articles
  .map{|a| a.words}
  .reduce {|acc, w| acc + w}

如果你以為這樣寫就等同於MapReduce那就錯了。我們講的MapReduce的Map比較像是sql的group by, reduce是aggregation function。reduce就跟collection pipeline的一樣,MapReduce的map在java stream裡面是用Collectors.groupingBy,並且搭配collect去使用。

所以不要再說我用Java8做MapReduce,然後只用map()跟reduce()了。先試著寫一個WordCount才比較清楚什麼叫做MapReduce。

IngramChen 積分 1 編輯於

It's a matter of taste whether you prefer the list comprehension form, or the pipeline form (I prefer pipelines). In general pipelines are more powerful, in that you can't refactor all pipelines into comprehensions.

同感

Despite this issue, I still find the pipeline version easier to comprehend. I could combine the filters into a single conjunction, but I usually find it easier to understand each filter as a single element.

要不要將 filter 合併,這個我也沒什麼準則。如果效能不是問題的話,我也會傾向一個個分開寫,比較清楚