What is the difference between a ‘closure’ and a ‘lambda’?

A lambda is just an anonymous function – a function defined with no name. In some languages, such as Scheme, they are equivalent to named functions. In fact, the function definition is re-written as binding a lambda to a variable internally. In other languages, like Python, there are some (rather needless) distinctions between them, but … Read more

Python: Why is functools.partial necessary?

What functionality does functools.partial offer that you can’t get through lambdas? Not much in terms of extra functionality (but, see later) – and, readability is in the eye of the beholder. Most people who are familiar with functional programming languages (those in the Lisp/Scheme families in particular) appear to like lambda just fine – I … Read more

How do I replace while loops with a functional programming alternative without tail call optimization?

An example in JavaScript Here’s an example using JavaScript. Currently, most browsers do not support tail call optimisation and therefore the following snippet will fail const repeat = n => f => x => n === 0 ? x : repeat (n – 1) (f) (f(x)) console.log(repeat(1e3) (x => x + 1) (0)) // 1000 … Read more

What is Scala’s yield?

I think the accepted answer is great, but it seems many people have failed to grasp some fundamental points. First, Scala’s for comprehensions are equivalent to Haskell’s do notation, and it is nothing more than a syntactic sugar for composition of multiple monadic operations. As this statement will most likely not help anyone who needs … Read more