简体   繁体   English

在球拍中管道

[英]Piping in Racket

Is it possible to have piping in Racket with output of one function going to next. 是否可以在Racket中使用一个功能输出到下一个。 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. 我在这里查了https://docs.racket-lang.org/reference/pipeports.html但是找不到怎么做。

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. threading包正好实现了这种东西,借用了Clojure的线程宏。 Using threading , your function would look like this: 使用threading ,您的函数将如下所示:

(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. 还有一个无point-free包,它使用高阶函数而不是宏来实现类似的功能。 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: 当与像curly-fn这样的lambda速记包配对时,结果看起来非常类似于使用threading的版本而不需要使用宏:

#lang curly-fn racket/base

(require point-free)

(define safestr
  (λ~> string->list
       #{remove* (list #\| #\; #\: #\/ #\\ #\' #\")}
       list->string))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM