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

"( comp not-empty" to "(comp not-empty" #200

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion content/cftbat/functional-programming.html
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ <h3>Moving Pegs</h3>
</code></pre></div></div>

<p class="Body">The first element of the tuple is a position number, and the second is that position’s information. <code>filter</code> then applies the anonymous function <code>#(get (second %) :pegged)</code> 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 <code>map</code>, which calls <code>first</code> on each tuple to grab just the position number from the tuples.</p>
<p class="Body">After you get a seq of pegged positions numbers, you call a predicate <span>function on each one to find the first position that returns a truthy value. The predicate function is created with </span><code>(</code><code>comp not-empty (</code><code>partial valid-moves board))</code><span>. The idea is to first return a map of all valid moves for a position </span>and then test whether that map is empty.</p>
<p class="Body">After you get a seq of pegged positions numbers, you call a predicate <span>function on each one to find the first position that returns a truthy value. The predicate function is created with </span><code>(comp not-empty (</code><code>partial valid-moves board))</code><span>. The idea is to first return a map of all valid moves for a position </span>and then test whether that map is empty.</p>
<p class="Body">First, the expression <code>(partial valid-moves board)</code> derives an anonymous <span>function from </span><code>valid-moves</code><span> with the first argument, </span><code>board</code><span>, filled in using </span><code>partial</code><span> (because you’re using the same board each time you call </span><code>valid-moves</code><span>). </span>The new function can take a position and return the map of all its valid moves for the current board.</p>
<p class="Body">Second, you use <code>comp</code> to compose this function with <code>not-empty</code>. This function is self-descriptive; it returns <code>true</code> if the given collection is empty and <code>false</code> otherwise.</p>
<p class="Body"><span>What’s most interesting about this bit of code is that you’re using a </span>chain of functions to derive a new function, similar to how you use chains <span>of functions to derive new data. In Chapter 3, you learned that Clojure treats functions as data in that functions can receive functions as argu</span>ments and return them. Hopefully, this shows why that feature is fun and u<a id="Anchor-16"></a>seful.</p>
Expand Down