Skip to content

Instantly share code, notes, and snippets.

@LukaJCB
LukaJCB / MonadTrans.scala
Last active June 29, 2018 14:52
Easy monad transformer lifting?
trait MonadTrans[T[_[_], _]] {
def lift[M[_]: Monad, A](ma: M[A]): T[M, A]
def hoist[M[_]: Monad, N[_]: Monad, A](tma: T[M, A])(f: M ~> N): T[N, A]
}
object MonadTrans {
def apply[T[_[_], _]: MonadTrans]: MonadTrans[T] = implicitly
}
implicit def applicativeAskMonadTrans[T[_[_], _]: MonadTrans, M[_]: Monad, R]
@LukaJCB
LukaJCB / TaglessOptimize.scala
Last active May 3, 2019 10:43
Optimize an Applicative Program with Tagless Final
trait KVStore[F[_]] {
def get(key: String): F[Option[String]]
def put(key: String, a: String): F[Unit]
}
class TestInterpreter() extends KVStore[IO] {
def get(key: String): IO[Option[String]] = IO {
println("Hit network!")
Option(key + "!")
@LukaJCB
LukaJCB / Traverse.scala
Created November 29, 2017 16:20
Traverse
@typeclass trait Semigroup[A] {
def combine(x: A, y: A): A
}
@typeclass trait Monoid[A] extends Semigroup[A] {
def id: A
}
@typeclass trait Semigroupal[F[_]] {
def product[A, B](ga: F[A], gb: F[B]): F[(A, B)]
@LukaJCB
LukaJCB / Algebra.purs
Last active September 22, 2019 13:01
Alternative Tagless Final encoding in PureScript
module Algebra where
import Prelude
import Control.Monad.Eff (Eff)
import Data.Maybe (Maybe(..))
newtype ConsoleAlg f = ConsoleAlg
{ printLn :: String -> f Unit
, readLn :: f String
@typeclass trait NonEmptyTraverseUnordered[F[_]] {
def nonEmptyTraverseUnordered[G[_]: CommutativeApply, A, B](sa: F[A])(f: A => G[B]): G[F[B]]
def nonEmptySequenceUnordered[G[_]: CommutativeApply, A](fga: F[G[A]]): G[F[A]] =
nonEmptyTraverseUnordered(fga)(identity)
}
@typeclass trait NonEmptyCommutativeParallel[F[_], M[_]] {
def commutativeApply: CommutativeApply[F]
def commutativeFlatMap: CommutativeFlatMap[M]
package cats.free
import cats.free.Free.{FlatMapped, Pure, Suspend}
import cats.{Applicative, Monad, Parallel, ~>}
import cats.implicits._
class FreeParallel[S[_], A](val value: Free[FreeApplicative[S, ?], A]) extends AnyVal
object FreeParallel {
def apply[S[_], A](fp: Free[FreeApplicative[S, ? ], A]): FreeParallel[S, A] = new FreeParallel(fp)
@LukaJCB
LukaJCB / Main.scala
Created September 10, 2017 15:09
ParallelTaglessFinal
import cats._
import cats.implicits._
trait Alg[M[_]] {
def doSomething(s: String): M[Unit]
}
object AlgVect extends Alg[Vector] {
def doSomething(s: String): Vector[Unit] = Vector(println(s))
}
type Stack[E, A] = EitherT[Future, NonEmptyList[E], A]
def longRunningComputation(person: Person): Future[Either[NonEmptyList[Error], Result]] = ???
val people: List[Person] = ???
val result: Stack[Error], List[Result]] =
people.parTraverse(longRunningComputation _ andThen EitherT.apply)
@LukaJCB
LukaJCB / IO.scala
Created May 22, 2017 17:55
SimpleIOMonad
class IO[+T](run: => T) {
def map[R](f: T => R): IO[R] = {
IO(f(run))
}
def flatMap[R](f: T => IO[R]): IO[R] = {
IO(f(run).unsafeRun())
}
def unsafeRun(): T = run
module Main where
import OutWatch
import Builder ((<==))
import Control.Monad.Eff (Eff)
import Prelude (Unit, map, (#), (+))
import RxJS.Observable (interval, startWith)
main :: Eff () Unit
main = let