Skip to content

Instantly share code, notes, and snippets.

@kwrooijen
Last active April 1, 2023 00:52
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kwrooijen/7e8c4720046a8eb18f6c to your computer and use it in GitHub Desktop.
Save kwrooijen/7e8c4720046a8eb18f6c to your computer and use it in GitHub Desktop.
Clojure's threading macro in scheme
(define-syntax ->
(syntax-rules ()
([_ value]
value)
([_ value snd rest ...]
(cond
[(list? 'snd)
(let ([f (primitive-eval (car 'snd))]
[args (cons value (cdr 'snd))])
(-> (apply f args) rest ...))]
[(procedure? snd)
(-> (snd value) rest ...)]))))
(define-syntax -»
(syntax-rules ()
([_ value]
value)
([_ value snd rest ...]
(cond
[(list? 'snd)
(let ([f (primitive-eval (car 'snd))]
[args (append (cdr 'snd) (list value))])
(-» (apply f args) rest ...))]
[(procedure? snd)
(-» (snd value) rest ...)]))))
;;=> 2
(-> 1 1+ (+ 1 1) (/ 2))
;;=> 0.5
(-» 1 1+ (+ 1 1) (/ 2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment