This file contains 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
import cats._ | |
import cats.implicits._ | |
object Main extends App { | |
case class Fold[I, O, M: Monoid]( | |
tally: I => M, | |
summarize: M => O | |
) | |
object Fold { |
This file contains 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
// Note: on timeout, `fa` keeps running in the background | |
// make sure it's not leaking resources | |
def timeout[A](fa: IO[A], timeout: FiniteDuration)( | |
implicit s: Scheduler, | |
ec: ExecutionContext): IO[A] = | |
async.promise[IO, Either[Throwable, A]].flatMap { p => | |
async.fork(fa.attempt.flatMap(p.complete)) *> p | |
.timedGet(timeout, s) | |
.map(_.toRight(new java.util.concurrent.TimeoutException).flatten) | |
.rethrow |
This file contains 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
import cats.Functor | |
import scala.language.higherKinds | |
object L { | |
sealed trait List | |
case object Nil extends List | |
case class Cons(head: Int, tail: List) extends List | |
// Recursive collapse |
This file contains 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
case class Foo(fooId: Int, fooName: String) | |
object Foo extends io.circe.generic.AutoDerivation | |
case class Bar(barId: Int, barName: String) | |
object Bar { | |
import io.circe.Encoder | |
import io.circe.generic.semiauto._ | |
import io.circe.generic.extras.Configuration | |
implicit val config: Configuration = Configuration.default.withSnakeCaseMemberNames |
This file contains 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
import scala.language.higherKinds | |
trait KVStore[F[_]] { | |
def get(key: String): F[Option[String]] | |
def put(key: String, a: String): F[Unit] | |
} | |
import cats._ | |
import cats.data._ | |
import cats.implicits._ |
This file contains 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
object Main extends App { | |
import cats.effect.IO | |
import fs2.{Stream, Sink} | |
import scala.concurrent.ExecutionContext.Implicits.global | |
val count = new java.util.concurrent.atomic.AtomicLong(0) | |
val acquire = IO { println("incremented: " + count.incrementAndGet); () } | |
val release = IO { println("decremented: " + count.decrementAndGet); () } | |
val err = Stream.raiseError(new Exception("oh noes!")) |
This file contains 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
module Main where | |
import Control.Monad.Reader | |
import Control.Monad.State | |
r1 :: IO () | |
r1 = flip runReaderT 1 $ do | |
ask >>= liftIO . print | |
local (+1) $ ask >>= liftIO . print | |
ask >>= liftIO . print |
This file contains 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
object Main extends App { | |
import java.io.File | |
case class Config( | |
foo: Int = -1, | |
//out: File = new File("."), | |
// xyz: Boolean = false, | |
// libName: String = "", | |
// maxCount: Int = -1, | |
// mode: String = "", |
This file contains 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
import cats.data.State | |
import org.scalacheck.rng.Seed | |
object Main extends App { | |
def pick[A](as: List[A]): State[Seed, Option[(A, List[A])]] = State { seed => | |
if (as.size == 0) { | |
(seed, None) | |
} else { | |
val (n, nextSeed) = seed.long | |
val idx = n.toInt.abs % as.size |
This file contains 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
{-# language DeriveFunctor #-} | |
{-# language TypeFamilies #-} | |
{-# language MultiParamTypeClasses #-} | |
{-# language InstanceSigs #-} | |
{-# language FlexibleContexts #-} | |
module Main where | |
import Data.Functor (void) | |
import Data.Functor.Adjunction | |
import Data.Functor.Rep | |
import Data.Distributive |