clojurian

A collection of modules providing syntax and utility functions inspired by Clojure.

  1. clojurian
    1. Overview
    2. Modules
      1. clojurian-syntax
      2. clojurian-atom (experimental)
    3. About this egg
      1. Source
      2. Authors
      3. Version history
      4. License

Overview

Currently this egg provides two modules: clojurian-syntax and clojurian-atom.

Modules

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.

Example:

(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.

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.

Example:

(->> (iota 10)
     (map add1)
     (fold + 0)
     (print))

=>

(print
 (fold + 0
  (map add1
   (iota 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.

clojurian-atom (experimental)

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

Source

The source code is hosted at Bitbucket. Feel free to fork it and send pull requests there.

Authors

Moritz Heidkamp

Original doto implementation by Martin DeMello

Version history

0.0.4
Preserve #f return values of the then clause in if-let* (thanks to Andy Bennett for pointing this out)
0.0.3
Prefix all clojurian-atom identifiers with atom-
0.0.2
Add if-let, if-let* and clojurian-atom
0.0.1
Initial release

License

 Copyright (c) 2012, 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.