"익명 함수"의 두 판 사이의 차이

잔글 (봇: 자동으로 텍스트 교체 (-</source> +</syntaxhighlight>, -<source +<syntaxhighlight ))
 
5번째 줄: 5번째 줄:


==Python 예시==
==Python 예시==
<source lang='Python'>
<syntaxhighlight lang='Python'>
print( (lambda x: x*x)(5) )
print( (lambda x: x*x)(5) )
# 25
# 25
</source>
</syntaxhighlight>
<source lang='Python'>
<syntaxhighlight lang='Python'>
print( lambda x: x*x )
print( lambda x: x*x )
# <function <lambda> at 0x02D30390>
# <function <lambda> at 0x02D30390>
</source>
</syntaxhighlight>
<source lang='Python'>
<syntaxhighlight lang='Python'>
def square(x): return x*x
def square(x): return x*x
print( square )
print( square )
# <function square at 0x02D678A0>
# <function square at 0x02D678A0>
</source>
</syntaxhighlight>


;filter, map, reduce (Python 3)
;filter, map, reduce (Python 3)
<source lang='Python'>
<syntaxhighlight lang='Python'>
numbers = [1, 2, 3, 4, 5, 6]
numbers = [1, 2, 3, 4, 5, 6]
print( list(filter(lambda x: x % 2 == 0, numbers)) )
print( list(filter(lambda x: x % 2 == 0, numbers)) )
29번째 줄: 29번째 줄:
print( functools.reduce(lambda x, y: x + y, numbers) )
print( functools.reduce(lambda x, y: x + y, numbers) )
# 21
# 21
</source>
</syntaxhighlight>


==같이 보기==
==같이 보기==

2021년 8월 23일 (월) 22:45 기준 최신판

1 개요[ | ]

anonymous function, function constant, function literal, lambda function
익명 함수, 람다 함수, 축약 함수
  • 대리자나 식 트리 형식을 만드는 데 사용할 수 있음

2 Python 예시[ | ]

print( (lambda x: x*x)(5) )
# 25
print( lambda x: x*x )
# <function <lambda> at 0x02D30390>
def square(x): return x*x
print( square )
# <function square at 0x02D678A0>
filter, map, reduce (Python 3)
numbers = [1, 2, 3, 4, 5, 6]
print( list(filter(lambda x: x % 2 == 0, numbers)) )
# [2, 4, 6]
print( list(map(lambda x: x * 2 - 1, numbers)) )
# [1, 3, 5, 7, 9, 11]
import functools
print( functools.reduce(lambda x, y: x + y, numbers) )
# 21

3 같이 보기[ | ]

4 참고[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}