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 / gist:d705dc450596ae75e45c
Last active August 29, 2015 14:07
Turning List[Int => Int] into Int => List[Int] with cosequence
import scalaz.std.function._
import scalaz.syntax.functor._
import scalaz.Functor
/* https://gist.github.com/ceedubs/f8273ede78f86df7df7f shows we can do this with sequenceU.
* Stephen Compall (S11001001) pointed out that cosequence can do this without requiring a
* Traverse instance of the outer type by using the Distributive instance of the inner type.
* (It still requires a Functor instance of the outer type, but many types such as Task, Future,
* etc have Functor but not Traverse.)
*/
@ceedubs
ceedubs / OrderingContravariantExample.scala
Created November 20, 2014 13:17
Contravariant[Ordering]
import scalaz._
import scala.math.Ordering
implicit val orderingContra: Contravariant[Ordering] = new Contravariant[Ordering] {
def contramap[A, B](r: Ordering[A])(f: B => A): Ordering[B] = r.on(f)
}
case class Doodad(description: String, priceCents: Int)
import scalaz.syntax.contravariant._
@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))
@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 / 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 / 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 / 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 / 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
}
}
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 / 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)
}