Created
July 16, 2011 05:58
-
-
Save dagoof/1086059 to your computer and use it in GitHub Desktop.
Scheme expression threading (->>)
This file contains 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
; Scheme implementation of 'expression threading'. | |
; chains the given argument through each expression; such as | |
; (->> 5 | |
; (+ 3) | |
; (* 2)) === 16 | |
; | |
; or even better: | |
; | |
; (->> | |
; "lets find some even chars!" | |
; string->list | |
; (map char->integer) | |
; (filter even?) | |
; (map integer->char) | |
; list->string) === "lt fnd vn hr" | |
(load "~~/lib/syntax-case") | |
(define (curry f . c) (lambda x (apply f (append c x)))) | |
(define-syntax expr-or-func | |
(syntax-rules | |
() | |
((_ (f default-arg ...)) | |
(curry f default-arg ...)) | |
((_ f) | |
f))) | |
(define-syntax ->> | |
(syntax-rules | |
() | |
((_ s expr ...) | |
(let loop | |
((current s) | |
(rest (list (expr-or-func expr) ...))) | |
(if (null? rest) | |
current | |
(loop ((car rest) current) (cdr rest))))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment