You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/guides/weird_characters.adoc
+187Lines changed: 187 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -352,3 +352,190 @@ user=> (def x "x") ; this is a comment
352
352
user=> ; this is a comment too
353
353
<returns nothing>
354
354
----
355
+
356
+
357
+
// =================================
358
+
359
+
== ` - Syntax quote
360
+
361
+
See `~@` (unquote splicing) and `~` (unquote) for additional information
362
+
````` is the syntax quote. When used on a symbol it resolves to the symbol
363
+
in the current context:
364
+
[source,clojure]
365
+
----
366
+
user=> (def five 5)
367
+
#'user/five
368
+
user=> `five
369
+
user/five
370
+
----
371
+
When used with lists (remember everything in Clojure is data) it forms a
372
+
*template* for the data strucutre and won't immediately resolve it.
373
+
374
+
[source,clojure]
375
+
----
376
+
user=> (1 2 3)
377
+
ClassCastException java.lang.Long cannot be cast to clojure.lang.IFn user/eval832 (NO_SOURCE_FILE:1)
378
+
user=> `(1 2 3)
379
+
(1 2 3)
380
+
----
381
+
You'll see this most often in teh context of macros. We can write one now:
382
+
[source,clojure]
383
+
----
384
+
user=> (defmacro debug [body]
385
+
#_=> `(let [val# ~body]
386
+
#_=> (println "DEBUG: " val#)
387
+
#_=> val#))
388
+
#'user/debug
389
+
user=> (debug (+ 2 2))
390
+
DEBUG: 4
391
+
4
392
+
----
393
+
The macro takes a single statement and wraps it in a *quoted* `let` block,
394
+
evaluates and prints the result and then evaluates the body. In effect this
395
+
`defmacro` call returns a quoted data structure representing the program we
396
+
are writing with it. The ``` allows this to happen.
397
+
398
+
* http://www.braveclojure.com/writing-macros/[Clojure for the Brave and True - Writing Macros]
399
+
* http://aphyr.com/posts/305-clojure-from-the-ground-up-macros[Clojure from the ground up: macros]
400
+
* http://clojure.org/macros[Clojure Official Documentation]
401
+
402
+
== \*var-name* - Earmuffs
403
+
404
+
Earmuffs (a pair of asterisk bookending var names) is a *naming convention* in
405
+
many LISPs used to denote *special vars*. Most commonly in Clojure this seems
406
+
to be used to denote *dynamic* vars, i.e. ones that can change depending on
407
+
where you are in the program. The earmuffs act as a warning that "here be dragons"
408
+
and to never assume the state of the var. Remember, this is a *convention*, not a
409
+
*rule*.
410
+
411
+
Core Clojure exampels are `\*out*` and `\*in*` which represent the standard in and
412
+
out writers for Clojure.
413
+
414
+
* http://stackoverflow.com/questions/1986961/how-is-the-var-name-naming-convention-used-in-clojure[How is the var-name naming-convention used in clojure?]
415
+
* http://clojure.github.io/clojure/clojure.core-api.html#clojure.core/\*out*[Clojure API Docs]
416
+
417
+
== >!!, <!!, >! and <! - core.async channel macros
418
+
419
+
These symbols are channel operations in `core.async` - a Clojure/ClojureScript
420
+
library for channel based asynchronous programming (specifically http://en.wikipedia.org/wiki/Communicating_sequential_processes[CSP - Communicating Sequential Processes]).
421
+
422
+
If you imagine, for the sake of argument, a channel is a bit like a queue that
423
+
things can put stuff on and take stuff off, then these symbols support that
424
+
simple API.
425
+
426
+
* `>!!` and `<!!` are *blocking put* and *take* respectively
427
+
* `>!` and `<!`are, simply *put* and *take*
428
+
429
+
THe difference being the blocking version operate outside `go` blocks and block
430
+
the tread they operate on.
431
+
[source,clojure]
432
+
----
433
+
user=> (def my-channel (chan 10)) ; create a channel
434
+
user=> (>!! my-channel "hello") ; put stuff on the channel
435
+
user=> (println (<!! my-channel)) ; take stuff off the channel
436
+
hello
437
+
----
438
+
The non-blocking version sneed to be executed within a `go` block, otherwise
439
+
they'll throw an exception.
440
+
[source,clojure]
441
+
----
442
+
user=> (def c (chan))
443
+
#'user/c
444
+
user=> (>! c "nope")
445
+
AssertionError Assert failed: >! used not in (go ...) block
446
+
nil clojure.core.async/>! (async.clj:123)
447
+
----
448
+
While the diffence between these is well outside the scope of this guide,
449
+
fundamentally the `go` blocks operate and manage their own resources pausing
450
+
*execution* of code without blocking threads. This makes asynchronously executed
451
+
code appear to be synchronous, removing the pain of managing
0 commit comments