Skip to content

Instantly share code, notes, and snippets.

@drostron
Last active November 4, 2020 04:11
Show Gist options
  • Save drostron/11304626 to your computer and use it in GitHub Desktop.
Save drostron/11304626 to your computer and use it in GitHub Desktop.
import scala.concurrent._, ExecutionContext.Implicits.global
import scala.async.Async.{async, await}
object AsyncExploration {
// hmm, monad transformers are cool and all but for this simple case async might be the winner
val α =
async {
for {
i ← await(Future(3 :: 7 :: Nil))
j ← 5 :: 11 :: Nil
}
yield i + j
}
}
libraryDependencies += "org.scalaz" %% "scalaz-core" % "7.0.5"
libraryDependencies += "org.typelevel" %% "scalaz-contrib-210" % "0.1.4"
libraryDependencies += "org.scala-lang.modules" %% "scala-async" % "0.9.1"
libraryDependencies += "org.pelotom" %% "effectful" % "1.0.0"
import effectful._
import scala.concurrent._, ExecutionContext.Implicits.global
import scala.language.postfixOps
import scalaz._, Scalaz._, contrib.std.futureInstance
object EffectfulExploration {
// async looks promissing, but how bout the proposedly even more powerful effectful?
// looks quite similar for this simple case; will have to take a deeper effectful dive soon
val α =
effectfully {
for {
i ← unwrap(future(3 :: 7 :: Nil)) // postfix ! op without either a . or ; causes compile error, gah
j ← 5 :: 11 :: Nil
}
yield i + j
}
}
import scala.concurrent._, ExecutionContext.Implicits.global
import scalaz._, Scalaz._, contrib.std.futureInstance
object MonadTransformerExploration {
/* I'd like to do the following but am unable to convince the compiler to read my mind.
*
* val α =
* for {
* i ← Future(3 :: 7 :: Nil)
* j ← 5 :: 11 :: Nil
* k ← i + j
* }
* yield k
*
* it tells me with:
* found : List[Int]
* required: scala.concurrent.Future[?]
*/
// how bout monad transformers, say what?
type ListTFuture[T] = ListT[Future, T]
val β = {
for {
i ← ListT(Future(3 :: 7 :: Nil))
j ← 5 :: 11 :: ListT.empty[Future, Int]
k = i + j
}
yield k
}.underlying
trait ListTSugar[T[+_], U] { def listT: ListT[T, U] }
implicit class SweetFutureList[T](v: Future[List[T]]) extends ListTSugar[Future, T] {
def listT = ListT(v)
}
implicit class SweetList[T](v: List[T]) extends ListTSugar[Future, T] {
def listT = Future(v.toList).listT
}
val χ = {
for {
i ← Future(3 :: 7 :: Nil).listT
j ← List(5, 11).listT
k = i + j
}
yield k
}.underlying
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment