August 2014
Intermediate to advanced
120 pages
2h 45m
English
In order to avoid symbol capture issues like the one we just saw, Clojure gives us a few tools, all related to the gensym function. gensym’s job is simple: it produces a symbol with a unique name. The names will look funny because the name needs to be unique for the application, but that’s OK because we never need to type them into code:
| advanced_mechanics/gensym_2.clj | |
| | user=> (gensym) |
| | ;=> G__671 |
| | user=> (gensym) |
| | ;=> G__674 |
| | user=> (gensym "xyz") |
| | ;=> xyz677 |
| | user=> (gensym "xyz") |
| | ;=> xyz680 |
As you can see, any given invocation of gensym gives a unique value back—so if you want to refer to the same one twice, you’ll need to hold onto the value with a let binding or something similar. ...
Read now
Unlock full access