Skip to content

Instantly share code, notes, and snippets.

View folone's full-sized avatar
🏔️

George Leontiev folone

🏔️
View GitHub Profile
@gseitz
gseitz / CanBuildFrom.hs
Last active November 16, 2015 07:03
CanBuildFrom.hs
{-# 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
@milessabin
milessabin / gist:aae285025a32fac0f5c1
Last active August 26, 2017 10:28
Trivial type safe heterogenous map using only dependent method types, singleton-typed String literal keys and implicits.
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}
+----------------+
|InvariantFunctor|
+----------------+
| |
----------------------------- |
| ------
@paulp
paulp / coll.scala
Last active December 30, 2015 15:18
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
@YoEight
YoEight / Exo.scala
Created November 27, 2013 13:13
Mu exercise (Scala 2.10)
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)
}
@larsrh
larsrh / build.sbt
Created November 17, 2013 21:49
Code examples from scala.io 2013 (see comments). Note that until shapeless 2.0.0 is out, this is still in a state of flux.
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._
@milessabin
milessabin / gist:7136706
Last active December 26, 2015 10:29
My crib sheet from the shapeless workshop @ Scala IO.
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
anonymous
anonymous / Haskell
Created October 7, 2013 19:22
Comparison of the straightforward embedding of a basic tenet of category theory in Scala vs Haskell.
yonedaLemma = Iso (flip fmap) ($ id)
@tomjaguarpaw
tomjaguarpaw / lensesForArrows.lhs
Last active June 19, 2018 21:14
Lenses for Arrows describes how the Lens datatype from Control.Lens can be generalise to support arrows.
> {-# 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)
@milessabin
milessabin / gist:6707525
Created September 25, 2013 23:14
New in shapeless 2.0.0-SNAPSHOT (post M1): lazy recursive implicit values ... normally you would expect the recursion between List[T] and Cons[T] to cause the implicit resolution for Show[T] to blow up with a diverging implicit expansion.
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
}