A collection of modules providing syntax and utility functions inspired by Clojure.
Currently this egg provides two modules: clojurian-syntax and clojurian-atom.
clojurian-syntax[syntax] (doto val (proc args ...) ...)
Inserts val as the first argument of each (proc args ...) clause (i.e. right after proc). Returns val. This is useful e.g. for mutating a record on initialization.
(define boolean-vector (doto (make-vector 100) (vector-fill! #t 0 50) (vector-fill! #f 50))) => (define boolean-vector (let ((vec (make-vector 100))) (vector-fill! vec #t 0 50) (vector-fill! vec #f 50) vec))[syntax] (-> val (proc args ...) ...)
[syntax] (->* val (proc args ...) ...)
Inserts val as the first argument of the first (proc args ...) clause. The resulting form is then inserted as the first argument of the following proc form and so on. This is known as the thrush combinator. The starred version (->*) is multi value aware, i.e. each form's return values are spliced into the argument list of the successing form. As a shorthand it is also possible to pass proc instead of (proc).
Single value example:
(-> 10 (- 2) (/ 5) (* 3)) => (* (/ (- 10 2) 5) 3)
Multi value example:
(->* (values 1 2) (list)) => (receive args (values 1 2) (apply list args))[syntax] (->> val (proc args ...) ...)
[syntax] (->>* val (proc args ...) ...)
Works just like -> and ->* only that the forms are inserted at the end of each successive clause's argument list.
(->> (iota 10) (map add1) (fold + 0) (print)) => (print (fold + 0 (map add1 (iota 10))))[syntax] (as-> val name forms ...)
Evaluates forms in order in a scope where name is bound to val for the first form, the result of that for the second form, the result of that for the third form, and so forth. Returns the result of the last form.
(as-> 3 x (+ x 7) (/ x 2)) => 5
It's mainly useful in combination with ->:
(-> 10 (+ 3) (+ 7) (as-> x (/ 200 x))) => 10[syntax] (if-let (var val) then else)
Equivalent to (let ((var val)) (if var then else)).[syntax] (if-let* ((x1 y1) (x2 y2) ...) then else)
Similar to (or (and-let* ((x1 y1) (x2 y2) ...) then) else) except that returning #f from the then clause will not lead to the else clause being evaluated.
This module provides an implementation of Clojure's atom reference type. It is in an experimental state right now and only provides the very basic functionality, e.g. validators and watches are still missing. Note that atoms only work with immutable data structures so they can't be used to coordinate updates of record slots, for example.[procedure] (atom value)
Returns and atom with the given initial value.[procedure] (atom? x)
Checks whether x is an atom.[procedure] (atom-value atom)
Returns the current value of atom.[procedure] (atom-compare-and-set! atom old new)
Atomically sets the value of atom to new if its current value is eq? to the given old value. Returns #t if the update was successful and #f if not.[procedure] (atom-swap! atom proc . args)
Atomically swaps the value of atom to be (apply proc (atom-value atom) args). Because proc may be called multiple times it must be free of side-effects. Returns the new value of atom.[procedure] (atom-reset! atom value)
Sets the value of atom to value. Returns value.
About this egg
The source code is hosted at Bitbucket. Feel free to fork it and send pull requests there.
Original doto implementation by Martin DeMello
- Add support for non-parenthesized clauses to the thrushes (thanks Even Hanson). Add as-> (thanks Jan Herich).
- Preserve #f return values of the then clause in if-let* (thanks to Andy Bennett for pointing this out)
- Prefix all clojurian-atom identifiers with atom-
- Add if-let, if-let* and clojurian-atom
- Initial release
Copyright (c) 2014, Moritz Heidkamp All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. Neither the name of the author nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.