DEV Community

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

Collapse
 
kspeakman profile image
Kasey Speakman • Edited

Many devs are probably already familiar with map/filter/reduce, but they know it in a different form: SQL.

  • SELECT is map
  SELECT sales_volume > 10000 AS is_preferred_member ...
  • WHERE is filter
  SELECT ...
   WHERE order_status = 'open'
  • GROUP BY or aggregate functions like SUM are forms of reduce
  SELECT SUM(order_total) ...

Once I realized that, it seemed only natural that the same capabilities be available in my programming language for in-memory collections too. You could already do them in for loops of course, but adding these explicit operations to the language saves the code and mental weight from the overhead of the loop. And more importantly, it makes the commonly-performed operation obvious, whereas a for loop has to be read line-by-line to make sure of what it is doing.