May 2018
Beginner to intermediate
290 pages
6h 43m
English
Let’s start our adventures in functional programming by imagining that we have decided to add price and genre to the maps we’ve been using to keep track of our books, like this:
| | (def dracula {:title "Dracula" |
| | :author "Stoker" |
| | :price 1.99 |
| | :genre :horror}) |
Further, let’s imagine that we need to write some code to distinguish the books based on an arbitrary price:
| | (defn cheap? [book] |
| | (when (<= (:price book) 9.99) |
| | book)) |
| | |
| | (defn pricey? [book] |
| | (when (> (:price book) 9.99) |
| | book)) |
| | |
| | (cheap? dracula) ; Yes! |
| | (pricey? dracula) ; No! |
or the genre:
| | (defn horror? [book] |
| | (when (= (:genre book) :horror) |
| | book)) |
| | |
| | (defn adventure? [book] |
| | (when (= (:genre book) :adventure) ... |
Read now
Unlock full access