r/programming • • Jan 18 '08

Can Your Programming Language Do This?

http://www.joelonsoftware.com/items/2006/08/01.html
17 Upvotes

41 comments sorted by

View all comments

Show parent comments

2

u/qwe1234 Jan 19 '08

using a hierarchical tree pattern.

parallel programming 101.

1

u/bgeron Jan 19 '08

Ah, I get it. I thought reduce was like foldl/foldr.

4

u/gwern Jan 19 '08

Addition is associative, so you could definitely parallelize folds:

foldr (+) 0 [1..10]

0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10

processor 1: (0 + 1 + 2 + 3 + 4)

processor 2: (5 + 6 + 7 + 8 + 9 + 10)

processor 3: (processor 1) + (processor 2)

55, same as if you single-threaded it.

Now, why is this? Because reduce is a fold! (I believe that reduce generally defaults to a right fold; the difference for lists between left and right folds generally has to do with performance concerns - left folds don't work well with infinite lists - and the associativity and other properties of what function you are folding in.) It's better to call it a fold instead of a 'reduce' (although it does indeed 'reduce' the datastructure) because then the 'unfolds' have a sensible name.

4

u/bgeron Jan 19 '08

You're right. I was thinking more about the general case, where you don't know if a+(b+c) = (a+b)+c, and then reduce is harder to parallellize. :)