Upload
tomoharu-asami
View
3.597
Download
0
Embed Size (px)
Citation preview
scalaz-stream!Functional Reactie Programming2015818 Everforth
1985() UNIX/OSWeb
20019 Java, XML, UML
2005420073
() BusinessPlace () Everforth CTO
OSS SmartDoc Relaxer
UML(BP) () Relaxer Java/XMLWeb() Scala(Softbank Creative)
ApparelCloud
http://www.apparel-cloud.com/
ApparelCloud
Hask()
Curry-Haward
Scalaz https://github.com/scalaz/scalaz : Scalaz: Type Classes and Pure Functional Data
Structures for Scala : An extension to the core Scala library for functional
programming. http://typelevel.org
Scala HaskellScala
Pipeline(1)
def pipeline(x: Int) = h(g(f(x))
def pipeline(x: Option[Int]) = x.map(f).map(g).map(h)
def pipeline(x: Int) = x |> f |> g |> h
Functor
Monad
def pipeline(x: Option[Int]) = x.flatMap(f).flatMap(g).flatMap(h)
def pipeline(x: Option[Int]) = for (a
Pipeline(2)
val pipeline = State(f).flatMap(x => State(g(x))).flatMap(x => State(h(x))) pipeline.run(10)
val pipeline = for (a
State
State Monad
FuncionState Function Function
State
Value
Value Value Value
Value Value
Task scalaz.concurrent.Task TryFuture Scalaz
Future (run)
val t: Task[Int] = Task(f(1000)) val i: Int = t.run
val i: Int = Task(f(1000)).run
scalaz-stream
The Reactive Manifest http://www.reactivemanifesto.org/ http://okapies.hateblo.jp/entry/2014/12/03/025921
Responsive
Resilient
Elastic
Message Driven
: Reactive Streams http://www.reactive-streams.org/
Reactive
Service Cluster
CPU Core
Function
Service
Message
Message
Message
Function
Function
Function
Function
Function
Service
Service
Service
Service
Function
Reactive
Concurrent
Parallel
DistributedResponsive
Resillience
Elasticity
Message Driven
Concurrent
Parallel
Distributed
Functional Reactive Programming /
Callback hell
Functional Reactive Programming RxJava Scala https://github.com/ReactiveX/RxJava Observable
Scalaz Stream https://github.com/scalaz/scalaz-stream Process
Akka Streams https://typesafe.com/blog/typesafe-announces-akka-
streams Akka actor
scalaz-stream ScalazStreaming https://github.com/scalaz/scalaz-stream
Process Pull (backpressure)
Process
Function Function ProcessMonadProcessMonad
Channel Sink
Channel Sink
Process Monad
() Process Process
Process1 Process
Sink Process
Channel Process
Tee 2(interleave, zipping)Process
Wye 2Process
Writer 2Process
() Exchange
Queue Process
Topic pub/sub Process Process
scalaz-stream/ Scalaz
COMPARING AKKA-STREAM AND SCALAZ-STREAM WITH CODE EXAMPLES https://softwaremill.com/comparing-akka-stream-scalaz-stream
()
Topic EIPContent-Based Routing
scalaz-stream
val source: Process[Task, String] = io.linesR("infile.txt") val a: Process[Task, String] = source.filter(_.nonEmpty) val b: Process[Task, String] = a.map(x => s"${x.length}:$x") val c: Process[Task, ByteVector] = b.pipe(text.utf8Encode) val sink: Sink[Task, ByteVector] = io.fileChunkW("outfile.txt") val pipeline: Process[Task, Unit] = b.to(sink) val task: Task[Unit] = pipeline.run val result: Unit = task.run
io.linesR("infile.txt").filter(_.nonEmpty).map(x => s"${x.length}:$x"). pipe(text.utf8Encode).to(io.fileChunkW("outfile.txt")).run.run
3 start
: [scalaz-stream] http://modegramming.blogspot.jp/2015/03/scalaz-stream.html
case class Packet(seqno: Int, end: Boolean, content: String)
def sink: Sink[Task, Packet] = { io.channel((a: Packet) => Task.delay(println(a))) }
: [scalaz-stream] http://modegramming.blogspot.jp/2015/04/scalaz-stream.html
: [scalaz-stream] Scala/OOP http://modegramming.blogspot.jp/2015/03/scalaoop.html
: [scalaz-stream] Scala/FP http://modegramming.blogspot.jp/2015/03/scalafp.html
def buildTextToPacket[M[_]: Monad](source: Process[M, String]): Process[M, Packet] = { source. chunk(3). // 3 pipe(zipWithNext). // pipe(zipWithIndex). // map(toPacket) } def toPacket(x: ((Vector[String], Option[Vector[String]]), Int)): Packet = { val ((current, next), index) = x val content = current.mkString("-") val isend = next.cata(_.headOption === Some("start"), true) Packet(index + 1, isend, content) }
- 3- - start
val source = io.linesR("data.txt") val pipeline = buildTextToPacket(source).to(sink) pipeline.run.run
Packet(1,false,1-2-3) Packet(2,true,4-5-6) Packet(3,true,start-8-9)
1 2 3 4 5 6 start 8 9
chunk zipWithNextzipWithIndex toPacketChannel Sink
Process Monad
EventProcessor
val queue = EventProcessor.q val source = Vector("1", "2", "3", "4", "5", "6", "start", "8", "9") source foreach { x => queue.enqueueOne(x).run Thread.sleep(2000) }
object EventProcessor { val q = async.unboundedQueue[String] val eventStream: Process[Task, String] = q.dequeue }
: [scalaz-stream] http://modegramming.blogspot.jp/2015/04/scalaz-stream.html
val source = EventProcessor.eventStream val pipeline = buildTextToPacket(source).to(sink) pipeline.run.run
Packet(1,false,1-2-3) Packet(2,true,4-5-6)
chunk zipWithNextzipWithIndex toPacketChannel Sink
Process Monad
Reactive Responsive, Elastice, Resilience, Message-Driven Cuncurrent, Parallel, Distributed
/ /// vs
scalaz stream /
Push
END