Skip to content

Instantly share code, notes, and snippets.

@letoh
Last active December 12, 2015 08:48
Show Gist options
  • Select an option

  • Save letoh/4746357 to your computer and use it in GitHub Desktop.

Select an option

Save letoh/4746357 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
def outer():
times_called = 0
def inner():
print "called", times_called, "times"
return inner
f = outer()
f()
#!/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
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])
#!/usr/bin/env python
def outer():
times_called = 0
def inner():
times_called += 1
print "called", times_called, "times"
return inner
f = outer()
f()
#!/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