Skip to content

Instantly share code, notes, and snippets.

@petitviolet
Created May 9, 2017 06:58
Show Gist options
  • Select an option

  • Save petitviolet/492ec0fb4d64b762c23a69f5e989d043 to your computer and use it in GitHub Desktop.

Select an option

Save petitviolet/492ec0fb4d64b762c23a69f5e989d043 to your computer and use it in GitHub Desktop.
pipeline operation in Scala
class Conf(private var host: String, private var port: Int) {
 def setHost(host: String): Unit = this.host = host
 def setPort(port: Int): Unit = this.port = port
 override def toString: String = s"Conf($host, $port)"
}
> "hoge" |> { _.size } |> { i => 1 to i } |> { _.mkString(",") }
res: String = 1,2,3,4
> conf |>> { c => c.setPort(8080) } |>> { _.setHost("127.0.0.1") }
res: Conf = Conf(127.0.0.1, 8080)
implicit class PipeOps[A](val a: A) extends AnyVal {
def |>[B](f: A => B): B = f(a)
def |>>(f: A => Unit): A = { f(a); a }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment