diff --git a/content/cftbat/functional-programming.html b/content/cftbat/functional-programming.html index 19db1dd6..59cdc330 100644 --- a/content/cftbat/functional-programming.html +++ b/content/cftbat/functional-programming.html @@ -639,7 +639,7 @@
The first element of the tuple is a position number, and the second is that position’s information. filter
then applies the anonymous function #(get (second %) :pegged)
to each of these tuples, filtering out the tuples where the position’s information indicates that the position is not currently housing a peg. Finally, the result is passed to map
, which calls first
on each tuple to grab just the position number from the tuples.
After you get a seq of pegged positions numbers, you call a predicate function on each one to find the first position that returns a truthy value. The predicate function is created with (
comp not-empty (
partial valid-moves board))
. The idea is to first return a map of all valid moves for a position and then test whether that map is empty.
After you get a seq of pegged positions numbers, you call a predicate function on each one to find the first position that returns a truthy value. The predicate function is created with (comp not-empty (
partial valid-moves board))
. The idea is to first return a map of all valid moves for a position and then test whether that map is empty.
First, the expression (partial valid-moves board)
derives an anonymous function from valid-moves
with the first argument, board
, filled in using partial
(because you’re using the same board each time you call valid-moves
). The new function can take a position and return the map of all its valid moves for the current board.
Second, you use comp
to compose this function with not-empty
. This function is self-descriptive; it returns true
if the given collection is empty and false
otherwise.
What’s most interesting about this bit of code is that you’re using a chain of functions to derive a new function, similar to how you use chains of functions to derive new data. In Chapter 3, you learned that Clojure treats functions as data in that functions can receive functions as arguments and return them. Hopefully, this shows why that feature is fun and useful.