brave-clojure-web icon indicating copy to clipboard operation
brave-clojure-web copied to clipboard

Clarification on chapter 5

Open rcanepa opened this issue 9 years ago • 1 comments

Hi Daniel,

After presenting the function can-move?, you wrote this:

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.

But it seems that it is wrong, because from official Clojure's docs, the function not-empty return nil if the collection is empty, and collection otherwise.

For reference, this is the function that I am talking about:

(defn can-move?
  "Do any of the pegged positions have valid moves?"
  [board]
  (some (comp not-empty (partial valid-moves board))
        (map first (filter #(get (second %) :pegged) board))))

Also, I wanted to say thank you for writing this great book. I am really enjoying it and learning a lot!

rcanepa avatar Nov 11 '15 01:11 rcanepa

Funny enough, due to the implementation of valid-moves, if a position doesn't have a valid move, the result is nil, not an empty collection. So as far as I can see, composing not-empty is doing nothing.

Odder yet some is a function that returns the first truthy value in a collection, meaning the return type of can-move? will not be boolean, but a truthy value (in this case, the first position found that can still be moved to) or nil. I'm new to Clojure, but I thought functions ending in ? are generally supposed to either return true or false. Again, I could be wrong, but it just seems inconsistent.

hallfox avatar Jan 05 '16 05:01 hallfox