Last active
April 1, 2023 00:52
-
-
Save kwrooijen/7e8c4720046a8eb18f6c to your computer and use it in GitHub Desktop.
Clojure's threading macro in scheme
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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