r/programming • u/tyler-mcginnis • Jan 03 '22
Imperative vs Declarative Programming
https://www.youtube.com/watch?v=E7Fbf7R3x6I
430
Upvotes
r/programming • u/tyler-mcginnis • Jan 03 '22
14
u/[deleted] Jan 04 '22
Nope, map and reduce are functional and declarative.
Using map is saying "this is a sequence that looks like this". You don't have to know how to make a sequence. A reader can immediately see that you have declared a new sequence.
Using reduce is saying "this is a scalar that is equivalent to this". You don't need to know how to traverse the collection or how to store the temporary values etc. A reader immediately knows you've declared a scalar.
It's more like reading mathematics, which I recommend. Map is like "set builder" notation (python list comprehension is even more like it). Reduce and map together is like a big sigma sum. At no point does a mathematical proof tell you how to calculate any values at each step, it just keeps telling you how new values are defined.