Last active
December 12, 2015 08:48
-
-
Save letoh/4746357 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| def outer(): | |
| times_called = 0 | |
| def inner(): | |
| print "called", times_called, "times" | |
| return inner | |
| f = outer() | |
| f() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| ret = lambda y: y | |
| bind = lambda x, f=ret: f(x) | |
| def foo(): | |
| data = 0 | |
| def bar(): | |
| bar.get = lambda: data | |
| bar.set = lambda n: bind( | |
| [None for data in [n]], lambda _: | |
| ret(data)) | |
| return bar | |
| return bar | |
| a = foo() | |
| assert a().set(5) == 5 | |
| assert a().get() == 5 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def Foo(xs, acc = {}): | |
| if not xs: | |
| return acc['a0'] + acc['a1'] if acc else 0 | |
| if not acc: | |
| acc['a0'], acc['a1'] = xs[0], 0 | |
| else: | |
| acc['a0'], acc['a1'] = xs[0], acc['a0'] + acc['a1'] | |
| return Foo(xs[1:], acc) | |
| print Foo([1,2,3,4]) | |
| print Foo([1,2,3]) # wrong | |
| def Foo(xs0): | |
| def Bar(xs, acc = {}): | |
| if not xs: | |
| return acc['a0'] + acc['a1'] if acc else 0 | |
| if not acc: | |
| acc['a0'], acc['a1'] = xs[0], 0 | |
| else: | |
| acc['a0'], acc['a1'] = xs[0], acc['a0'] + acc['a1'] | |
| return Bar(xs[1:], acc) | |
| return Bar(xs0) | |
| print Foo([1,2,3,4]) | |
| print Foo([1,2,3]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| def outer(): | |
| times_called = 0 | |
| def inner(): | |
| times_called += 1 | |
| print "called", times_called, "times" | |
| return inner | |
| f = outer() | |
| f() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| def outer(): | |
| times_called = 0 | |
| def inner(): | |
| nonlocal times_called | |
| times_called += 1 | |
| print("called", times_called, "times") | |
| return inner | |
| f = outer() | |
| f() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment