Clojure threading in scheme.

Skip to first unread message

Alain De Vos

Nov 13, 2021, 12:20:30 PM11/13/21
Clojure has a syntax for left to right evaluation and pipeline.
Does there exist a similar function in scheme ?
Or a similar function in scheme with a name be coded in a few lines of code.
I think you apply on the expression left and that output is then the input of
the expression on the right etc...
It allows to read nested expressions from left to right which is more like the natural language.

I have found rackjure but this is only an implementation of racket.
It would be nice to do something similar in chez-scheme

Chris Vine

Nov 13, 2021, 12:57:41 PM11/13/21
I don't know clojure but it is relatively simple to make your own
pipeline macro, if that is what you are after. Here's one approach:

(define-syntax ->
(lambda (x)
(syntax-case x ()
[(_ exp0 exp1 ...)
(let ([reversed (reverse #'(exp0 exp1 ...))])
(let loop ([first (car reversed)]
[rest (cdr reversed)])
(if (null? rest)
(syntax-case first ()
[(func arg0 ...)
(append #'(func arg0 ...)
(list (loop (car rest) (cdr rest))))])))])

You can also do it with syntax-rules:

(define-syntax ->
(syntax-rules ()
[(-> exp)
[(-> exp ... (op args ...))
(op args ... (-> exp ...))]))

Expressions are evaluated left to right, rather like ML's |>
operator. So usage of these is thus:

(-> "hello"
(format #t "~A~%"))

In that example the pipeline expression itself evaluated to
undefined (the return value of format), but it could evaluate to
Reply all
Reply to author
0 new messages