Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Switch to ``` clojure syntax for code snippets; requires re-running b…

…undle install!
  • Loading branch information...
commit 6b21a83580045e5b65c8c3b0f3955a9c95253bbd 1 parent 0f450d9
Michael Klishin authored October 12, 2012
1  .gitignore
@@ -12,3 +12,4 @@ _site/*
12 12
 _source/*
13 13
 deploy.sh
14 14
 repl*
  15
+_cache/*
3  Gemfile
... ...
@@ -1,6 +1,7 @@
1 1
 source :rubygems
2 2
 
3 3
 gem "jekyll"
4  
-gem "rdiscount"
  4
+# gem "rdiscount"
  5
+gem "redcarpet"
5 6
 gem "haml"
6 7
 gem "RedCloth"
4  Gemfile.lock
@@ -21,7 +21,7 @@ GEM
21 21
     maruku (0.6.0)
22 22
       syntax (>= 1.0.0)
23 23
     posix-spawn (0.3.6)
24  
-    rdiscount (1.6.8)
  24
+    redcarpet (2.2.1)
25 25
     syntax (1.0.0)
26 26
 
27 27
 PLATFORMS
@@ -31,4 +31,4 @@ DEPENDENCIES
31 31
   RedCloth
32 32
   haml
33 33
   jekyll
34  
-  rdiscount
  34
+  redcarpet
7  _config.yml
@@ -3,6 +3,9 @@ destination: ./_site
3 3
 plugins:     ./_plugins
4 4
 auto: true
5 5
 exclude: [bin, CNAME, Gemfile, Gemfile.lock, README.md]
6  
-markdown: rdiscount
7 6
 permalink: /:title
8  
-pygments: true
  7
+pygments: true
  8
+# markdown: rdiscount
  9
+markdown: redcarpet2
  10
+redcarpet:
  11
+  extensions: ["no_intra_emphasis", "fenced_code_blocks", "autolink", "tables",     "with_toc_data"]
48  _plugins/redcarpet2markdown.rb
... ...
@@ -0,0 +1,48 @@
  1
+require 'fileutils'
  2
+require 'digest/md5'
  3
+require 'redcarpet'
  4
+require 'albino'
  5
+
  6
+PYGMENTS_CACHE_DIR = File.expand_path('../../_cache', __FILE__)
  7
+FileUtils.mkdir_p(PYGMENTS_CACHE_DIR)
  8
+
  9
+class Redcarpet2Markdown < Redcarpet::Render::HTML
  10
+  def block_code(code, lang)
  11
+    lang = lang || "text"
  12
+    path = File.join(PYGMENTS_CACHE_DIR, "#{lang}-#{Digest::MD5.hexdigest code}.html")
  13
+    cache(path) do
  14
+      colorized = Albino.colorize(code, lang)
  15
+      add_code_tags(colorized, lang)
  16
+    end
  17
+  end
  18
+
  19
+  def add_code_tags(code, lang)
  20
+    code.sub(/<pre>/, "<pre><code class=\"#{lang}\">").
  21
+         sub(/<\/pre>/, "</code></pre>")
  22
+  end
  23
+
  24
+  def cache(path)
  25
+    if File.exist?(path)
  26
+      File.read(path)
  27
+    else
  28
+      content = yield
  29
+      File.open(path, 'w') {|f| f.print(content) }
  30
+      content
  31
+    end
  32
+  end
  33
+end
  34
+
  35
+class Jekyll::MarkdownConverter
  36
+  def extensions
  37
+    Hash[ *@config['redcarpet']['extensions'].map {|e| [e.to_sym, true] }.flatten ]
  38
+  end
  39
+
  40
+  def markdown
  41
+    @markdown ||= Redcarpet::Markdown.new(Redcarpet2Markdown.new(extensions), extensions)
  42
+  end
  43
+
  44
+  def convert(content)
  45
+    return super unless @config['markdown'] == 'redcarpet2'
  46
+    markdown.render(content)
  47
+  end
  48
+end
68  articles/language/core_overview.md
Source Rendered
@@ -32,25 +32,25 @@ This guide covers Clojure 1.4.
32 32
 The body of a (let ...) statement also provides an implicit (do ...) that allows for multiple statements in the body of (let ...).
33 33
 
34 34
 A basic example:
35  
-{% highlight clojure %}
  35
+``` clojure
36 36
 (let [x 1 y 2] (println x y)) ;; 1 2
37  
-{% endhighlight %}
  37
+```
38 38
 
39 39
 Let can be nested, and the scope is lexically determined. This means that a binding's value is determined by the nearest binding form for that symbol.
40 40
 
41 41
 This example basically demonstrates the lexical scoping of the let form.
42  
-{% highlight clojure %}
  42
+``` clojure
43 43
 (let [x 1]
44 44
   (println x) ;; prints 1
45 45
   (let [x 2]
46 46
     (println x))) ;; prints 2
47  
-{% endhighlight %}
  47
+```
48 48
 
49 49
 Let bindings are immutable and can be destructured.
50 50
 
51  
-{% highlight clojure %}
  51
+``` clojure
52 52
 todo - link to destructuring
53  
-{% endhighlight %}
  53
+```
54 54
 
55 55
 ### def
56 56
 
@@ -61,10 +61,10 @@ A root binding is a value that is shared across all threads.
61 61
 The (let ...) form is the preferred method of creating local bindings. It is strongly suggested to prefer it where possible, and never use (def ...) within another form.
62 62
 
63 63
 
64  
-{% highlight clojure %}
  64
+``` clojure
65 65
 ;; todo - reference to var documentation, basic example
66 66
 ;; todo - metadata
67  
-{% endhighlight %}
  67
+```
68 68
 
69 69
 ### declare
70 70
 
@@ -72,7 +72,7 @@ The (let ...) form is the preferred method of creating local bindings. It is str
72 72
 
73 73
 There are much better methods of value-based dispatch or code architecture in general, but this presents a simple situation forward declarations would be necessary.
74 74
 
75  
-{% highlight clojure %}
  75
+``` clojure
76 76
 (declare func<10 func<20)
77 77
 
78 78
 ;; without declare you will receive an error similar to:
@@ -89,7 +89,7 @@ There are much better methods of value-based dispatch or code architecture in ge
89 89
    (< x 10) (func10 x)
90 90
    (< x 20) "More than 10, less than 20"
91 91
    :else "too far!"))
92  
-{% endhighlight %}
  92
+```
93 93
 
94 94
 No matter which order you put func<10 and func<20 in, there will be a reference to a var that does not yet exist when the compiler does the initial evaluation of top-level forms.
95 95
 
@@ -101,7 +101,7 @@ No matter which order you put func<10 and func<20 in, there will be a reference
101 101
 
102 102
 Without (defn ..), a var would be directly bound to a function definition and explicit metadata about the doc string and argslits would be added manually.
103 103
 
104  
-{% highlight clojure %}
  104
+``` clojure
105 105
 (def func (fn [x] x))
106 106
 
107 107
 ;; same as:
@@ -112,11 +112,11 @@ Without (defn ..), a var would be directly bound to a function definition and ex
112 112
 
113 113
 ;;same as
114 114
 (defn func "documentation!" [x] x)
115  
-{% endhighlight %}
  115
+```
116 116
 
117  
-{% highlight clojure %}
  117
+``` clojure
118 118
 ;; todo - link to doc and metadata
119  
-{% endhighlight %}
  119
+```
120 120
 
121 121
 ### ns
122 122
 
@@ -131,7 +131,7 @@ TBD: [How to Contribute](https://github.com/clojuredocs/cds#how-to-contribute)
131 131
 If a third expression is provided, when the first expression returns nil or false the third expression is evaluated and returned.
132 132
 
133 133
 
134  
-{% highlight clojure %}
  134
+``` clojure
135 135
 user=> (if 0 "second") ;; 0 is a 'true' value. Only false or nil are 'false'
136 136
 "second"
137 137
 
@@ -146,20 +146,20 @@ nil
146 146
 
147 147
 user=> (if (nil? (= 1 2)) "second" "third") ;; differentiate between nil and false if needed
148 148
 "third"
149  
-{% endhighlight %}
  149
+```
150 150
 
151 151
 ### when
152 152
 
153 153
 (when ...) provides an implicit do form that is evaluated if an expression returns true, otherwise nil is returned. When does not provide an 'else'.
154 154
 
155  
-{% highlight clojure %}
  155
+``` clojure
156 156
 user=> (when (= 1 2) (print "hey") 10)
157 157
 nil
158 158
 
159 159
 user=> (when (< 10 11) (print "hey") 10)
160 160
 hey
161 161
 10
162  
-{% endhighlight %}
  162
+```
163 163
 
164 164
 ### for
165 165
 
@@ -167,16 +167,16 @@ hey
167 167
 
168 168
 (for ...) allows for explicit let, when and while through use of ":let []" ":when (expression)" ":while (expression)" in the binding vector.
169 169
 
170  
-{% highlight clojure %}
  170
+``` clojure
171 171
 (for [x [1 2 3] y [4 5 6]] 
172 172
   [x y])
173 173
   
174 174
 ;; ([1 4] [1 5] [1 6] [2 4] [2 5] [2 6] [3 4] [3 5] [3 6])
175  
-{% endhighlight %}
  175
+```
176 176
 
177 177
 :when only evaluates the body when a true value is returned by the expression provided
178 178
 
179  
-{% highlight clojure %}
  179
+``` clojure
180 180
 (for [x [1 2 3] y [4 5 6]
181 181
       :when (and
182 182
              (even? x)
@@ -184,17 +184,17 @@ hey
184 184
   [x y])
185 185
   
186 186
 ;; ([2 5])
187  
-{% endhighlight %}
  187
+```
188 188
 
189 189
 :while evaluates the body until a non-true value is reached. Note that the rightmost collection is fully bound to y before a non-true value of (< x 2) is reached. This demonstrates the order of the comprehension.
190 190
 
191  
-{% highlight clojure %}
  191
+``` clojure
192 192
 (for [x [1 2 3] y [4 5 6]
193 193
       :while (< x 2)]
194 194
   [x y])
195 195
   
196 196
 ;; ([1 4] [1 5] [1 6])
197  
-{% endhighlight %}
  197
+```
198 198
 
199 199
 ### doseq
200 200
 
@@ -202,29 +202,29 @@ hey
202 202
 
203 203
 (doseq ...) supports the same bindings as for - :let :when :while. For examples of these, see for.
204 204
 
205  
-{% highlight clojure %}
  205
+``` clojure
206 206
 (doseq [x [1 2 3] y [4 5 6]]
207 207
   (println [x y]))
208 208
   
209 209
 ;; [1 4][1 5][1 6][2 4][2 5][2 6][3 4][3 5][3 6]
210 210
 ;; nil
211  
-{% endhighlight %}
  211
+```
212 212
 
213 213
 ### apply
214 214
 
215 215
 (apply ...) effectively unrolls the supplied args and a collection into a list of arguments to the supplied function.
216 216
 
217  
-{% highlight clojure %}
  217
+``` clojure
218 218
 (str ["Hel" "lo"])
219 219
 "[\"Hel\" \"lo\"]" ;; not what we want, str is operating on the vector
220 220
 
221 221
 user> (apply str ["Hel" "lo"]) ;; same as (str "Hel" "lo")
222 222
 "Hello"
223  
-{% endhighlight %}
  223
+```
224 224
 
225 225
 (apply ...) prepends any supplied arguments to the form as well.
226 226
 
227  
-{% highlight clojure %}
  227
+``` clojure
228 228
 (map + [[1 2 3] [1 2 3]]) ;; This attempts to add 2 vectors with +
229 229
 ;; ClassCastException   java.lang.Class.cast (Class.java:2990)
230 230
 
@@ -233,7 +233,7 @@ user> (apply str ["Hel" "lo"]) ;; same as (str "Hel" "lo")
233 233
 
234 234
 (apply + 1 2 3 [4 5 6]) ;; same as  (+ 1 2 3 4 5 6)
235 235
 ;; 21
236  
-{% endhighlight %}
  236
+```
237 237
 
238 238
 Note that apply can not be used with macros.
239 239
 
@@ -257,17 +257,17 @@ TBD: [How to Contribute](https://github.com/clojuredocs/cds#how-to-contribute)
257 257
 
258 258
 Returns a count of the number of items in a collection. Nil returns a value of 0.
259 259
 
260  
-{% highlight clojure %}
  260
+``` clojure
261 261
 (count "Hello")
262 262
 ;; 5
263 263
 
264 264
 (count [1 2 3 4 5 6 7])
265 265
 ;; 7
266  
-{% endhighlight %}
  266
+```
267 267
 
268 268
 Note that count does not return in constant time for all collections. This can be determined with (counted? ...). Lazy sequences must be realized to get a count of the items.
269 269
 
270  
-{% highlight clojure %}
  270
+``` clojure
271 271
 (counted? "Hello")
272 272
 ;; false
273 273
 
@@ -276,7 +276,7 @@ Note that count does not return in constant time for all collections. This can b
276 276
 
277 277
 (counted? [1 2 3 4 5]) ;; Constant time return of (count)
278 278
 ;; true 
279  
-{% endhighlight %}
  279
+```
280 280
 
281 281
 ### conj
282 282
 
156  articles/language/functions.md
Source Rendered
@@ -31,12 +31,12 @@ Clojure is a functional programming language. Naturally, functions are very impo
31 31
 
32 32
 Functions are typically defined using the [defn](http://clojuredocs.org/clojure_core/clojure.core/defn) macro:
33 33
 
34  
-{% highlight clojure %}
  34
+``` clojure
35 35
 (defn round
36 36
   [d precision]
37 37
   (let [factor (Math/pow 10 precision)]
38 38
     (/ (Math/floor (* d factor)) factor)))
39  
-{% endhighlight %}
  39
+```
40 40
 
41 41
 Type hints sometimes allow the compiler to avoid reflective method calls and/or produce significantly more efficient bytecode.
42 42
 However, as a rule of thumb, it is usually not necessary to use type hints. Start writing your code without them. The compiler
@@ -45,35 +45,35 @@ is also free to ignore provided hints.
45 45
 Functions can have doc strings (documentation strings) and it is a good idea to document functions that
46 46
 are part of the public API:
47 47
 
48  
-{% highlight clojure %}
  48
+``` clojure
49 49
 (defn round
50 50
   "Round down a double to the given precision (number of significant digits)"
51 51
   [d precision]
52 52
   (let [factor (Math/pow 10 precision)]
53 53
     (/ (Math/floor (* d factor)) factor)))
54  
-{% endhighlight %}
  54
+```
55 55
 
56 56
 In Clojure, function arguments may have optional type hints:
57 57
 
58  
-{% highlight clojure %}
  58
+``` clojure
59 59
 (defn round
60 60
   [^double d ^long precision]
61 61
   (let [factor (Math/pow 10 precision)]
62 62
     (/ (Math/floor (* d factor)) factor)))
63  
-{% endhighlight %}
  63
+```
64 64
 
65 65
 
66 66
 Functions can also define *preconditions* and *postconditions* that put restrictions on argument values and
67 67
 the value function returns:
68 68
 
69  
-{% highlight clojure %}
  69
+``` clojure
70 70
 (defn round
71 71
   "Round down a double to the given precision (number of significant digits)"
72 72
   [^double d ^long precision]
73 73
   {:pre [(not-nil? d) (not-nil? precision)]}
74 74
   (let [factor (Math/pow 10 precision)]
75 75
     (/ (Math/floor (* d factor)) factor)))
76  
-{% endhighlight %}
  76
+```
77 77
 
78 78
 In the example above, we use preconditions to check that both arguments are not nil. The `not-nil?` macro (or function) is not
79 79
 demonstrated in this example and assumed to be implemented elsewhere.
@@ -83,34 +83,34 @@ demonstrated in this example and assumed to be implemented elsewhere.
83 83
 
84 84
 Anonymous functions are defined using the `fn` special form:
85 85
 
86  
-{% highlight clojure %}
  86
+``` clojure
87 87
 (fn [x]
88 88
   (* 2 x))
89  
-{% endhighlight %}
  89
+```
90 90
 
91 91
 Anonymous functions can be assigned to locals, passed between functions (higher order functions are covered later in this document)
92 92
 and returned from functions:
93 93
 
94  
-{% highlight clojure %}
  94
+``` clojure
95 95
 (let [f (fn [x]
96 96
           (* 2 x))]
97 97
   (map f (range 0 10)))
98  
-{% endhighlight %}
  98
+```
99 99
 
100 100
 There is also a reader macro for anonymous functions:
101 101
 
102  
-{% highlight clojure %}
  102
+``` clojure
103 103
 (let [f #(* 2 %)]
104 104
   (map f (range 0 10)))
105  
-{% endhighlight %}
  105
+```
106 106
 
107 107
 The `%` in the example above means "the first argument". To refer to more than one argument, use `%1`, `%2` and so on:
108 108
 
109  
-{% highlight clojure %}
  109
+``` clojure
110 110
 ;; an anonymous function that takes 3 arguments and adds them together
111 111
 (let [f #(+ %1 %2 %3)]
112 112
   (f 1 2 3))
113  
-{% endhighlight %}
  113
+```
114 114
 
115 115
 Please **use this reader macro sparingly**; excessive use may lead to unreadable code.
116 116
 
@@ -119,24 +119,24 @@ Please **use this reader macro sparingly**; excessive use may lead to unreadable
119 119
 
120 120
 Functions are invoked by placing a function to the leading position (*the calling position*) of a list:
121 121
 
122  
-{% highlight clojure %}
  122
+``` clojure
123 123
 (format "Hello, %s" "world")
124  
-{% endhighlight %}
  124
+```
125 125
 
126 126
 This works also if you have a function stored in a local, a var or passed as an argument:
127 127
 
128  
-{% highlight clojure %}
  128
+``` clojure
129 129
 (let [f format]
130 130
   (f "Hello, %s" "world"))
131  
-{% endhighlight %}
  131
+```
132 132
 
133 133
 Alternatively, you can call a function using [clojure.core/apply](http://clojuredocs.org/clojure_core/clojure.core/apply)
134 134
 
135  
-{% highlight clojure %}
  135
+``` clojure
136 136
 (apply format "Hello, %s" ["world"])
137 137
 
138 138
 (apply format "Hello, %s %s" ["Clojure" "world"])
139  
-{% endhighlight %}
  139
+```
140 140
 
141 141
 `clojure.core/apply` is usually only necessary when calling variadic functions or having the list of arguments passed in
142 142
 as a collection.
@@ -146,13 +146,13 @@ as a collection.
146 146
 
147 147
 Functions in Clojure can have multiple *arities*, or sets of arguments:
148 148
 
149  
-{% highlight clojure %}
  149
+``` clojure
150 150
 (defn tax-amount
151 151
   ([amount]
152 152
      (tax-amount amount 35))
153 153
   ([amount rate]
154 154
      (Math/round (double (* amount (/ rate 100))))))
155  
-{% endhighlight %}
  155
+```
156 156
 
157 157
 In the example above, the version of the function that takes only one argument (so called *one-arity* or *1-arity* function)
158 158
 calls another version (*2-arity*) with a default parameter. This is a common use case for multiple arities: to have default
@@ -164,7 +164,7 @@ parameters may or may not be available to the compiler.
164 164
 
165 165
 A larger example:
166 166
 
167  
-{% highlight clojure %}
  167
+``` clojure
168 168
 (defn range
169 169
   ([]
170 170
     (range 0 Double/POSITIVE_INFINITY 1))
@@ -174,7 +174,7 @@ A larger example:
174 174
     (range start end 1))
175 175
   ([start end step]
176 176
     (comment Omitted for clarity)))
177  
-{% endhighlight %}
  177
+```
178 178
 
179 179
 
180 180
 
@@ -184,22 +184,22 @@ A larger example:
184 184
 Sometimes function arguments are data structures: vectors, sequences, maps. To access parts of such
185 185
 data structure, you may do something like this:
186 186
 
187  
-{% highlight clojure %}
  187
+``` clojure
188 188
 (defn currency-of
189 189
   [m]
190 190
   (let [currency (get m :currency)]
191 191
     currency))
192  
-{% endhighlight %}
  192
+```
193 193
 
194 194
 For vector arguments:
195 195
 
196  
-{% highlight clojure %}
  196
+``` clojure
197 197
 (defn currency-of
198 198
   [pair]
199 199
   (let [amount   (first  pair)
200 200
         currency (second pair)]
201 201
     currency))
202  
-{% endhighlight %}
  202
+```
203 203
 
204 204
 However, this is boilerplate code that has little to do with what the function really does. Clojure
205 205
 lets developer **destructure** parts of arguments, for both maps and sequences.
@@ -210,111 +210,111 @@ Destructuring over vectors (**positional destructuring**) works like this: you r
210 210
 with a vector that has "placeholders" (symbols) in positions you want to bind. For example, if the
211 211
 argument is known to be a pair and you need second argument, it would look like this:
212 212
 
213  
-{% highlight clojure %}
  213
+``` clojure
214 214
 (defn currency-of
215 215
   [[amount currency]]
216 216
   currency)
217  
-{% endhighlight %}
  217
+```
218 218
 
219 219
 In the example above the first element in the pair is bound to `amount` and the second one is bound to
220 220
 `currency`. So far so good. However, notice that we do not use the `amount` local. In that case, we can
221 221
 ignore it by replacing it with an underscore:
222 222
 
223  
-{% highlight clojure %}
  223
+``` clojure
224 224
 (defn currency-of
225 225
   [[_ currency]]
226 226
   currency)
227  
-{% endhighlight %}
  227
+```
228 228
 
229 229
 Destructuring can nest (destructure deeper than one level):
230 230
 
231  
-{% highlight clojure %}
  231
+``` clojure
232 232
 (defn first-first
233 233
   [[[i _] _]]
234 234
   i)
235  
-{% endhighlight %}
  235
+```
236 236
 
237 237
 While this article does not cover `let` and locals, it is worth demonstrating that positional destructuring works
238 238
 exactly the same way for let bindings:
239 239
 
240  
-{% highlight clojure %}
  240
+``` clojure
241 241
 (let [pair         [10 :gbp]
242 242
       [_ currency] pair]
243 243
   currency)
244  
-{% endhighlight %}
  244
+```
245 245
 
246 246
 
247 247
 ### Map Destructuring
248 248
 
249 249
 Destructuring over maps and records (**map destructuring**) works slightly differently:
250 250
 
251  
-{% highlight clojure %}
  251
+``` clojure
252 252
 (defn currency-of
253 253
   [{currency :currency}]
254 254
   currency)
255  
-{% endhighlight %}
  255
+```
256 256
 
257 257
 In this case example, we want to bind the value for key `:currency` to `currency`. Keys don't have to be
258 258
 keywords:
259 259
 
260  
-{% highlight clojure %}
  260
+``` clojure
261 261
 (defn currency-of
262 262
   [{currency "currency"}]
263 263
   currency)
264  
-{% endhighlight %}
  264
+```
265 265
 
266  
-{% highlight clojure %}
  266
+``` clojure
267 267
 (defn currency-of
268 268
   [{currency 'currency}]
269 269
   currency)
270  
-{% endhighlight %}
  270
+```
271 271
 
272 272
 When destructuring multiple keys at once, it is more convenient to use a slightly different syntax:
273 273
 
274  
-{% highlight clojure %}
  274
+``` clojure
275 275
 (defn currency-of
276 276
   [{:keys [currency amount]}]
277 277
   currency)
278  
-{% endhighlight %}
  278
+```
279 279
 
280 280
 The example above assumes that map keys will be keywords and we are interested in two values: `currency`
281 281
 and `amount`. The same can be done for strings:
282 282
 
283  
-{% highlight clojure %}
  283
+``` clojure
284 284
 (defn currency-of
285 285
   [{:strs [currency amount]}]
286 286
   currency)
287  
-{% endhighlight %}
  287
+```
288 288
 
289 289
 and symbols:
290 290
 
291  
-{% highlight clojure %}
  291
+``` clojure
292 292
 (defn currency-of
293 293
   [{:syms [currency amount]}]
294 294
   currency)
295  
-{% endhighlight %}
  295
+```
296 296
 
297 297
 In practice, keywords are very commonly used for map keys so destructuring with `{:keys [...]}` is very common
298 298
 as well.
299 299
 
300 300
 Map destructuring also lets us specify default values for keys that may be missing:
301 301
 
302  
-{% highlight clojure %}
  302
+``` clojure
303 303
 (defn currency-of
304 304
   [{:keys [currency amount] :or {currency :gbp}}]
305 305
   currency)
306  
-{% endhighlight %}
  306
+```
307 307
 
308 308
 This is very commonly used for implementing functions that take "extra options" (faking named arguments support).
309 309
 
310 310
 
311 311
 Just like with positional destructuring, map destructuring works exactly the same way for let bindings:
312 312
 
313  
-{% highlight clojure %}
  313
+``` clojure
314 314
 (let [money               {:currency :gbp :amount 10}
315 315
      {currency :currency} money]
316 316
   currency)
317  
-{% endhighlight %}
  317
+```
318 318
 
319 319
 
320 320
 ## Variadic Functions
@@ -322,42 +322,42 @@ Just like with positional destructuring, map destructuring works exactly the sam
322 322
 Variadic functions are functions that take varying number of arguments (some arguments are optional). Two examples
323 323
 of such function in `clojure.core` are `clojure.core/str` and `clojure.core/format`:
324 324
 
325  
-{% highlight clojure %}
  325
+``` clojure
326 326
 (str "a" "b")     ;= "ab"
327 327
 (str "a" "b" "c") ;= "abc"
328 328
 
329 329
 (format "Hello, %s" "world")              ;= "Hello, world"
330 330
 (format "Hello, %s %s" "Clojure" "world") ;= "Hello, Clojure world"
331  
-{% endhighlight %}
  331
+```
332 332
 
333 333
 To define a variadic function, prefix optional arguments with an ampersand (`&`):
334 334
 
335  
-{% highlight clojure %}
  335
+``` clojure
336 336
 (defn log
337 337
   [message & args]
338 338
   (comment ...))
339  
-{% endhighlight %}
  339
+```
340 340
 
341 341
 In the example above, one argument is requried and the rest is optional. Variadic functions
342 342
 are invoked as usual:
343 343
 
344  
-{% highlight clojure %}
  344
+``` clojure
345 345
 (defn log
346 346
   [message & args]
347 347
   (println "args: " args))
348 348
 
349 349
 (log "message from " "192.0.0.76")
350  
-{% endhighlight %}
  350
+```
351 351
 
352 352
 Running the example above in the REPL produces:
353 353
 
354  
-{% highlight clojure %}
  354
+``` clojure
355 355
 user=> (log "message from " "192.0.0.76")
356 356
 args:  (192.0.0.76)
357 357
 
358 358
 user=> (log "message from " "192.0.0.76" "service:xyz")
359 359
 args:  (192.0.0.76 service:xyz)
360  
-{% endhighlight %}
  360
+```
361 361
 
362 362
 As you can see, optional arguments (`args`) are packed into a list.
363 363
 
@@ -367,23 +367,23 @@ Named parameters are achieved through the use of destructuring a variadic functi
367 367
 
368 368
 Approaching named parameters from the standpoint of destructuring a variadic function allows for more clearly readable function invocations.  This is an example of named parameters:
369 369
 
370  
-{% highlight clojure %}
  370
+``` clojure
371 371
 (defn job-info
372 372
   [& {:keys [name job income] :or {job "unemployed" income "$0.00"}}]
373 373
   (if name
374 374
     [name job income]
375 375
     (println "No name specified")))
376  
-{% endhighlight %}
  376
+```
377 377
 
378 378
 Using the function looks like this:
379 379
 
380  
-{% highlight clojure %}
  380
+``` clojure
381 381
 user=> (job-info :name "Robert" :job "Engineer")
382 382
 ["Robert" "Engineer" "$0.00"]
383 383
 
384 384
 user=> (job-info :job "Engineer")
385 385
 No name specified
386  
-{% endhighlight %}
  386
+```
387 387
 
388 388
 Without the use of a variadic argument list, you would have to call the function with a single map argument such as `{:name "Robert" :job "Engineer}`.
389 389
 
@@ -402,32 +402,32 @@ TBD: [How to contribute](https://github.com/clojuredocs/cds#how-to-contribute)
402 402
 
403 403
 In Clojure, keywords can be used as functions. They take a map or record and look themselves up in it:
404 404
 
405  
-{% highlight clojure %}
  405
+``` clojure
406 406
 (:age {:age 27 :name "Michael"}) ;= 27
407  
-{% endhighlight %}
  407
+```
408 408
 
409 409
 This is commonly used with higher order functions:
410 410
 
411  
-{% highlight clojure %}
  411
+``` clojure
412 412
 (map :age [{:age 45 :name "Joe"} {:age 42 :name "Jill"} {:age 17 :name "Matt"}]) ;= (45 42 17)
413  
-{% endhighlight %}
  413
+```
414 414
 
415 415
 and the `->` macro:
416 416
 
417  
-{% highlight clojure %}
  417
+``` clojure
418 418
 (-> [{:age 45 :name "Joe"} {:age 42 :name "Jill"}] first :name) ;= "Joe"
419  
-{% endhighlight %}
  419
+```
420 420
 
421 421
 
422 422
 ## Maps as Functions
423 423
 
424 424
 Clojure maps are also functions that take keys and look up values for them:
425 425
 
426  
-{% highlight clojure %}
  426
+``` clojure
427 427
 ({:age 42 :name "Joe"} :name)    ;= "Joe"
428 428
 ({:age 42 :name "Joe"} :age)     ;= 42
429 429
 ({:age 42 :name "Joe"} :unknown) ;= nil
430  
-{% endhighlight %}
  430
+```
431 431
 
432 432
 Note that this is **not true** for Clojure records, which are almost identical to maps in other
433 433
 cases.
@@ -435,24 +435,24 @@ cases.
435 435
 
436 436
 ## Sets as Functions
437 437
 
438  
-{% highlight clojure %}
  438
+``` clojure
439 439
 (#{1 2 3} 1)  ;= 1
440 440
 (#{1 2 3} 10) ;= 10
441 441
 
442 442
 (#{:us :au :ru :uk} :uk) ;= :uk
443 443
 (#{:us :au :ru :uk} :cn) ;= nil
444  
-{% endhighlight %}
  444
+```
445 445
 
446 446
 This is often used to check if a value is in a set:
447 447
 
448  
-{% highlight clojure %}
  448
+``` clojure
449 449
 (when (countries :in)
450 450
   (comment ...))
451 451
 
452 452
 (if (countries :in)
453 453
   (comment Implement positive case)
454 454
   (comment Implement negative case))
455  
-{% endhighlight %}
  455
+```
456 456
 
457 457
 because everything but `false` and `nil` evaluates to `true` in Clojure.
458 458
 
94  articles/language/interop.md
Source Rendered
@@ -40,21 +40,21 @@ Java classes can be referenced either using their fully-qualified names (FQNs) s
40 40
 `java.util.Date` or be *imported* in the current Clojure namespace using `clojure.core/import` and
41 41
 referenced by short names:
42 42
 
43  
-{% highlight clojure %}
  43
+``` clojure
44 44
 java.util.Date ;= java.util.Date
45  
-{% endhighlight %}
  45
+```
46 46
 
47  
-{% highlight clojure %}
  47
+``` clojure
48 48
 (import java.util.Date)
49 49
 
50 50
 Date ;= java.util.Date
51  
-{% endhighlight %}
  51
+```
52 52
 
53 53
 `ns` macro supports imports, too:
54  
-{% highlight clojure %}
  54
+``` clojure
55 55
 (ns myservice.main
56 56
   (:import java.util.Date))
57  
-{% endhighlight %}
  57
+```
58 58
 
59 59
 More about the `ns` macro can be found in the article on [Clojure namespaces](/articles/language/namespaces.html).
60 60
 
@@ -66,7 +66,7 @@ protocol/service/feature and it is not possible to tell which one should be used
66 66
 In Java, classes can be nested inside other classes. They are called *inner classes* and by convention,
67 67
 separated from their outer class by a dollar sign (`$`):
68 68
 
69  
-{% highlight clojure %}
  69
+``` clojure
70 70
 (import java.util.Map$Entry)
71 71
 
72 72
 Map$Entry ;= java.util.Map$Entry
@@ -75,7 +75,7 @@ Map$Entry ;= java.util.Map$Entry
75 75
 (import com.rabbitmq.client.AMQP$BasicProperties)
76 76
 
77 77
 AMQP$BasicProperties ;= com.rabbitmq.client.AMQP$BasicProperties
78  
-{% endhighlight %}
  78
+```
79 79
 
80 80
 Note that if you need to use both a class and one or more of its inner classes, they all need to be imported separately.
81 81
 As far as JVM is concerned, they are all separate classes, there is no "imports hierarchy".
@@ -85,30 +85,30 @@ As far as JVM is concerned, they are all separate classes, there is no "imports
85 85
 
86 86
 Java classes are instantiated using the `new` special form:
87 87
 
88  
-{% highlight clojure %}
  88
+``` clojure
89 89
 (new java.util.Date) ;= #inst "2012-10-09T21:23:57.278-00:00"
90  
-{% endhighlight %}
  90
+```
91 91
 
92 92
 However, Clojure reader provides a bit of syntactic sugar and you are much more likely
93 93
 to see it used:
94 94
 
95  
-{% highlight clojure %}
  95
+``` clojure
96 96
 (java.util.Date.)    ;= #inst "2012-10-09T21:24:43.878-00:00"
97  
-{% endhighlight %}
  97
+```
98 98
 
99 99
 It is possible to use fully qualified names (e.g. `java.util.Date`) or short names with imports:
100 100
 
101  
-{% highlight clojure %}
  101
+``` clojure
102 102
 (import java.util.Date)
103 103
 
104 104
 (Date.) ;= #inst "2012-10-09T21:24:27.229-00:00"
105  
-{% endhighlight %}
  105
+```
106 106
 
107 107
 An example with constructor arguments:
108 108
 
109  
-{% highlight clojure %}
  109
+``` clojure
110 110
 (java.net.URI. "http://clojure.org") ;= #<URI http://clojure.org>
111  
-{% endhighlight %}
  111
+```
112 112
 
113 113
 ## How to Invoke Java Methods
114 114
 
@@ -116,44 +116,44 @@ An example with constructor arguments:
116 116
 
117 117
 Instance methods are invoked using the `.` special form:
118 118
 
119  
-{% highlight clojure %}
  119
+``` clojure
120 120
 (let [d (java.util.Date.)]
121 121
   (. d getTime)) ;= 1349819873183
122  
-{% endhighlight %}
  122
+```
123 123
 
124 124
 Just like with object instantiation, it is much more common to see an alternative version:
125 125
 
126  
-{% highlight clojure %}
  126
+``` clojure
127 127
 (let [d (java.util.Date.)]
128 128
   (.getTime d)) ;= 1349819873183
129  
-{% endhighlight %}
  129
+```
130 130
 
131 131
 
132 132
 ### Static Methods
133 133
 
134 134
 Static methods can be invoked with the same `.` special form:
135 135
 
136  
-{% highlight clojure %}
  136
+``` clojure
137 137
 (. Math floor 5.677) ;= 5.0
138  
-{% endhighlight %}
  138
+```
139 139
 
140 140
 or (typically) to sugared version, `ClassName/methodName`:
141 141
 
142  
-{% highlight clojure %}
  142
+``` clojure
143 143
 (Math/floor 5.677) ;= 5.0
144 144
 
145 145
 (Boolean/valueOf "false") ;= false
146 146
 (Boolean/valueOf "true")  ;= true
147  
-{% endhighlight %}
  147
+```
148 148
 
149 149
 
150 150
 ### Chained Calls
151 151
 
152 152
 It is possible to chain method calls using the `..` special form:
153 153
 
154  
-{% highlight clojure %}
  154
+``` clojure
155 155
 (.. (Date.) getTime toString) ;= "1349821993809"
156  
-{% endhighlight %}
  156
+```
157 157
 
158 158
 
159 159
 ## How to Access Java Fields
@@ -161,7 +161,7 @@ It is possible to chain method calls using the `..` special form:
161 161
 Public mutable fields are not common in Java libraries but sometimes you need to access them.
162 162
 It's done with the same dot special form:
163 163
 
164  
-{% highlight clojure %}
  164
+``` clojure
165 165
 (import java.awt.Point)
166 166
 
167 167
 (let [pt (Point. 0 10)]
@@ -169,11 +169,11 @@ It's done with the same dot special form:
169 169
 
170 170
 (let [pt (Point. 0 10)]
171 171
   (. pt y)) ;= 10
172  
-{% endhighlight %}
  172
+```
173 173
 
174 174
 and just like with instance methods, it is much more common to see the following version:
175 175
 
176  
-{% highlight clojure %}
  176
+``` clojure
177 177
 (import java.awt.Point)
178 178
 
179 179
 (let [pt (Point. 0 10)]
@@ -181,7 +181,7 @@ and just like with instance methods, it is much more common to see the following
181 181
 
182 182
 (let [pt (Point. 0 10)]
183 183
   (.y pt)) ;= 10
184  
-{% endhighlight %}
  184
+```
185 185
 
186 186
 
187 187
 ## How to Set Java Fields
@@ -189,11 +189,11 @@ and just like with instance methods, it is much more common to see the following
189 189
 To set a public mutable field, use `clojure.core/set!` that takes a field in the dot notation
190 190
 demonstrated earlier and a new value:
191 191
 
192  
-{% highlight clojure %}
  192
+``` clojure
193 193
 (let [pt (Point. 0 10)]
194 194
   (set! (.y pt) 100)
195 195
   (.y pt)) ;= 100
196  
-{% endhighlight %}
  196
+```
197 197
 
198 198
 Fortunately, mutable public fields are rare to meet in the JVM ecosystem so you won't need
199 199
 to do this often.
@@ -204,21 +204,21 @@ to do this often.
204 204
 [Enums (enumeration) type](http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html) values are accessed
205 205
 the same way as fields, except on enum classes and not objects:
206 206
 
207  
-{% highlight clojure %}
  207
+``` clojure
208 208
 java.util.concurrent.TimeUnit/MILLISECONDS ;= #< MILLISECONDS>
209  
-{% endhighlight %}
  209
+```
210 210
 
211 211
 
212 212
 ## Determining Classes of Java Objects
213 213
 
214 214
 To get class of a particular value, pass it to `clojure.core/class`:
215 215
 
216  
-{% highlight clojure %}
  216
+``` clojure
217 217
 (class 1)      ;= java.lang.Long
218 218
 (class 1.0)    ;= java.lang.Double
219 219
 (class "docs") ;= java.lang.String
220 220
 (class (java.net.URI. "http://github.com")) ;= java.net.URI
221  
-{% endhighlight %}
  221
+```
222 222
 
223 223
 As this example demonstrates, Clojure strings are JVM strings, integer literals are compiled
224 224
 as longs and floating point literals are compiled as doubles.
@@ -228,9 +228,9 @@ as longs and floating point literals are compiled as doubles.
228 228
 
229 229
 To obtain a class reference by its string name (fully qualified), use `Class/forName` via Java interop:
230 230
 
231  
-{% highlight clojure %}
  231
+``` clojure
232 232
 (Class/forName "java.util.Date") ;= java.util.Date
233  
-{% endhighlight %}
  233
+```
234 234
 
235 235
 ### Array Types, Primitives
236 236
 
@@ -316,25 +316,25 @@ interface FilenameFilter {
316 316
      */
317 317
     boolean accept(File dir, String name);
318 318
 }
319  
-{% endhighlight %}
  319
+```
320 320
 
321 321
 here is how to implement it in Clojure:
322 322
 
323  
-{% highlight clojure %}
  323
+``` clojure
324 324
 ;; a FileFilter implementation that accepts everything
325 325
 (reify java.io.FilenameFilter
326 326
   (accept [this dir name]
327 327
     true))
328  
-{% endhighlight %}
  328
+```
329 329
 
330 330
 `reify` takes an interface (fully-qualified name or short name) and one or more
331 331
 method implementations that mimic function definitions without the `defn` and with
332 332
 *this* (as in Java, JavaScript or *self* in Ruby, Python) reference being the first argument:
333 333
 
334  
-{% highlight clojure %}
  334
+``` clojure
335 335
 (accept [this dir name]
336 336
   true)
337  
-{% endhighlight %}
  337
+```
338 338
 
339 339
 With `reify`, generally there is no need to add type hints on arguments: Clojure
340 340
 compiler typically will detect the best matching method (by name and number of arguments).
@@ -343,19 +343,19 @@ compiler typically will detect the best matching method (by name and number of a
343 343
 the interface and instantiate it. To demonstrate that reified objects indeed implement
344 344
 the interface:
345 345
 
346  
-{% highlight clojure %}
  346
+``` clojure
347 347
 (let [ff (reify java.io.FilenameFilter
348 348
            (accept [this dir name]
349 349
              true))]
350 350
   (instance? java.io.FileFilter ff)) ;= true
351  
-{% endhighlight %}
  351
+```
352 352
 
353 353
 ### Example 1
354 354
 
355 355
 The following example demonstrates how instances created with `reify` are passed around
356 356
 as regular Java objects:
357 357
 
358  
-{% highlight clojure %}
  358
+``` clojure
359 359
 (import java.io.File)
360 360
 
361 361
 ;; a file filter implementation that keeps only .clj files
@@ -364,7 +364,7 @@ as regular Java objects:
364 364
              (.endsWith name ".clj")))
365 365
     dir  (File. "/Users/antares/Development/ClojureWerkz/neocons.git/")]
366 366
   (into [] (.listFiles dir ff))) ;= [#<File /Users/antares/Development/ClojureWerkz/neocons.git/project.clj>]
367  
-{% endhighlight %}
  367
+```
368 368
 
369 369
 
370 370
 ### Clojure Functions Implement Runnable and Callable
40  articles/language/namespaces.md
Source Rendered
@@ -28,21 +28,20 @@ Java packages and Python modules. Namespaces are basically maps (dictionaries) t
28 28
 to *vars*. In many cases, those vars store functions in them.
29 29
 
30 30
 
31  
-
32 31
 ## Defining a Namespace
33 32
 
34 33
 Namespaces are usually defined using the `clojure.core/ns` macro. In its basic
35 34
 form, it takes a name as a symbol:
36 35