Skip to content

Instantly share code, notes, and snippets.

@rerorero
Last active August 29, 2015 14:15
Show Gist options
  • Save rerorero/001022c891801dc13d4b to your computer and use it in GitHub Desktop.
Save rerorero/001022c891801dc13d4b to your computer and use it in GitHub Desktop.
List[ReaderT]をfoldしたい
package test
import scalaz._
import scala.util._
import scala.concurrent._
import scala.concurrent.ExecutionContext.Implicits.global
object main extends App {
type Input = String
type FReader[T] = ReaderT[Future, Input, T]
object FReader {
def apply[T](f: Input => Future[T]) : FReader[T] = Kleisli(f)
}
implicit val futureM = new Monad[Future] {
def point[A](a: => A) : Future[A] = Future{a}
def bind[A, B](fa: Future[A])(f: A => Future[B]) : Future[B] = fa.flatMap(f)
}
val pureAction : FReader[Unit] = FReader{ _ => Future.successful(())}
val stringAction : FReader[String] = FReader{ in => Future { println("stringAction:" + in); "fuga" } }
val integralAction : FReader[Int] = FReader{ in => Future { println("integralAction:" + in); 2525 } }
val failedAction : FReader[Int] = FReader{ in => Future { println("failedAction:" + in); throw new Exception("failed!!") } }
/*
* ↓と同じようなことをList[Reader]に対して再帰的にやりたい
val procs = pureAction flatMap { _ =>
stringAction flatMap { _ =>
integralAction flatMap { _ =>
stringAction
}
}
}
*/
val proclist : List[FReader[_]] = List(
stringAction,
integralAction,
//failedAction,
stringAction
)
val procs = proclist.foldLeft(pureAction : FReader[Any]) { (acc, elem) =>
// compile error Kleisli[M[_], A, B] が共変ではないためエラー。 ちなみに Future[+T] は共変
acc.flatMap { _ => elem }
}
procs("injections") onComplete {
case Success(v) => println("Hello world.")
case Failure(e) => println("Good bye world."+e)
}
}
package test
import scala.concurrent._
import scala.util._
import scala.concurrent.ExecutionContext.Implicits.global
object alter extends App {
type Input = String
// 共変にしてfold可能に
trait FReader[+T] extends (Input => Future[T]) { self =>
def map[U](f: T => U)(implicit executor: ExecutionContext) = FReader { self(_) map f }
def flatMap[U](f:T => FReader[U])(implicit executor: ExecutionContext) = FReader { in =>
self(in) flatMap { v => f(v)(in) }
}
}
object FReader {
def apply[T](f: Input => Future[T]) = new FReader[T] {
def apply(in:Input) = f(in)
}
}
val pureAction : FReader[Unit] = FReader{ _ => Future.successful(())}
val stringAction : FReader[String] = FReader{ in => Future { println("stringAction:" + in); "fuga" } }
val integralAction : FReader[Int] = FReader{ in => Future { println("integralAction:" + in); 2525 } }
val failedAction : FReader[Int] = FReader{ in => Future { println("failedAction:" + in); throw new Exception("failed!!") } }
val proclist : List[FReader[_]] = List(
stringAction,
integralAction,
//failedAction,
stringAction
)
val procs = proclist.foldLeft(pureAction : FReader[Any]) { (acc, elem) =>
acc.flatMap { _ => elem }
}
procs("injections") onComplete {
case Success(v) => println("Hello world."+v)
case Failure(e) => println("Good bye world."+e)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment