DEV Community

Discussion on: ELI5 - "Map-filter-reduce"

Collapse
 
dimven profile image
Dimitar Venkov • Edited

for the sake of completeness, if we were interested in the total sum and are not concerned about making our code follow functional programming methods, we could rewrite the python one liner as:

sum(i*i for i in range(10) if not i % 2)

:)

Collapse
 
idanarye profile image
Idan Arye

You don't have to ignore language constructs to "follow functional programming methods". In Haskell, for example, you can use the combinators:

Prelude> sum (map (\x -> x * x) (filter (\x -> x `mod` 2 == 0) [0..9]))
120

Or you can use list comprehension(like in Python):

Prelude> sum [x * x | x <- [0..9], x `mod` 2 == 0]
120

The second approach is also functional programming.

Collapse
 
alephnaught2tog profile image
Max Cerrina

That single line explained more of map-filter-reduce than the articles I've been reading has! Not directed at the author, to be clear, it was a genuinely great article--I just have been puzzling over it because something has been tripping around in my brain and BAM, this comment did it. My bachelor's is in math and so the map-filter-reduce descriptions sounded so close and yet so far to what I kept almost thinking and that particular one-line tripped the right memory circuits!

Collapse
 
__namc profile image
Namc

Is there something I missed that I could have done better?

I tried to make it as simple as possible, and I'm sorry if it did not simplify things for you.

Thread Thread
 
alephnaught2tog profile image
Max Cerrina

No, not at all! I tried to make that clear, and failed. Your article was wonderfully written--it's just a topic that I've been chewing on for awhile and has eluded me, and that random one-liner reminded me of past math classes and was the final link, as it were. I really loved your piece!