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 HList._ | |
sealed abstract class HList | |
case object HNil extends HList | |
final case class HCons[H, T <: HList](head: H, tail: T) extends HList | |
object HList { | |
type HNil = HNil.type | |
type ::[H, T <: HList] = HCons[H, T] |
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.free.Trampoline, Trampoline._ | |
import cats.implicits._ | |
def isEven(xs: List[Int]): Trampoline[Boolean] = | |
if (xs.isEmpty) done(true) else suspend(isOdd(xs.tail)) | |
def isOdd(xs: List[Int]): Trampoline[Boolean] = | |
if (xs.isEmpty) done(false) else suspend(isEven(xs.tail)) | |
// returns true (doesn't overflow the stack) |
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 CurryInference { | |
implicit final class OptionOps[A](val oa: Option[A]) { | |
/** curried fold */ | |
def curriedFold[B](z: => B)(f: A => B): B = oa.fold(z)(f) | |
/** uncurried fold */ | |
def uncurriedFold[B](z: => B, f: A => B): B = oa.fold(z)(f) | |
} |
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
trait Show[A] { | |
def show(a: A): String | |
} | |
object Show { | |
implicit val stringShow: Show[String] = new Show[String] { | |
def show(a: String) = a | |
} | |
} |
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
trait Show[A] { | |
def show(a: A): String | |
} | |
object Show { | |
implicit val stringShow: Show[String] = new Show[String] { | |
def show(a: String) = a | |
} | |
} |
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
package shapeless.examples | |
import shapeless._ | |
import nat._ | |
import ops.hlist._ | |
object TableExample { | |
final case class Row[L <: HList](cells: L) |
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 scalaz._ | |
import shapeless._ | |
import shapeless.ops.hlist._ | |
/** contrived IndexedState example */ | |
object HStack extends App { | |
def push[H, T <: HList](h: H): IndexedState[T, H :: T, H] = | |
IndexedState(t => (h :: t, h)) | |
def pop[H, T <: HList]: IndexedState[H :: T, T, H] = |
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 FreeCTwoRandomIntExample extends App { | |
import scalaz._ | |
import scalaz.effect.IO | |
import scala.util.Random | |
sealed trait Action[A] | |
final case class Log(msg: String) extends Action[Unit] | |
final case class RandomInt(below: Int) extends Action[Int] |
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 WriterTExample extends App { | |
import scalaz._ | |
import scalaz.effect.IO | |
import scala.util.Random | |
import scalaz.syntax.monad._ | |
type Logged[F[_], A] = WriterT[F, DList[String], A] | |
def log[F[_]](msg: => String)(implicit F: Applicative[F]): Logged[F, Unit] = |
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 WriterExample extends App { | |
import scalaz._ | |
// DList has constant time append, which is nice for logging | |
// for simplicity we will just use String for logs, but you could | |
// get fancier and have a model with Error, Warn, Info, Debug, etc. | |
type Logged[A] = Writer[DList[String], A] | |
def log(msg: String): Logged[Unit] = WriterT.tell(DList(msg)) |