Lazy evaluation, Higher-order functions, and Currying

Flare
3 min readMay 19, 2022

A. lazy evaluation

Lazy evaluation can be defined as an approach that delays the value of an expression until it’s needed (tutorialspoint, n.d) the primary purpose of using lazy evaluation is to gain more performance, taking Python programming language for instance; lazy evaluation was one of the many things that were improved in Python3, for example, a simple code like following:

in Python2 the output would be: <type ‘list’>

in Python3 the output is: <class ‘range’>

So the type of the variable test shows that in Python3 it holds a range instead of a list, and that’s because lazy evaluation was applied to some built-in functions in Python like range, an alternative in Python2 is xrange which also supports lazy evaluation.

B. Higher-order functions and Currying

Higher-order functions are functions that take another function as an argument or create another function, while currying is when a function takes the first argument and then returns another function until all arguments are fulfilled (geeksforgeeks, 2018)

There are many higher-order built-in functions that you may know in different programming languages, like map() or filter() in Python, the following example demonstrates how can you return a function as a result of a high-order function:

return a function as a result of a high-order function

The output of print(type(num)) is <class ‘function’> so you can notice that the num variable is holding a function which allowed us to use it as a higher-order function in order to get the product of two numbers. There are also other ways to use higher-order functions like built-in functions as previously mentioned or decorators.

On the other hand, currying is when you have n arguments passed to a function that calls a curried function which takes the first argument in order to get to n-1 with given arguments of the first function; In other words, currying is the process of turning a function that takes multiple arguments into a chain of functions where each one takes one argument. However, the concept might seem a little bit odd by just reading about it; Therefore, the example below demonstrates how currying works:

currying

The advantage of using higher-order functions and currying is gaining more code readability, especially in a programming language like Haskell, especially that the essential power of currying is to avoid repetition by passing the same argument over and over. Also, increase functions reusability so you can use the same function for multiple purposes, last but not least the most crucial part about higher-order functions and currying: is that refactoring becomes an easy task which is one of the top things that all companies will need for their software at some point.

References:

- Tutorialspoint. (n.d). Functional Programming — Lazy Evaluation. Retrieved from https://www.tutorialspoint.com/functional_programming/functional_programming_lazy_evaluation.htm

- geeksforgeeks. (2018). Higher Order Functions and Currying. Retrieved from https://www.geeksforgeeks.org/higher-order-functions-currying/

--

--

Flare

I write what I needed to read in the past | Opinions are my own.