Skip to content

Instantly share code, notes, and snippets.

@bpholt
Last active January 12, 2018 00:01
Show Gist options
  • Save bpholt/d003e9a24eeca2c963f58e9bba5bbed5 to your computer and use it in GitHub Desktop.
Save bpholt/d003e9a24eeca2c963f58e9bba5bbed5 to your computer and use it in GitHub Desktop.
Free Monad Stream
name := "example"
scalaVersion := "2.12.4"
scalacOptions ++= Seq(
"-feature",
"-deprecation",
"-Ypartial-unification",
// "-Xlog-implicits",
)
libraryDependencies ++= {
val fs2Version = "0.10.0-RC1"
val catsVersion = "1.0.1"
Seq(
"org.typelevel" %% "cats-free" % catsVersion,
"co.fs2" %% "fs2-core" % fs2Version,
)
}
// Exiting paste mode, now interpreting.
<pastie>:39: error: type mismatch;
found : fs2.Stream[F,MetricAlarm]
required: FreeCloudWatch.this.StreamF[A]
(which expands to) fs2.Stream[F,A]
case DescribeAlarms() ⇒ Stream.emit(MetricAlarm("my-alarm")).covary[F]
// cats 1.0.1, fs2 0.10.0-RC1
import cats._
import cats.data._
import cats.implicits._
import cats.effect.Effect
import cats.free.Free
import cats.free.Free.liftF
import fs2._
import scala.language.higherKinds
case class MetricAlarm(alarmName: String)
class FreeCloudWatch[F[_] : Effect] {
sealed trait CloudWatchA[A]
case class DescribeAlarms() extends CloudWatchA[StreamF[MetricAlarm]]
case class DeleteAlarm(alarmNames: StreamF[MetricAlarm]) extends CloudWatchA[Unit]
type CloudWatch[A] = Free[CloudWatchA, A]
type StreamF[A] = Stream[F, A]
def describeAlarms: CloudWatch[StreamF[MetricAlarm]] =
liftF[CloudWatchA, StreamF[MetricAlarm]](DescribeAlarms())
def deleteAlarms(alarms: StreamF[MetricAlarm]): CloudWatch[Unit] =
liftF[CloudWatchA, Unit](DeleteAlarm(alarms))
def javaSdkCompiler: CloudWatchA ~> StreamF = new (CloudWatchA ~> StreamF) {
override def apply[A](fa: CloudWatchA[A]): StreamF[A] = fa match {
case DescribeAlarms() ⇒ Stream.emit(MetricAlarm("my-alarm")).covary[F]
case DeleteAlarm(_) ⇒ Stream.emit(()).covary[F]
}
}
}
@bpholt
Copy link
Author

bpholt commented Jan 12, 2018

free.scala:31 should have been

      case DescribeAlarms()  Stream.emit(Stream.emit(MetricAlarm("my-alarm")).covary[F])

With that change, the gist compiles.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment