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
| {-# LANGUAGE MultiParamTypeClasses #-} | |
| {-#LANGUAGE FlexibleInstances #-} | |
| module CanBuildFrom where | |
| import Data.Foldable | |
| import Data.Map | |
| data Builder from elem to = Builder { | |
| result :: to, | |
| add :: elem -> Builder from elem to |
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
| scala> trait Assoc[K] { type V ; val value: V } | |
| defined trait Assoc | |
| scala> def mkAssoc[V0](k: String, v: V0): Assoc[k.type] { type V = V0 } = | |
| | new Assoc[k.type] { type V = V0 ; val value = v } | |
| mkAssoc: [V0](k: String, v: V0)Assoc[k.type]{type V = V0} | |
| scala> implicit def nameAssoc = mkAssoc("Name", "Mary") | |
| nameAssoc: Assoc[String("Name")]{type V = String} |
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
| +----------------+ | |
| |InvariantFunctor| | |
| +----------------+ | |
| | | | |
| ----------------------------- | | |
| | ------ |
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 Collections { | |
| type CC[+X] // the overarching container type (in scala: any covariant collection, e.g. List, Vector) | |
| type Min[+X] // the minimum type constructor which can be reconstituted to CC[X] (in scala: GenTraversableOnce) | |
| type Opt[+X] // the container type for optional results (in scala: Option) | |
| type CCPair[+X] // some representation of a divided CC[A] (at simplest, (CC[A], CC[A])) | |
| type ~>[-V1, +V2] // some means of composing operations (at simplest, Function1) | |
| type Iso[A] = CC[A] ~> CC[A] // e.g. filter, take, drop, reverse, etc. | |
| type Map[-A, +B] = CC[A] ~> CC[B] // e.g. map, collect | |
| type FlatMap[-A, +B] = CC[A] ~> Min[B] // e.g. flatMap |
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 scala.language.higherKinds | |
| trait Functor[F[_]] { | |
| def map[A, B](fa: F[A])(f: A => B): F[B] | |
| } | |
| object Functor { | |
| def apply[F[_], A, B](fa: F[A])(f: A => B)(implicit F: Functor[F]): F[B] = | |
| F.map(fa)(f) | |
| } |
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
| scalaVersion := "2.10.2" | |
| resolvers += Resolver.sonatypeRepo("snapshots") | |
| libraryDependencies += "org.typelevel" %% "shapeless-scalaz" % "0.2-SNAPSHOT" | |
| libraryDependencies += "org.typelevel" %% "shapeless-scalacheck" % "0.2-SNAPSHOT" | |
| initialCommands in console := """ | |
| import scalaz._ |
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 scalaio | |
| import shapeless._, poly._ | |
| object list extends (Id ~> List) { | |
| def apply[T](t : T) = List(t) | |
| } | |
| object headOption extends (List ~> Option) { | |
| def apply[T](l : List[T]) = l.headOption |
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
| yonedaLemma = Iso (flip fmap) ($ id) |
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
| > {-# LANGUAGE Rank2Types #-} | |
| > | |
| > import Prelude hiding (id) | |
| > import Data.Functor.Constant (Constant(Constant), getConstant) | |
| > import Control.Arrow (Arrow, arr, first, Kleisli(Kleisli), runKleisli, (&&&)) | |
| > import Control.Category ((<<<)) | |
| > import Control.Lens (sequenceAOf) | |
| > import qualified Control.Lens as L | |
| > import qualified Control.Lens.Internal.Setter as LS | |
| > import Data.Profunctor (Profunctor, rmap) |
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 shapeless._ | |
| sealed trait List[+T] | |
| case class Cons[T](hd: T, tl: List[T]) extends List[T] | |
| sealed trait Nil extends List[Nothing] | |
| case object Nil extends Nil | |
| trait Show[T] { | |
| def apply(t: T): String | |
| } |