From 4ef76723cfa13fe1b04a21136fb2fafc76b50e1d Mon Sep 17 00:00:00 2001 From: Sean Date: Tue, 29 Dec 2009 15:47:21 -0500 Subject: [PATCH] Finished episode 5 --- src/episode_003/cover.html | 2 +- src/episode_004/cover.html | 2 +- src/episode_005/README.markdown | 23 ++++++++++++++++++----- src/episode_005/episode_005.clj | 10 +--------- 4 files changed, 21 insertions(+), 16 deletions(-) diff --git a/src/episode_003/cover.html b/src/episode_003/cover.html index 8b0d262..82efda6 100644 --- a/src/episode_003/cover.html +++ b/src/episode_003/cover.html @@ -5,7 +5,7 @@

Full Disclojure

-

Episode 3 - Templates

+

Episode 3 : Templates

\ No newline at end of file diff --git a/src/episode_004/cover.html b/src/episode_004/cover.html index 9a68699..73cb0bb 100644 --- a/src/episode_004/cover.html +++ b/src/episode_004/cover.html @@ -5,7 +5,7 @@

Full Disclojure

-

Episode 4 - Constraints

+

Episode 4 : Constraints

\ No newline at end of file diff --git a/src/episode_005/README.markdown b/src/episode_005/README.markdown index c3e5ae1..07b931f 100644 --- a/src/episode_005/README.markdown +++ b/src/episode_005/README.markdown @@ -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)) @@ -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. \ No newline at end of file diff --git a/src/episode_005/episode_005.clj b/src/episode_005/episode_005.clj index 75c14eb..74b04b8 100644 --- a/src/episode_005/episode_005.clj +++ b/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]