我正在寻找使用Scala构建管道模式.我希望在编写管道对象后,它们可以像这样连接在一起:
Pipeline1 :: Pipeline2 :: Pipeline3 ...
Run Code Online (Sandbox Code Playgroud)
到目前为止,我已经尝试了一些想法.有些工作,有些则没有.但它们似乎都没有完全摆脱样板代码.以下是我最接近的.
首先定义Pipeline和Source抽象类:
// I is the input type and O is the output type of the pipeline
abstract class Pipeline[I, +O](p: Pipeline[_, _ <: I]) {
val source = p
val name: String
def produce(): O
def stats():String
}
abstract class Source[+T] extends Pipeline[AnyRef, T](null)
Run Code Online (Sandbox Code Playgroud)
接下来,我创建了两个管道并尝试将它们链接在一起
// this creates a random integer
class RandomInteger extends Source[Int] {
override val name = "randInt"
def produce() = {
scala.Math.round(scala.Math.random.asInstanceOf[Float] * 10)
}
def stats()="this pipeline …Run Code Online (Sandbox Code Playgroud)