003-005. Lambda
@
Lambda makes method one sentence
@
lambda parameters: method expression
@
def hap(x, y):
return x + y
hap(10, 20)
# 30
(lambda x,y: x + y)(10, 20)
# 30
@
map(method, list)
# python2
# [0,1,2,3,4]
map(lambda x: x ** 2, range(5))
# [0, 1, 4, 9, 16]
# python2, 3
# [0,1,2,3,4]
list(map(lambda x: x ** 2, range(5)))
# [0, 1, 4, 9, 16]
@
# reduce(method, ordered type data)
# ordered type data is string, list, tuple
# reduce() applies elements of ordered type data accumulatively
# You should use following code in python 3
from functools import reduce
reduce(lambda x, y: x + y, [0, 1, 2, 3, 4])
# 10
reduce(lambda x, y: y + x, 'abcde')
# 'edcba'
@
filter(method, list)
# python2
filter(lambda x: x < 5, range(10))
# [0, 1, 2, 3, 4]
# python2,3
list(filter(lambda x: x < 5, range(10)))
# [0, 1, 2, 3, 4]
filter(lambda x: x % 2, range(10))
# [1, 3, 5, 7, 9]
list(filter(lambda x: x % 2, range(10))) # 파이썬 2 및 파이썬 3
# [1, 3, 5, 7, 9]