scala - How to use sequence from scalaz to transform T[G[A]] to G[T[A]] -
scala - How to use sequence from scalaz to transform T[G[A]] to G[T[A]] -
i have code transform list[future[int]] future[list[int]] using scalaz sequence.
import scalaz.concurrent.future val t = list(future.now(1), future.now(2), future.now(3)) //list[future[int]] val r = t.sequence //future[list[int]] because using future scalaz, may have implicit resolution magic me, wonder if type class custom class not predefined 1 future, how can define implicit resolution accomplish same result
case class foo(x: int) val t = list(foo(1), foo(2), foo(3)) //list[foo[int]] val r = t.sequence //foo[list[int]] many in advance
you need create applicative[foo] (or monad[foo]) in implicit scope.
what asking won't work, since foo not universally quantified (so expected type r doesn't create sense foo[list[int]], since foo doesn't take type parameter.
lets define foo differently:
case class foo[a](a: a) object foo { implicit val fooapplicative = new applicative[foo] { override def point[a](a: => a) = foo(a) override def ap[a,b](fa: => foo[a])(f: => foo[a=>b]): foo[b] = foo(f.a(fa.a)) } } now if create sure implicit in scope, can sequence:
scala> val t = list(foo(1), foo(2), foo(3)) t: list[foo[int]] = list(foo(1), foo(2), foo(3)) scala> t.sequence res0: foo[list[int]] = foo(list(1, 2, 3)) scala scalaz scalaz7
Comments
Post a Comment