Last active
December 22, 2015 08:29
-
-
Save PipelineBaron/6445448 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.plasmaconduit.pipelines | |
| sealed trait Pipeline[E, A] { | |
| def map[B](f: A => A): Pipeline[E, A] = this match { | |
| case Continue(n) => Continue(f(n)) | |
| case Done(n) => Done(n) | |
| case Error(e) => Error(e) | |
| } | |
| def flatMap[B](f: A => Pipeline[E, A]): Pipeline[E, A] = this match { | |
| case Continue(n) => f(n) | |
| case Done(n) => Done(n) | |
| case Error(e) => Error(e) | |
| } | |
| } | |
| final case class Continue[E, A](subject: A) extends Pipeline[E, A] | |
| final case class Done[E, A](subject: A) extends Pipeline[E, A] | |
| final case class Error[E, A](error: E) extends Pipeline[E, A] | |
| object Pipeline { | |
| def convey[E, A](init: A, pipes: List[A => Pipeline[E, A]]): Pipeline[E, A] = { | |
| pipes.foldLeft[Pipeline[E, A]](Continue(init)) { (memo, pipe) => | |
| memo flatMap { n => pipe(n) } | |
| } | |
| } | |
| } | |
| object Test { | |
| def exclaimAndContinue[E](data: String): Pipeline[E, String] = Continue(data + "!") | |
| def generateDone[E](data: String): Pipeline[E, String] = Done(data) | |
| def generateError[E](error: E): Pipeline[E, String] = Error(error) | |
| def testEnd: Pipeline[String, String] = { | |
| for( | |
| first <- exclaimAndContinue("lol"); | |
| second <- exclaimAndContinue(first); | |
| third <- exclaimAndContinue(second); | |
| fourth <- exclaimAndContinue(third); | |
| last <- generateDone(third) | |
| ) yield last | |
| } | |
| def testDone: Pipeline[String, String] = { | |
| for( | |
| first <- exclaimAndContinue("lol"); | |
| second <- generateDone(first); | |
| third <- exclaimAndContinue(second); | |
| fourth <- exclaimAndContinue(third); | |
| last <- generateDone(third) | |
| ) yield last | |
| } | |
| def testError: Pipeline[String, String] = { | |
| for( | |
| first <- exclaimAndContinue("lol"); | |
| second <- generateError("Boom!"); | |
| third <- exclaimAndContinue(second); | |
| fourth <- exclaimAndContinue(third); | |
| last <- generateDone(third) | |
| ) yield last | |
| } | |
| def testConvey: Pipeline[String, String] = { | |
| val pipes = List[String => Pipeline[String, String]]( | |
| exclaimAndContinue, | |
| exclaimAndContinue, | |
| exclaimAndContinue, | |
| exclaimAndContinue, | |
| exclaimAndContinue, | |
| generateDone, | |
| exclaimAndContinue, | |
| exclaimAndContinue | |
| ) | |
| Pipeline.convey("lol", pipes) | |
| } | |
| def printResult(result: Pipeline[String, String]): Unit = result match { | |
| case Continue(n) => println(s"Continue: $n") | |
| case Done(n) => println(s"Done: $n") | |
| case Error(e) => println(s"Error: $e") | |
| } | |
| def main() { | |
| printResult(testEnd) | |
| printResult(testDone) | |
| printResult(testError) | |
| printResult(testConvey) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment