Skip to content

Instantly share code, notes, and snippets.

@julianpeeters
Created March 29, 2019 15:33
Show Gist options
  • Save julianpeeters/8ff81afcade6bd224b773350906e6597 to your computer and use it in GitHub Desktop.
Save julianpeeters/8ff81afcade6bd224b773350906e6597 to your computer and use it in GitHub Desktop.
import cats.implicits._
import cats.data._
import cats.effect._
import cats.effect.concurrent._
abstract class Alg[F[_]]{
def up: F[Unit]
def down: F[Unit]
def get: F[Int]
}
object Alg {
def impl[F[_]: Sync](init: Int): F[Alg[F]] =
Ref.of[F, Int](init).map(new CounterAlg[F](_))
private class CounterAlg[F[_]](ref: Ref[F, Int]) extends Alg[F]{
def up: F[Unit] = ref.update(_ + 1 )
def down: F[Unit] = ref.update(_ - 1)
def get: F[Int] = ref.get
}
private class CounterAlgWithAbstractBase[F[_]](
ref: Ref[F, Int]
) extends Alg[Kleisli[F, Int, ?]]{
def up: Kleisli[F, Int, Unit] = Kleisli{i =>
ref.update(_ + i)
}
def down: Kleisli[F, Int, Unit] = Kleisli{i =>
ref.update(_ - i)
}
def get: Kleisli[F, Int, Int] = Kleisli{i =>
ref.get
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment