Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@nulldatamap
Last active February 15, 2018 03:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nulldatamap/f4edec50578824b4158f6ec6ace2bc37 to your computer and use it in GitHub Desktop.
Save nulldatamap/f4edec50578824b4158f6ec6ace2bc37 to your computer and use it in GitHub Desktop.
Threading macros for Scheme
(define-syntax ->
(syntax-rules ()
((_ val)
val)
((_ val (f args ...) rest ...)
(-> (f val args ...)
rest ...))
((_ val f rest ...)
(-> (f val)
rest ...))))
(define-syntax ->>
(syntax-rules ()
((_ val)
val)
((_ val (f args ...) rest ...)
(->> (f args ... val)
rest ...))
((_ val f rest ...)
(->> (f val)
rest ...))))
(define-syntax as->
(syntax-rules ()
((_ val name)
val)
((_ val name (f args ...) rest ...)
(let ((name val))
(as-> (f args ...) name
rest ...)))
((_ val name f rest ...)
(as-> val name (f)
rest ...))))
(define-syntax if->
(syntax-rules ()
((_ val (then args ...) else)
(let ((x val))
(if x
(then x args ...)
else)))))
(define-syntax if->>
(syntax-rules ()
((_ val (then args ...) else)
(let ((x val))
(if x
(then args ... x)
else)))))
(define-syntax if->*
(syntax-rules ()
((_ (then args ...) else val)
(let ((x val))
(if x
(then x args ...)
else)))))
(define-syntax if->>*
(syntax-rules ()
((_ (then args ...) else val)
(let ((x val))
(if x
(then args ... x)
else)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment