Skip to content

Commit da8e557

Browse files
authored
Final QA (#562)
1 parent ac31789 commit da8e557

File tree

9 files changed

+19
-31
lines changed

9 files changed

+19
-31
lines changed

functional-programming-python/function_scopes.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
def outer():
77
def inner():
88
print("I am function inner()!")
9+
return "return value from inner()"
910

1011
# Function outer() returns function inner().
1112
return inner

functional-programming-python/functions_as_arguments.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ def reverse_len(s):
1919
print(sorted(animals, key=reverse_len))
2020

2121
# Using a lambda expression
22-
sorted(animals, key=lambda s: -len(s))
22+
print(sorted(animals, key=lambda s: -len(s)))

functional-programming-python/functions_overview.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def func():
88
print("I am function func()!")
99

1010

11-
print(func())
11+
func()
1212

1313

1414
# Assign it to a new name.
@@ -20,9 +20,9 @@ def func():
2020

2121
# Access a function object in a list.
2222
objects = ["cat", func, 42]
23-
objects[1]
23+
print(objects[1])
2424

25-
print(objects[1]())
25+
objects[1]()
2626

2727
# Use a function object as a key in a dictionary.
2828
# Note that this is probably not a good idea.

functional-programming-python/higher_order_reduce.py

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,44 +3,36 @@
33
functional versions of `map()` and `filter()`.
44
"""
55

6-
# Compare `map()` to `custom_map()`.
7-
numbers = [1, 2, 3, 4, 5]
6+
from functools import reduce
87

9-
print(list(map(str, numbers)))
8+
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
109

1110

11+
# Compare `map()` to `custom_map()`.
1212
def custom_map(function, iterable):
13-
from functools import reduce
14-
1513
return reduce(
1614
lambda items, value: items + [function(value)],
1715
iterable,
1816
[],
1917
)
2018

2119

20+
print(list(map(str, numbers)))
2221
print(list(custom_map(str, numbers)))
2322

2423

2524
# Compare `filter()` to `custom_filter()`.
26-
numbers = list(range(10))
27-
28-
2925
def is_even(x):
3026
return x % 2 == 0
3127

3228

33-
print(list(filter(is_even, numbers)))
34-
35-
3629
def custom_filter(function, iterable):
37-
from functools import reduce
38-
3930
return reduce(
4031
lambda items, value: items + [value] if function(value) else items,
4132
iterable,
4233
[],
4334
)
4435

4536

37+
print(list(filter(is_even, numbers)))
4638
print(list(custom_filter(is_even, numbers)))

functional-programming-python/lambda_expressions.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ def reverse(s):
88
return s[::-1]
99

1010

11-
reverse("I am a string")
12-
11+
print(reverse("I am a string"))
1312

1413
reverse = lambda s: s[::-1] # noqa E731
1514
print(reverse("I am a string"))

functional-programming-python/map_multiple.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ def add_three(a, b, c):
1313
print(
1414
list(
1515
map(
16-
(lambda a, b, c: a + b + c),
17-
[1, 2, 3],
18-
[10, 20, 30],
19-
[100, 200, 300],
16+
lambda a, b, c: a + b + c,
17+
[1, 2, 3, 4],
18+
[10, 20, 30, 40],
19+
[100, 200, 300, 400],
2020
)
2121
)
2222
)

functional-programming-python/map_single.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,17 @@ def reverse(s):
1717

1818
# Consume the iterator with a for loop.
1919
iterator = map(reverse, animals)
20-
for i in iterator:
21-
print(i)
20+
for animal in iterator:
21+
print(animal)
2222

2323
# Consume the iterator with a call to `list()`.
2424
iterator = map(reverse, animals)
2525
print(list(iterator))
2626

27-
2827
# Use a lambda expression.
2928
iterator = map(lambda s: s[::-1], animals)
3029
print(list(iterator))
3130

32-
3331
# All the code in a single line.
3432
print(list(map(lambda s: s[::-1], ["cat", "dog", "hedgehog", "gecko"])))
3533

functional-programming-python/reduce_with_initializer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ def add_two_values(x, y):
1717
print(reduce(lambda x, y: x + y, [1, 2, 3, 4, 5], 100))
1818

1919
# Use `sum()` and add a value instead.
20-
print(100 + sum([1, 2, 3, 4, 5]))
20+
print(sum([1, 2, 3, 4, 5], start=100))

functional-programming-python/reduce_with_two.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ def multiply(x, y):
2525

2626

2727
def factorial_with_reduce(n):
28-
from functools import reduce
29-
3028
return reduce(multiply, range(1, n + 1))
3129

3230

@@ -54,5 +52,5 @@ def greater(x, y):
5452

5553
# Use lambda expressions to complete the tasks.
5654
print(reduce(lambda x, y: x + y, [1, 2, 3, 4, 5]))
57-
print(reduce(lambda x, y: x + y, ["foo", "bar", "baz", "quz"]))
55+
print(reduce(lambda x, y: x + y, ["cat", "dog", "hedgehog", "gecko"]))
5856
print(reduce((lambda x, y: x if x > y else y), [23, 49, 6, 32]))

0 commit comments

Comments
 (0)