logoalt Hacker News

snackbrokenlast Tuesday at 5:42 AM4 repliesview on HN

Map and Filter are nice because they let you reason locally about a single element in isolation. Reduce(Fold) forces you to reason globally about intermediate results. Reduce also forces you to conjure up a "zero" value of the relevant type, which isn't usually difficult but it does constitute some extra mental overhead.


Replies

sigbottleyesterday at 7:08 PM

Isn't reduce usually used for monoidal operations? Or do people implicitly absue ordering?

If the algortihm doesn't work the same forward, backwards, and with a tree scan, it ain't reduce (as a first approximation not IFF)

show 1 reply
mrkeenyesterday at 6:26 PM

It's pairwise, not global reasoning.

show 1 reply
globular-toasttoday at 7:04 AM

> Reduce also forces you to conjure up a "zero" value of the relevant type

It's more accurately an identity. If you are multiplying the identity is 1. While I think most people are comfortable saying the sum of no elements is 0 it's perhaps less intuitive that the product of no elements is 1. This makes me think reduce might be preferred by those with a mathematical background.

mcphageyesterday at 7:42 PM

> Reduce also forces you to conjure up a "zero" value of the relevant type, which isn't usually difficult but it does constitute some extra mental overhead.

It's always worthwhile to consider what the result will be when you pass in an empty list.

show 1 reply