Is it possible to have piping in Racket with output of one function going to next. For example, can following code be rewritten:
(define (safestr sentstr)
(list->string
(remove*
(list #\| #\; #\: #\/ #\\ #\' #\")
(string->list sentstr) )))
(define (safestr sentstr)
sentstr |
(string->list .) |
(remove* (list #\: #\;) .) |
(list->string .) )
Where "." indicates output of previous statement.
This also shows normal direction of flow and progress rather than reverse flow.
Racket being language to create languages should be able to do this. I checked here https://docs.racket-lang.org/reference/pipeports.html but could not find how to do this.
Thanks for your comments/answers.
Yes, actually, though the syntax is rather different from what you have listed. The threading package implements precisely this sort of thing, borrowed from Clojure’s threading macros. Using threading, your function would look like this:
(require threading)
(define (safestr sentstr)
(~>> sentstr
string->list
(remove* (list #\| #\; #\: #\/ #\\ #\' #\"))
list->string))
Take a look at the documentation for ~>> for more information.
You could also use λ~>> to eliminate the need to name the argument entirely:
(require threading)
(define safestr
(λ~>> string->list
(remove* (list #\| #\; #\: #\/ #\\ #\' #\"))
list->string))
There’s also the point-free package, which implements similar functionality using higher-order functions rather than macros. When paired with a lambda shorthand package like curly-fn, the result ends up looking quite similar to the versions using threading without needing to use macros:
#lang curly-fn racket/base
(require point-free)
(define safestr
(λ~> string->list
#{remove* (list #\| #\; #\: #\/ #\\ #\' #\")}
list->string))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With