This file contains hidden or 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.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.) | |
*/ |
This file contains hidden or 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 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._ |
This file contains hidden or 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)) |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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) | |
} |