Skip to content

Instantly share code, notes, and snippets.

@halcat0x15a
Last active December 15, 2015 04:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save halcat0x15a/5202982 to your computer and use it in GitHub Desktop.
Save halcat0x15a/5202982 to your computer and use it in GitHub Desktop.

!SLIDE

Stream Processing Libraries!

@halcat0x15a

!SLIDE

Iteratee

現在Scalaで広く使われている

  • Play Framework
  • Slick
  • Akka
  • Scalaz

!SLIDE

利点

  • メモリ効率
  • データの供給、処理、変換の分離

!SLIDE

欠点

書くのがめんどい

!SLIDE

New Streaming IO

  • Haskell
    • Machines
    • Conduit
  • Scala
    • scala-machines
    • scala-conduit
    • scalaz.stream

!SLIDE

共通点

モナドとプリミティブ

for {
  input <- await[String] // 読み込み
  _ <- emit(input) // 書き込み
} yield ()

!SLIDE

scala-machines

副作用が完全に分離されている

  • Plan
  • Driver
  • Procedure

!SLIDE

Plan

def append[A: Semigroup] = for {
  e1 <- await[A]
  e2 <- await[A]
  _ <- emit(e1 |+| e2)
} yield ()

!SLIDE

Driver

def lines = new Driver[IO, String => Any] {
  val M = Monad[IO]
  def apply(k: String => Any) = readLn map k map Some.apply
}

!SLIDE

Procedure

new Procedure[IO, String] {
  type K = String => Any
  def machine = append[String].compile
  def withDriver[R](f: Driver[IO, K] => IO[R]) = f(lines)
} foreach putStrLn

!SLIDE

変換

def count = for {
  s <- await[String]
  _ <- emit(s.size)
} yield ()

def show = for {
  i <- await[Int]
  _ <- emit(i.shows)
} yield ()

def machine = count.compile andThen show.compile

!SLIDE

TwitterのUser Streams

Gist

!SLIDE

雑感

  • Iterateeより書きやすい(?)
  • DriverとProcedureが大きくなる
  • Planを構築するときに副作用が書けない
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment