From 9cab6f16eb4ad096d0d98e088ddd61937011dee9 Mon Sep 17 00:00:00 2001 From: Daniel Higginbotham Date: Mon, 23 May 2016 20:19:29 -0400 Subject: [PATCH] errata --- .bundle/config | 1 + content/cftbat/core-functions-in-depth.html | 4 ++-- content/cftbat/multimethods-records-protocols.html | 2 +- content/index.html | 10 +++++----- lib/redcarpet_syntax_highlighter.rb | 1 - 5 files changed, 9 insertions(+), 9 deletions(-) create mode 100644 .bundle/config diff --git a/.bundle/config b/.bundle/config new file mode 100644 index 00000000..2fbf0ffd --- /dev/null +++ b/.bundle/config @@ -0,0 +1 @@ +--- {} diff --git a/content/cftbat/core-functions-in-depth.html b/content/cftbat/core-functions-in-depth.html index 4f83579d..abac04b2 100644 --- a/content/cftbat/core-functions-in-depth.html +++ b/content/cftbat/core-functions-in-depth.html @@ -214,7 +214,7 @@

map

; => ({:human 8.1, :critter 0.0} {:human 7.3, :critter 0.2} {:human 6.6, :critter 0.3} - {:human 5.0, :critter 1.8}) + {:human 5.0, :critter 1.1})

Good job laying off the human!

@@ -260,7 +260,7 @@

reduce

:min (inc 10)) -

The function assoc takes three arguments: a map, a key, and a value. It returns derives a new map from the map you give it by associating the given key with the given value. For example, (assoc {:a 1} :b 2)) would return {:a 1 :b 2}

+

The function assoc takes three arguments: a map, a key, and a value. It derives a new map from the map you give it by associating the given key with the given value. For example, (assoc {:a 1} :b 2) would return {:a 1 :b 2}

Another use for reduce is to filter out keys from a map based on their value. In the following example, the anonymous function checks whether the value of a key-value pair is greather than 4. If it isn’t, then the key-value pair is filtered out. In the map {:human 4.1 :critter 3.9}, 3.9 is less than 4, so the :critter key and its 3.9 value are filtered out.

(reduce (fn [new-map [key val]]
diff --git a/content/cftbat/multimethods-records-protocols.html b/content/cftbat/multimethods-records-protocols.html
index 2112f29c..fd082fca 100644
--- a/content/cftbat/multimethods-records-protocols.html
+++ b/content/cftbat/multimethods-records-protocols.html
@@ -246,7 +246,7 @@ 

Records

We’ve created a new protocol, WereCreature , with one method, full-moon-behavior ➋. At ➌, defrecord implements WereCreature for WereWolf. The most interesting part of the full-moon-behavior implementation is that you have access to name. You also have access to title and any other fields that might be defined for your record. You can also extend records using extend-type and extend-protocol.

When should you use records, and when should you use maps? In general, you should consider using records if you find yourself creating maps with the same fields over and over. This tells you that that set of data represents information in your application’s domain, and your code will communicate its purpose better if you provide a name based on the concept you’re trying to model. Not only that, but record access is more performant than map access, so your program will become a bit more efficient. Finally, if you want to use protocols, you’ll need to create a record.

Further Study

-

Clojure offers other tools for working with abstractions and data types. These tools, which I consider advanced, include deftype, reify, and proxy. If you’re interested in learning more, check out the documentation on data types at http://clojure.org/datatypes/.

+

Clojure offers other tools for working with abstractions and data types. These tools, which I consider advanced, include deftype, reify, and proxy. If you’re interested in learning more, check out the documentation on data types at http://clojure.org/datatypes/.

Summary

One of Clojure’s design principles is to write to abstractions. In this chapter, you learned how to define your own abstractions using multimethods and prototypes. These constructs provide polymorphism, allowing the same operation to behave differently based on the arguments it’s given. You also learned how to create and use your own associative data types with defrecord and how to extend records to implement protocols.

When I first started learning Clojure, I was pretty shy about using multi­methods, protocols, and records. However, they are used often in Clojure libraries, so it’s good to know how they work. Once you get the hang of them, they’ll help you write cleaner code.

diff --git a/content/index.html b/content/index.html index 77435cb7..3372c219 100644 --- a/content/index.html +++ b/content/index.html @@ -157,11 +157,11 @@

Brave Clojure Quests

Learn by Email

- The Clojure Atom is a weekly email I'm - cooking up. Every Thursday, you'll get a single link - to a high-quality, free instructional resource. Once - it's ready, anyway. Sign up now so you don't miss a - single issue! + The Clojure Atom is a bi-weekly email I'm + cooking up. Every other Thursday, you'll get a single + link to a high-quality, free instructional + resource. Once it's ready, anyway. Sign up now so you + don't miss a single issue!

" + Pygments.highlight(code, :lexer => language) + "
" end end -