Skip to content

Instantly share code, notes, and snippets.

@tizoc
Created May 2, 2012 03:43
Show Gist options
  • Save tizoc/2573417 to your computer and use it in GitHub Desktop.
Save tizoc/2573417 to your computer and use it in GitHub Desktop.
clojure's -> and ->> for chibi scheme
(import (scheme) (clojure pipeline) (chibi test))
(test
"(-> 3.0 (- 2) (* 5) sqrt)"
(sqrt (* (- 3.0 2) 5))
(-> 3.0 (- 2) (* 5) sqrt))
(test
"(->> 3.0 (- 5) (* 5) sqrt)"
(sqrt (* 5 (- 5 3.0)))
(->> 3.0 (- 5) (* 5) sqrt))
(test
"(doto (make-vector 3) (vector-set! 0 0) (vector-set! 1 1) (vector-set! 2 2))"
(vector 0 1 2)
(doto (make-vector 3) (vector-set! 0 0) (vector-set! 1 1) (vector-set! 2 2)))
(define-library (clojure pipeline)
(export -> ->> doto)
(import (scheme))
(begin
(define-syntax ->
(syntax-rules ()
((_ ?form) ?form)
((_ ?form (?f ?arg ...)) (?f ?form ?arg ...))
((_ ?form ?f) (?f ?form))
((_ ?form ?form2 . ?forms) (-> (-> ?form ?form2) . ?forms))))
(define-syntax ->>
(syntax-rules ()
((_ ?form) ?form)
((_ ?form (?f ?arg ...)) (?f ?arg ... ?form))
((_ ?form ?f) (?f ?form))
((_ ?form ?form2 . ?forms) (->> (->> ?form ?form2) . ?forms))))
(define-syntax doto
(syntax-rules ()
((_ ?form) ?form)
((_ ?form (?f ?arg ...)) (?f ?form ?arg ...))
((_ ?form ?f) (?f ?form))
((_ ?form ?form2 . ?forms)
(let ((value ?form))
(doto value ?form2)
(doto value . ?forms)
value))))
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment