Reduce and Anonymous Functions

Anonymous functions, also called higher order functions, allow developers to treat functions like data. You can pass anonymous functions as arguments, or hold them in variables.

You can then use anonymous functions to automate the code we’ve written so far. Sometimes, you might want to build your own function to combine elements in a list. Take another look at the critical line of code that uses an accumulator:

 total_with_accumulator(rest, partial_total + first)

Now that you know how that works, let’s organize the code with a slight difference:

 process_list(rest, arbitrary_function)

This pattern is common enough that there’s a function to do this work called reduce. To use it, we need to be able to specify our ...

Get Programmer Passport: Elixir now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.