フリーモナドを使用してETLプロセス用の簡単な言語を実装しました。入力と出力の両方にデータの取り出しと格納のためにList
を使用すると、すべて正常に動作します。予想通りしかし、私はFuture[List]
フリーモナドの使用方法[M [_]]
case class Fetch(offset: Int, amount: Int) extends Ops[Future[List[Record]]]
case class Store(recs: List[Record]) extends Ops[Future[List[Response]]]
def fetch(offset: Int, amount: Int): OpsF[Future[List[Record]]] =
liftF[Ops, Future[List[Record]]](Fetch(offset, amount))
def store(recs: List[Record]): OpsF[Future[List[Response]]] =
liftF[Ops, Future[List[Response]]](Store(recs))
// explicit types in case I am misunderstanding more than I think
def simpleEtl(offset: Int, amount: Int): Free[Ops, Future[List[Response]]] =
fetch(offset, amount).flatMap { rf: Future[List[Record]] =>
val getResponses: OpsF[Future[List[Response]]] = rf map { r: List[Record] =>
store(r)
}
getResponses
}
で作業していないList
case class Fetch(offset: Int, amount: Int) extends Ops[List[Record]]
case class Store(recs: List[Record]) extends Ops[List[Response]]
def fetch(offset: Int, amount: Int): OpsF[List[Record]] =
liftF[Ops, List[Record]](Fetch(offset, amount))
def store(recs: List[Record]): OpsF[List[Response]] =
liftF[Ops, List[Response]](Store(recs))
def simpleEtl(offset: Int, amount: Int): Free[Ops, List[Response]] =
fetch(offset, amount).flatMap(r => store(r))
で作業を非同期ライブラリとFuture[List]
共通の輸入と仕事と定義
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
import cats.free.Free
import cats.free.Free._
sealed trait Ops[A]
type OpsF[A] = Free[Ops, A]
を使用しています、返された型flatMap
/map
は間違っている - 私はOpsF[Future]
を得ていないのですが、Future[OpsF]
Error:(34, 60) type mismatch;
found : scala.concurrent.Future[OpsF[scala.concurrent.Future[List[Response]]]]
(which expands to) scala.concurrent.Future[cats.free.Free[Ops,scala.concurrent.Future[List[String]]]]
required: OpsF[scala.concurrent.Future[List[Response]]]
(which expands to) cats.free.Free[Ops,scala.concurrent.Future[List[String]]]
val getResponses: OpsF[Future[List[Response]]] = rf map { r: List[Record] =>
私の現在の回避策はstore
がFuture[List[Record]]
を受け入れ、Future
上インタプリタマップをさせることですが、それは不器用な感じ。
この問題はList
に固有の問題ではありません。 Option
も有用であろう。
私は間違っていますか?これにはモナド変圧器がありますか?
これは、モナド変圧器の典型的なパターンのようですハスケルは何とか 'FreeT'を持っていますが、スカラズやネコでは見つけられませんでした。 –
scalazは[7.2.0]から 'FreeT'を持っています(https://oss.sonatype.org/service/local/repositories/releases/archive/org/scalaz/scalaz_2.11/7.2.0/scalaz_2.11-7.2 .0-javadoc.jar /!/ index.html#scalaz.FreeT)。 –
すぐにtypelevelインキュベーターになるhttp://47deg.github.io/fetch/という47度の図書館に向けることができますか?心配してください、私は47度で働いていませんが、これはすでにあなたがやりたいことの多くのための解決策を持っているようです。 – wheaties