Skip to content

Instantly share code, notes, and snippets.

@jfriedly
Last active August 29, 2015 14:00
Show Gist options
  • Save jfriedly/11064368 to your computer and use it in GitHub Desktop.
Save jfriedly/11064368 to your computer and use it in GitHub Desktop.
greater_42 = (num for num in mylist if num > 42)
square = (num ** 2 for num in greater_42)
retval = 0
for num in square:
    retval = 3 * retval + num

vs.

retval = 0
for num in (y ** 2 for y in (z for z in mylist if z > 42)):
    retval = 3 * retval + num

vs.

reduce(lambda w,x: w * 3 + x,
       map(lambda y: y ** 2,
           filter(lambda z: z > 42, mylist)), 0)

vs.

reduce(lambda w,x: w * 3 + x, map(lambda y: y ** 2, filter(lambda z: z > 42, mylist)), 0)

vs.

def greater_42(iterable):
    for num in iterable:
        if num > 42:
            yield num

def square(iterable):
    for num in iterable:
        yield num ** 2

retval = 0
for num in square(greater_42(mylist)):
    retval = 3 * retval + num
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment