Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clarification on chapter 5 #83

Open
rcanepa opened this issue Nov 11, 2015 · 1 comment
Open

Clarification on chapter 5 #83

rcanepa opened this issue Nov 11, 2015 · 1 comment

Comments

@rcanepa
Copy link

rcanepa commented Nov 11, 2015

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!

@hallfox
Copy link

hallfox commented Jan 5, 2016

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants