Skip to content

Instantly share code, notes, and snippets.

import foo._
object MyApp extends App {
println(this.foo)
}
@mpilquist
mpilquist / State.scala
Created February 9, 2015 02:31
trampolined state
package cats
package data
import free.Free.Trampoline
import free.Trampoline
// Quick port of http://blog.higher-order.com/assets/trampolines.pdf for cats
sealed abstract class State[S, A] {
import State._
@mpilquist
mpilquist / notes.md
Last active August 29, 2015 14:13
Scala type class syntax notes

This page contains notes about a possible improvement to Scala for defining and working with type classes. It is mostly just notes at this point, not a proposal to be taken seriously.

Consider the following:

type class Functor[F[_]] {
  def map[A, B](fa: F[A], f: A => B): F[B]
  def replace[A, B](fa: F[A], replacement: => B): F[B] = map(fa, _ => replacement)
  def lift[A, B](f: A => B): F[A] => F[B] = fa => map(fa, f)
}
@mpilquist
mpilquist / gist:6d940a84424732f78d76
Last active August 29, 2015 14:09
Aligning coproduct types
package scodec
import shapeless._
import shapeless.ops.coproduct._
object CoproductOps {
sealed trait Align[A <: Coproduct, B <: Coproduct] {
def apply(a: A): B
}
sealed trait Interact[A]
case class Ask(prompt: String)
extends Interact[String]
case class Tell(msg: String)
extends Interact[Unit]
trait Monad[M[_]] {
def pure[A](a: A): M[A]
@mpilquist
mpilquist / disjunction.scala
Created September 18, 2014 15:47
Reasons for none
def noReasons(x: Int): Option[Int] = for {
y <- Option(x).filter(_ > 0)
z <- Option(y / 2).filter(_ > 1)
} yield z
(0 to 4) map noReasons
// scala.collection.immutable.IndexedSeq[Option[Int]] = Vector(None, None, None, None, Some(2))
import scalaz.\/
import scalaz.syntax.std.option._
@mpilquist
mpilquist / out.scala
Created July 2, 2014 16:06
Spurious warning from -Xlint in Scala 2.11.1
› scala -Xlint
Welcome to Scala version 2.11.1 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0).
Type in expressions to have them evaluated.
Type :help for more information.
scala> val x: PartialFunction[Any, Unit] = PartialFunction.empty
x: PartialFunction[Any,Unit] = <function1>
scala> val y: PartialFunction[Any, Unit] = PartialFunction.empty
y: PartialFunction[Any,Unit] = <function1>
@mpilquist
mpilquist / StateActor.scala
Last active December 10, 2018 13:06
Minimal integration of State and Actor
import akka.actor._
import scalaz.State
/**
* Minimal integration between State monad and Akka's Actor.
*
* Usage: upon receiving a message, create a State instance and then call runState(st). The state value
* is persisted in an actor field between invocations of runState.
*/
abstract class StateActor[S] extends Actor {
@mpilquist
mpilquist / variance.scala
Last active August 29, 2015 14:01
Nesting functors
// build.sbt:
// scalaVersion := "2.11.0
//
// libraryDependencies += "org.scalaz" %% "scalaz-core" % "7.1.0-M7"
//
import scalaz._
import Scalaz._
/**
* Scalaz port of Ed Kmett's code in http://comonad.com/reader/2008/rotten-bananas/
@mpilquist
mpilquist / Example.scala
Last active August 29, 2015 13:57
Integrating scalaz.Lens with scalaz.stream.Process1
import scalaz.{ Lens, LensFamily }
import scalaz.stream.{ Process, process1 }
import Process._
object Example extends App {
/**
* Lifts a `Process1[B, B]` to a `Process1[A, A]` using the provided lens.
*
* Values fed to this process are converted to type `B` and fed to