4 ms·
Hope this helps: (Python) def closure(x): return lambda y:x+y a = closure(5) #'a' is a function now a(2) #returns 7 a(3) #returns 8 The function 'closure'
by jrvarela56 15y ago
Hope this helps:
(Python)
def closure(x):
return lambda y:x+y
a = closure(5) #'a' is a function now
a(2) #returns 7
a(3) #returns 8
The function 'closure' can be carried around and it remembers the value of x from when it was created.
Got the example from this article: http://linuxgazette.net/109/pramode.html http://linuxgazette.net/109/pramode.html (I believe it was shared a few weeks ago) - great reference for other FP concepts and a good way to get started in FP if you already like working with Python.