Skip to content

Instantly share code, notes, and snippets.

View ceedubs's full-sized avatar

Cody Allen ceedubs

  • central Indiana, USA
View GitHub Profile
@ceedubs
ceedubs / hlist.scala
Created March 5, 2016 21:45
The start of an HList implementation from my nescala unconf session. There are two errors that are left to the reader as an exercise. Hint: it's mostly right but a couple types are incorrect.
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]
@ceedubs
ceedubs / trampoline.scala
Created October 20, 2015 11:20
Port of example code from http://www.scala-lang.org/api/2.10.4/#scala.util.control.TailCalls$ to Trampoline. Note that for this example, Eval is probably more efficient.
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)
@ceedubs
ceedubs / folds.scala
Created April 25, 2015 16:49
Type inference of curried vs uncurried fold on Option
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)
}
trait Show[A] {
def show(a: A): String
}
object Show {
implicit val stringShow: Show[String] = new Show[String] {
def show(a: String) = a
}
}
@ceedubs
ceedubs / NoImplicitFor.scala
Created March 5, 2015 21:51
Evidence that a typeclass instance does not exist for a type. You probably don't want to use this.
trait Show[A] {
def show(a: A): String
}
object Show {
implicit val stringShow: Show[String] = new Show[String] {
def show(a: String) = a
}
}
@ceedubs
ceedubs / TableExample.scala
Created March 5, 2015 20:42
Weird implicit search failure with Shapeless At and Nat._0
package shapeless.examples
import shapeless._
import nat._
import ops.hlist._
object TableExample {
final case class Row[L <: HList](cells: L)
@ceedubs
ceedubs / HDequeue.scala
Created February 25, 2015 23:25
HStack and HDequeue
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] =
@ceedubs
ceedubs / FreeCTwoRandomIntExample.scala
Created December 7, 2014 23:18
An app that does some logging and adds two random Ints like https://gist.github.com/ceedubs/510a7eb9147c9d27132c except using FreeC
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]
@ceedubs
ceedubs / WriterTExample.scala
Created December 6, 2014 17:34
Using WriterT for logging of Random => IO[A]
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] =
@ceedubs
ceedubs / WriterExample.scala
Last active August 27, 2021 05:27
Using the Writer monad for log messages
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))