Created
January 13, 2017 21:16
-
-
Save jto/b3e493c9586ff4f59a0785ad546cb798 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
def run = { | |
import com.mfglabs.precepte._, default._, Macros.callee, corescalaz._ | |
import scala.concurrent.ExecutionContext.Implicits.global | |
import scala.concurrent.Future | |
import default._ | |
import scalaz.std.scalaFuture._ | |
type Pre[A] = DefaultPre[Future, Unit, A] | |
type ST = default.ST[Unit] | |
type SF[T] = (ST, Future[T]) | |
def tags(n: String) = BaseTags(com.mfglabs.precepte.default.Callee(n), Category.Database) | |
val env = BaseEnv(Host("localhost"), Environment.Test, Version("1.0")) | |
def nostate = ST(Span.gen, env, Vector.empty, ()) | |
implicit val unitSG = new scalaz.Semigroup[Unit] { | |
def append(f1: Unit, f2: => Unit) = () | |
} | |
// val f1 = Precepte(tags("f1"))((_: ST) => Future.successful("f1")) | |
// val f2 = Precepte(tags("f2"))((_: ST) => Future.successful("f2")) | |
val f = Precepte(tags("f")) { | |
Precepte.liftF(Future.successful("f1")): Pre[String] | |
} | |
def logcalls = new (SF ~~> Future) { | |
def apply[A](sf: SF[A]): Future[A] = { | |
val callee = sf._1.managed.path.map(_.tags.callee.value).last | |
println(callee) | |
sf._2 | |
} | |
} | |
f.mapSuspension(logcalls).eval(nostate) | |
} | |
run | |
*/ | |
/// -- | |
/* | |
import scalaz._, Scalaz._ | |
type Step0[F[_], S, A] = S => (S, F[A]) | |
type Precepte[F[_], S, A] = Free[({ type L[A] = Step0[F, S, A] })#L, A] | |
type UnmanagedState = List[String] | |
type Pre[A] = Precepte[Id, UnmanagedState, A] | |
object Pre { | |
type St[A] = Step0[Id, UnmanagedState, A] | |
def apply[A](tag: String)(a: A): Pre[A] = { | |
val p: Pre[A] = Free.pure[({ type L[A] = Step0[Id, UnmanagedState, A] })#L, A](a) | |
substep(tag)(p) | |
} | |
def pure[A](a: A): Pre[A] = | |
Free.pure[({ type L[A] = Step0[Id, UnmanagedState, A] })#L, A](a) | |
def liftF[A](tag: String)(fa: Id[A]): Pre[A] = { | |
val p = Free.liftF[({ type L[A] = Step0[Id, UnmanagedState, A] })#L, A](s => s -> fa) | |
substep(tag)(p) | |
} | |
def substep[A](tag: String)(p: Pre[A]): Pre[A] = { | |
Free[St, A](s => (s :+ tag, p)) | |
} | |
} | |
val p1: Pre[Int] = Pre("p1")(1) | |
val p2: Pre[Int] = Pre("p2")(2) | |
val p3: Pre[Int] = Pre.substep("p3")(p1.flatMap(r1 => p2.map(r2 => r1 + r2))) | |
type Out0[A] = (S, F[A]) | |
type Out[A] = S => Out0[A] | |
implicit def mo[F[_]: Monad, S] = { | |
new Monad[Out] { | |
def point[A](a: => A): Out[A] = s => (s, a.point[F]) | |
def bind[A, B](oa: Out[A])(f: A => Out[B]): Out[B] = { s0 => | |
val (s, fa) = oa(s0) | |
??? | |
} | |
} | |
} | |
def run(st: UnmanagedState) = new (Out ~> Out0) { | |
def apply[A](fa: Out[A]) = fa(st) | |
} | |
val nostate: UnmanagedState = Nil | |
p3.foldMap(run(nostate)) // (List("p3", "p1", "p2"), 3) | |
*/ | |
/* | |
def freeAp[F[_]: Applicative, S] = { | |
type St[A] = Step0[F, S, A] | |
type P[A] = Precepte[F, S, A] | |
new Applicative[P] { | |
def point[A](a: => A): P[A] = Free.pure[St, A](a) | |
def ap[A, B](pa: => P[A])(p: => P[A => B]): P[B] = Free[St, B] { s => | |
val (sa, fa) = pa.foldMap(run(s)) | |
// val (sf, ff) = p.foldMap(run(s)) | |
// sa -> (ff |@| fa)(_(_)) | |
??? | |
} | |
} | |
} | |
*/ | |
// | |
object Test { | |
import scalaz._, Scalaz._ | |
type Step0[F[_], S, A] = S => F[(S, A)] | |
type Precepte[F[_], S, A] = Free[({ type L[A] = Step0[F, S, A] })#L, A] | |
type UnmanagedState = List[String] | |
trait PreBuilder[F[_]]{ | |
type Pre[A] = Precepte[F, UnmanagedState, A] | |
type St[A] = Step0[F, UnmanagedState, A] | |
def apply[A](tag: String)(a: A)(implicit ap: Applicative[F]): Pre[A] = { | |
val p = Free[St, A] { s => (s, pure(a)).point[F] } | |
substep(tag)(p) | |
} | |
def suspend[A](tag: String)(fa: F[A])(implicit ap: Functor[F]): Pre[A] = { | |
val p = Free.liftF[St, A]{ s => fa.map(s -> _) } | |
substep(tag)(p) | |
} | |
def substep[A](tag: String)(p: Pre[A]): Pre[A] = { | |
val go = new (St ~> St) { | |
def apply[A](st: St[A]) = s => st(s :+ tag) | |
} | |
p.mapFirstSuspension(go) | |
} | |
def pure[A](a: A): Pre[A] = | |
Free.pure[St, A](a) | |
} | |
object Precepte { | |
def apply[F[_]] = new PreBuilder[F]{} | |
} | |
def run[F[_]] = | |
new (F ~> F) { | |
def apply[A](fa: F[A]) = fa | |
} | |
implicit def mo[F[_]: Monad, S] = | |
new Monad[({ type λ[α] = Step0[F, S, α] })#λ] { | |
def point[A](a: => A) = s => (s, a).point[F] | |
def bind[A, B](oa: Step0[F, S, A])(f: A => Step0[F, S, B]) = | |
s0 => oa(s0).flatMap { case (s1, a) => f(a)(s1) } | |
} | |
// def freeAp[F[_]: Applicative, S] = { | |
// type St[A] = Step0[F, S, A] | |
// type P[A] = Precepte[F, S, A] | |
// new Applicative[P] { | |
// def point[A](a: => A): P[A] = Free.pure[St, A](a) | |
// def ap[A, B](pa: => P[A])(p: => P[A => B]): P[B] = Free[St, B] { s => | |
// } | |
// } | |
// } | |
import akka.pattern.after | |
import scala.concurrent.ExecutionContext.Implicits.global | |
import scala.concurrent.Future | |
import scala.concurrent.duration._ | |
import akka.actor.ActorSystem | |
val system = ActorSystem("theSystem") | |
def f1 = after(duration = 10 second, using = system.scheduler)({println(System.currentTimeMillis); 1}.point[Future]) | |
def f2 = after(duration = 10 second, using = system.scheduler)({println(System.currentTimeMillis); 2}.point[Future]) | |
def go[F[_]: Monad](f1: F[Int], f2: F[Int]): UnmanagedState => F[(UnmanagedState, (Int, Int))] = { | |
import scalaz.syntax.applicative._ | |
type Pre[A] = Precepte[F, UnmanagedState, A] | |
val p1: Pre[Int] = Precepte[F].suspend("p1")(f1) | |
val p2: Pre[Int] = Precepte[F].suspend("p2")(f2) | |
val p3: Pre[Int] = Precepte[F].substep("p3")(p1.flatMap(r1 => p2.map(r2 => r1 + r2))) | |
val nostate: UnmanagedState = Nil | |
type S0[α] = Step0[F, UnmanagedState, α] | |
implicit val freeMonad = Free.freeMonad[S0] | |
// p3.foldMap(run[S0]) // (List("p3", "p1", "p2"), 3) | |
(p1 |@| p2).tupled.foldMap(run[S0]) | |
} | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> import scalaz._, Scalaz._ | |
import scalaz._ | |
import Scalaz._ | |
scala> implicit val ex = scala.concurrent.ExecutionContext.global | |
ex: scala.concurrent.ExecutionContextExecutor = scala.concurrent.impl.ExecutionContextImpl@2d934fae | |
scala> Test.go(Test.f1, Test.f2) | |
res3: Test.UnmanagedState => scala.concurrent.Future[(Test.UnmanagedState, (Int, Int))] = Test$$anon$1$$Lambda$1802/448225936@7afe5475 | |
scala> res3(Nil) | |
res4: scala.concurrent.Future[(Test.UnmanagedState, (Int, Int))] = Future(<not completed>) | |
scala> 1484342169252 | |
1484342169252 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment