Skip to content

Instantly share code, notes, and snippets.

@YoEight
YoEight / Fun.hs
Last active December 13, 2015 22:48
{-# LANGUAGE RankNTypes #-}
module Fun where
import Data.Monoid
-- Warm up
-- negate 1 = -1
negateCPS :: Int -> (Int -> r) -> r
def foo[F[_], A](xs: F[A], f: A => String)(implicit F: Foldable[F], P: Pure[F], M: Monoid[F[A]]): Map[String, F[A]] = {
F.foldMap[A, Map[String, F[A]]](xs, a => Map(f(a) -> F.pure(a)))
}
foo[List, BreakDownRow](xs: List[BreakDownRow])(f: BreakDownRow => String): Map[String, List[BreakDownRow]]
foo[Id, SummaryRow](xs: Id[SummaryRow])(f: SummaryRow => String): Map[String, Id[SummaryRow]]
bar[A <: Json, B <: Json](l: Map[String, A], r: Map[String, B]): Map[String, Json]
@YoEight
YoEight / reporting.scala
Created February 4, 2013 10:55
Reporting
object Toto {
case class SummaryRow(msg: String, count: Int, rate: Float)
case class BreakDownRow(sup: String, amount: Int)
case class SegmentedRow(segment: String, length: Int)
type Id[A] = A
trait Reporting[A, B[_]] {
val parser: Parser[A]
def incr(i: Int): EvanderProcess[Int] = (i + 1).pure[EvanderProcess]
def apply_n(events: List[Event]): EvanderProcess[List[Event]] = {
val sorted = events sortBy (_.v)
val received = sorted map (_.v)
def msg(expected: Int, got: Int, version: Int) =
"Event %d expected, but got %d instead (in events %s from version %d)".format(expected, got, received mkString ", ", version)
for {
@YoEight
YoEight / monadWriterExample.scala
Last active December 10, 2015 02:19
A MonadWriter example using Scala 2.9.2 and Scalaz 7.0.0-M7
package gist
import scalaz._
import scalaz.std.string._
import scalaz.syntax.listenableMonadWriter._
object MonadWriterExample extends App {
implicit val monadWriter = EitherT.listenableMonadWriter[Writer, String, String]
case class Person(firstName: String, age: Int)
@YoEight
YoEight / cps.hs
Created October 31, 2012 16:36
CPS-ed State monad
{-# LANGUAGE RankNTypes #-}
module CPS where
import Control.Applicative
newtype StateCPS s a = StateCPS { runStateCPS :: forall r. s -> (a -> s -> r) -> r }
instance Functor (StateCPS s) where
fmap f (StateCPS k) = StateCPS $ \s c -> k s (c . f)
@YoEight
YoEight / examples.hs
Created October 10, 2012 09:43
Haskell machine package examples
module Examples where
import Control.Applicative
import Control.Monad.Trans
import Data.Machine
data Bit = EMPTY | One | Zero
instance Show Bit where
@YoEight
YoEight / binary.hs
Created September 19, 2012 15:43
Basic Binary parser
{-# LANGUAGE RankNTypes #-}
module Binary where
import Prelude hiding (head)
import Control.Applicative
import Control.Monad
import qualified Data.ByteString as B
@YoEight
YoEight / monadwriterexample.scala
Created August 8, 2012 06:47
Monad Writer Example
package scalaz
import scalaz.std.string._
import scalaz.syntax.listenableMonadWriter._
object MonadWriterExample extends App {
implicit val monadWriter = EitherT.listenableMonadWriter[Writer, String, String]
case class Person(firstName: String, age: Int)
@YoEight
YoEight / coprod.scala
Created July 31, 2012 12:00
Coproduct implicit resolution
object coprod {
trait Functor[F[_]]{
def map[A, B](fa: F[A])(f: A => B): F[B]
}
implicit val optionFunctor = new Functor[Option]{
def map[A, B](fa: Option[A])(f: A => B) = fa map f
}
implicit val listFunctor = new Functor[List]{