chickadee » clojurian

Outdated egg!

This is an egg for CHICKEN 4, the unsupported old release. You're almost certainly looking for the CHICKEN 5 version of this egg, if it exists.

If it does not exist, there may be equivalent functionality provided by another egg; have a look at the egg index. Otherwise, please consider porting this egg to the current version of CHICKEN.

clojurian

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

Overview

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

Modules

clojurian-syntax

(doto val (proc args ...) ...)syntax

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))
(-> val (proc args ...) ...)syntax
(->* val (proc args ...) ...)syntax

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))
(->> val (proc args ...) ...)syntax
(->>* val (proc args ...) ...)syntax

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))))
(as-> val name forms ...)syntax

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.

Examples:

(as-> 3 x (+ x 7) (/ x 2)) => 5

It's mainly useful in combination with ->:

(-> 10 (+ 3) (+ 7) (as-> x (/ 200 x))) => 10
(and-> val forms ...)syntax

Works just like -> but will return #f in case val or any of the forms evaluates to #f.

Examples:

(define some-alist '((a . 1) (b . 2)))

(and-> 'b (assq some-alist) cdr add1) => 3

(and-> 'c (assq some-alist) cdr add1) => #f

This syntax is essentially a shortcut for certain uses of and-let*, e.g. the above example would often be expressed like this:

(and-let* ((x 'b)
           (x (assq x some-alist))
           (x (cdr x)))
  (add1 x))
(if-let (var val) then else)syntax

Equivalent to (let ((var val)) (if var then else)).

(if-let* ((x1 y1) (x2 y2) ...) then else)syntax

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.

atom valueprocedure

Returns and atom with the given initial value.

atom? xprocedure

Checks whether x is an atom.

atom-value atomprocedure

Returns the current value of atom.

atom-compare-and-set! atom old newprocedure

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.

atom-swap! atom proc #!rest argsprocedure

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.

atom-reset! atom valueprocedure

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

3
First version to be compatible with CHICKEN 5.
2
Failed attempt at CHICKEN 5 compatibiltity.
1
Failed attempt at CHICKEN 5 compatibiltity.
0.0.6
Add and->, the Schemey version of some->
0.0.5
Add support for non-parenthesized clauses to the thrushes (thanks Even Hanson). Add as-> (thanks Jan Herich).
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) 2014-2018, 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.

Contents »