mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-08-10 08:44:28 +02:00
Add examples of higher-order functions taking multiple arguments
This commit is contained in:
@@ -473,9 +473,12 @@ add_10(3) # => 13
|
|||||||
|
|
||||||
# There are also anonymous functions
|
# There are also anonymous functions
|
||||||
(lambda x: x > 2)(3) # => True
|
(lambda x: x > 2)(3) # => True
|
||||||
|
(lambda x, y: x ** 2 + y ** 2)(2, 1) # => 5
|
||||||
|
|
||||||
# There are built-in higher order functions
|
# There are built-in higher order functions
|
||||||
map(add_10, [1, 2, 3]) # => [11, 12, 13]
|
map(add_10, [1, 2, 3]) # => [11, 12, 13]
|
||||||
|
map(max, [1, 2, 3], [4, 2, 1]) # => [4, 2, 3]
|
||||||
|
|
||||||
filter(lambda x: x > 5, [3, 4, 5, 6, 7]) # => [6, 7]
|
filter(lambda x: x > 5, [3, 4, 5, 6, 7]) # => [6, 7]
|
||||||
|
|
||||||
# We can use list comprehensions for nice maps and filters
|
# We can use list comprehensions for nice maps and filters
|
||||||
|
@@ -550,10 +550,13 @@ add_10(3) # => 13
|
|||||||
|
|
||||||
# There are also anonymous functions
|
# There are also anonymous functions
|
||||||
(lambda x: x > 2)(3) # => True
|
(lambda x: x > 2)(3) # => True
|
||||||
|
(lambda x, y: x ** 2 + y ** 2)(2, 1) # => 5
|
||||||
|
|
||||||
# TODO - Fix for iterables
|
# TODO - Fix for iterables
|
||||||
# There are built-in higher order functions
|
# There are built-in higher order functions
|
||||||
map(add_10, [1, 2, 3]) # => [11, 12, 13]
|
map(add_10, [1, 2, 3]) # => [11, 12, 13]
|
||||||
|
map(max, [1, 2, 3], [4, 2, 1]) # => [4, 2, 3]
|
||||||
|
|
||||||
filter(lambda x: x > 5, [3, 4, 5, 6, 7]) # => [6, 7]
|
filter(lambda x: x > 5, [3, 4, 5, 6, 7]) # => [6, 7]
|
||||||
|
|
||||||
# We can use list comprehensions for nice maps and filters
|
# We can use list comprehensions for nice maps and filters
|
||||||
|
Reference in New Issue
Block a user