Chapter Review Questions#
In your own words, what makes a function pure?
Classify each of the following as pure or side-effecting:
len(items)print(items)items.append(x)sorted(items)
What is a higher-order function? Give one example that takes a function and one that returns a function.
Three built-in shapes of data processing.
What does
mapdo?What does
filterdo?What does
functools.reducedo?
Rewrite
[n * n for n in nums if n % 2 == 0]usingmapandfilter. Which version do you find clearer, and why?Why does
reducetake a start value, and what does that value have to do with empty sequences?What is a closure? In
make_multiplierfrom this chapter, what does the returned function “remember”?The counter built with
nonlocalis convenient but no longer pure. Explain why, and what you give up.What does
functools.partial(power, exponent=2)produce, and how is it different from callingpower?If
h = compose(f, g), which offandgruns first when you callh(x)?Aliasing.
a = [1, 2, 3] b = a b.append(4)
What is
aafterward?How would you append to
bwithout changinga?
Describe the functional core, imperative shell pattern. Why is the core easy to test and the shell hard?
Why does a dynamically typed language like Python rely on testing more than a statically typed one does?
Three layers of testing.
What is a doctest, and where does it live?
How does a property-based test differ from an example-based test?
Give one property (invariant, round-trip, or oracle) you could test for a function that sorts a list.
Name the three equivalent models of computation discussed in the overview, and say which one functional programming descends from.