Skip to content

Instantly share code, notes, and snippets.

@dagoof
Created July 16, 2011 05:58
Show Gist options
  • Save dagoof/1086059 to your computer and use it in GitHub Desktop.
Save dagoof/1086059 to your computer and use it in GitHub Desktop.
Scheme expression threading (->>)
; 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