Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Clojure threading in scheme.

31 views
Skip to first unread message

Alain De Vos

unread,
Nov 13, 2021, 12:20:30 PM11/13/21
to
Clojure has a syntax for left to right evaluation and pipeline.
https://clojure.org/guides/threading_macros
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

unread,
Nov 13, 2021, 12:57:41 PM11/13/21
to
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 ...))])
(with-syntax
([out
(let loop ([first (car reversed)]
[rest (cdr reversed)])
(if (null? rest)
first
(syntax-case first ()
[(func arg0 ...)
(append #'(func arg0 ...)
(list (loop (car rest) (cdr rest))))])))])
#'out))])))

You can also do it with syntax-rules:

(define-syntax ->
(syntax-rules ()
[(-> exp)
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
anything.
0 new messages