Skip to content

Instantly share code, notes, and snippets.

@YoEight
YoEight / MonadWriterExample.scala
Created May 22, 2012 20:25
MonadWriter example
import scalaz._
import scalaz.std.string._
import scalaz.MonadWriter
import scalaz.ValidationT
import scalaz.WriterT._
object MonadWriterExample extends App {
val MW = ValidationT.validationTMonadWriter[Writer, String, String]
case class Person(firstName: String, age: Int)
@YoEight
YoEight / Alacarte.scala
Created June 28, 2012 11:09
Scala DataType à la carte
object Alacarte {
trait Functor[F[_]]{
def map[A, B](fa: F[A])(f: A => B): F[B]
}
trait Eval[F[_]] {
def F: Functor[F]
def evalAlgebra(fa: F[Int]): Int
}
@YoEight
YoEight / dataflow.hs
Created July 5, 2012 13:33
Dataflow programming homework
module Dataflow where
import Data.List
newtype Id a = Id { unId :: a }
class Comonad f where
counit :: f a -> a
cobind :: (f a -> b) -> f a -> f b
@YoEight
YoEight / free.hs
Created July 5, 2012 13:35
Codensity homework
{-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies, RankNTypes, FlexibleInstances, DeriveFunctor, DeriveFoldable, DeriveTraversable, UndecidableInstances #-}
module Free where
import Control.Monad.Trans
import Control.Monad
import Data.Traversable
import Data.Foldable
data Free f a = Pure a | Free (f (Free f a))
@YoEight
YoEight / future.scala
Created July 11, 2012 18:26
Akka Future
//assuming all akka imports
object FutureTest {
private case class FutureW[A](proc: ExecutionContext => Future[A]) { // note the similarity with Kleisli[Future, ExecutionContext, A]
implicit val defaultDuration = Duration("3s")
def map[B](f: A => B) = FutureW(c => proc(c).map(f))
def flatMap[B](f: A => FutureW[B]) = FutureW(c => proc(c).flatMap(a => f(a).apply(c)))
}
@YoEight
YoEight / Maybe.scala
Created July 27, 2012 13:14
Maybe monad as a continuation
object maybeExample {
sealed trait Maybe[A]{ self =>
def fold[Z](just: A => Z, nothing: => Z): Z
def map[B](f: A => B) = fold(a => Just(f(a)), Nothing[B])
def flatMap[B](f: A => Maybe[B]) = new Maybe[B]{
def fold[Z](just: B => Z, nothing: => Z) =
self.fold(a => f(a).fold(just, nothing), nothing)
}
@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]{
@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 / 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 / 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