None

dev notes

Lambda

07 Jan 2014

Tag: fp lambda

Python

Python supports the creation of anonymous functions (i.e. functions that are not bound to a name) at runtime, using a construct called “lambda”. This is not exactly the same as lambda in functional programming languages, but it is a very powerful concept that’s well integrated into Python and is often used in conjunction with typical functional concepts like filter(), map() and reduce().

functional programming in python ``` python »> mult3 = filter(lambda x: x % 3 == 0, [1, 2, 3, 4, 5, 6, 7, 8, 9]) »> print mult3 [3, 6, 9]

foo = [2, 18, 9, 22, 17, 24, 8, 12, 27]

print filter(lambda x: x % 3 == 0, foo) [18, 9, 24, 12, 27]

print map(lambda x: x * 2 + 10, foo) [14, 46, 28, 54, 44, 58, 26, 34, 64]

print reduce(lambda x, y: x + y, foo) 139

```

Scala

Reference:


comments powered by Disqus