DEV Community

Discussion on: Example: Imperative vs. Functional

Collapse
 
aminmansuri profile image
hidden_dude • Edited

In a practical sense, Functional programming "names your loops".. For example:


new_list = []
for elem in old_list :
.....new_list.append(xfer(elem))

In FP we call this MAPCAR, MAP or COLLECT.

REDUCE is a generalization of sums. REMOVE-IF-NOT, FILTER or SELECT is a generalization of filtering loops.

By thinking at a higher level of operations code is supposed to look more self-explanatory, and programmers are encouraged to think more globally. This is particularly useful for solving complex graph problems basing yourself on DFS and other algorithms, or for parallel programming as in OpenMPI's SCATTER, GATHER and COLLECT. And set theory gives us things such as UNION, INTERSECTION and DIFFERENCE.

Instead of focusing on the minor mechanics of a transformational operation, we can compose higher level transformations and express them more succinctly.