Skip to content

Instantly share code, notes, and snippets.

@nigelbabu
Created November 30, 2013 17:31
Show Gist options
  • Save nigelbabu/7721926 to your computer and use it in GitHub Desktop.
Save nigelbabu/7721926 to your computer and use it in GitHub Desktop.
Demonstrating some fun with python lambda
#!/usr/bin/env python
def with_lambda(hours=[]):
days = map(lambda x: x/8.0, hours)
total_days = reduce(lambda x, y: x+y, days)
return days, total_days
def without_lambda(hours=[]):
days = []
for hour in hours:
days.append(hour/8.0)
total_days = 0.0
for day in days:
total_days += day
return days, total_days
# Running this in a python shell
# >>> import lambdafun
# >>> hours = [3.5, 20.75, 0.67, 31.57, 1, 15.25, 50.66]
# >>> lambdafun.with_lambda(hours)
# ([0.4375, 2.59375, 0.08375, 3.94625, 0.125, 1.90625, 6.3325], 15.425)
# >>> lambdafun.without_lambda(hours)
# ([0.4375, 2.59375, 0.08375, 3.94625, 0.125, 1.90625, 6.3325], 15.425)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment