Skip to content

Instantly share code, notes, and snippets.

@slpsys
Created December 14, 2010 02:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slpsys/739920 to your computer and use it in GitHub Desktop.
Save slpsys/739920 to your computer and use it in GitHub Desktop.
A magical journey through bullshit.
>>> weight
10
>>> a
[1, 2, 3]
# ok, we have a nice list here. let's apply a map() to see if we can raise the power of all values:
>>> map((lambda x: x**2), a)
[1, 4, 9]
# sweet, it works! ok, now let's try to fold in your example of iterating and incrementing:
>>> map((lambda x: weight += x), a)
File "<stdin>", line 1
map((lambda x: weight += x), a)
^
SyntaxError: invalid syntax
# b..but..
>>> what
Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'what' is not defined
# ok, well. maybe `lambda` in Python is not a closure, which would mean it doesn't have access to the surrounding stack frame and all its variables.
>>> map((lambda x: x + weight), a)
[11, 12, 13]
# a;dfkjasdf;lkasjdf ok, it is an actual closure. that's annoying. let's..hm. _how about reduce()_? we know from Lisp that transforms a vector to a scalar..
>>> reduce((lambda x,y: x + y), a)
6
# yay, it works!
>>> exit
'Use Ctrl-Z plus Return to exit.'
# ok, you can just fuck right along now.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment