Skip to content

Commit

Permalink
Finished episode 5
Browse files Browse the repository at this point in the history
  • Loading branch information
Sean committed Dec 29, 2009
1 parent b129975 commit 4ef7672
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/episode_003/cover.html
Expand Up @@ -5,7 +5,7 @@
<body>
<div id="title-div">
<p id="title">Full Disclojure</p>
<p id="episode-name">Episode 3 - Templates</p>
<p id="episode-name">Episode 3 : Templates</p>
</div>
</body>
</html>
2 changes: 1 addition & 1 deletion src/episode_004/cover.html
Expand Up @@ -5,7 +5,7 @@
<body>
<div id="title-div">
<p id="title">Full Disclojure</p>
<p id="episode-name">Episode 4 - Constraints</p>
<p id="episode-name">Episode 4 : Constraints</p>
</div>
</body>
</html>
23 changes: 18 additions & 5 deletions src/episode_005/README.markdown
Expand Up @@ -2,12 +2,21 @@

Episode 005

In this episode we discuss a macro that took me a while to understand when I came Clojure
In episode 2 we were discussing, I wrote the following expression:

Explain expression threading, in context of episode has nothing to do with simultaneous code.
Note: Show threaded assoc!

Today I'd like to discuss this symbol right here

Note: Highlite thread-first

This is an example of an expression threading macro. The term expression threading describes taking one
s-expression and threading it through a second one. It has nothing to do with concurrency or traditional
multithreading.

#Thread-first
Let's take a look at a very simple function definition. Here we have a function designed to convert Celsius to Fahrenheit.
In order to understand expression threading, let's take a look at a very simple function definition. Here
we have a function designed to convert Celsius to Fahrenheit.

episode-005=>(defn c-to-f [c]
(* (+ c 32) 1.8))
Expand Down Expand Up @@ -96,12 +105,16 @@ Notice how the function drags right. We can clean up this code by using thread

And, as you can see both functions work just fine.


episode-005=>(square 10)
100

episode-005=>(square->> 10)
100

So, that's how you use thread-first and thread-last. I hope this makes using these macros in you code easier.
There's one last point about the threading macros I'd like to stress. At first glance, the end result only seems to be that it made our code easier
to read. However, that misses a very subtle point. These macros actually abstracted away the process of building up a very specific nested list.
This process of abstracting away code construction is the real point of these tools, not improving code readability. However, that's a point we'll
discuss in detail another day.

Anyway, that's how you use thread-first and thread-last. I hope this gave you a better understanding of these macros, and it is now easier for you to
read code other Clojurians write. Thanks for stopping by today. I'm Sean Devlin, and this is Full Disclojure.
10 changes: 1 addition & 9 deletions src/episode_005/episode_005.clj
@@ -1,19 +1,11 @@
(ns episode-005
(:use clojure.walk))

;;;=====================
;;; Convert C to F
;;; F = (1.8 * C) + 32
;;;=====================

(defn c-to-f [c]
(* (+ c 32) 1.8))

(defn c->f [c]
(-> c
(+ 32)
(* 1.8)
))
(-> c (+ 32) (* 1.8)))


(defn square [n]
Expand Down

0 comments on commit 4ef7672

Please sign in to comment.