Skip to content

Instantly share code, notes, and snippets.

@YoEight
YoEight / recursion.hs
Created December 23, 2013 22:57
Recursion scheme playground
{-# LANGUAGE NoImplicitPrelude #-}
module Origami where
import Prelude
(
(.)
, (-)
, (*)
, (++)
, ($)
@YoEight
YoEight / proc.scala
Last active December 31, 2015 22:09
scalaz.Process and Free equiv. using simple structure encoding
sealed trait Free[F[_], A]
case class Return[F[_], A](v: A) extends Free[F, A]
case class Suspend[F[_], A](s: F[Free[F, A]]) extends Free[F, A]
sealed trait ProcessB[T[_], O, B]
case class Emit[T[_], O, B](o: O, next: B) extends ProcessB[T, O, B]
case class Await[T[_], A, O, B](ta: T[A], k: A => B) extends ProcessB[T, O, B]
case class Stop[T[_], O, B] extends ProcessB[T, O, B]
type Process[T[_], O] = Free[({ type F[x] = ProcessB[T,O,x] })#F, O]
@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)
}
@YoEight
YoEight / output
Last active December 26, 2015 16:39
GHC (7.6.3) GADTs bug ? Output is GHC output when GADTs is enabled
ghc -Wall -ferror-spans -fforce-recomp -c /home/yorick/Desktop/process.hs
/home/yorick/Desktop/process.hs:34:9-34:
Couldn't match type `a1' with `a2'
because type variable `a2' would escape its scope
This (rigid, skolem) type variable is bound by
a type expected by the context:
m a2 -> (a2 -> Process m a) -> Process m a -> r
The following variables have types that mention a1
awaiting :: m a1 -> (a1 -> Process m a) -> Process m a -> r
@YoEight
YoEight / Foo-error.hs
Last active December 19, 2015 23:49
Why Foo-error.hs doesn't compile while Foo-working.hs does ?
{-# LANGUAGE RankNTypes #-}
type Foo = forall m. Monad m => Int -> m Int
data Free f a = Pure a
| Free (f (Free f a))
data Instr a = Instr Int a
instance Functor Instr where
@YoEight
YoEight / Conversion.scala
Last active December 19, 2015 18:48
Scalaz-stream Process to Play Enumerator
package deiko
import scala.concurrent.Future
import scala.concurrent.duration.Duration
import scalaz.concurrent.Task
import scalaz.stream._
import play.api.libs.iteratee._
import Process._
object Conversion {
@YoEight
YoEight / Server.scala
Created July 4, 2013 12:17
Chunk encoded response using Unfiltered, Netty and Scalaz-Stream
package deiko
import java.nio.charset.Charset
import java.util.concurrent.CancellationException
import org.jboss.netty.buffer.ChannelBuffers
import org.jboss.netty.channel.{ Channel, ChannelFuture, ChannelFutureListener }
import org.jboss.netty.handler.codec.http.{ DefaultHttpChunk, HttpChunk }
import scalaz.stream.{ Process, process1, processes }
import scalaz.concurrent.Task
import Process.{ Process1, Sink }
@YoEight
YoEight / conversion.scala
Last active December 18, 2015 01:48
Scala-machines Machine to Play Enumerator
import com.clarifi.machines._
import play.api.libs.iteratee.{ Enumerator, Input, Iteratee }
import play.api.libs.concurrent.Execution.Implicits._
import scala.concurrent.Future
def simple[K, E](machine: Machine[K, E]): Enumerator[E] = new Enumerator[E] {
def apply[A](start: Iteratee[E, A]): Future[Iteratee[E, A]] =
val result = machine.foldLeft(Future.successful(start)) { (future, value) ⇒
@YoEight
YoEight / gist:5679174
Created May 30, 2013 16:20
scala compiler bug. It compiles but produce a class cast exception at runtime
[error] (run-main) java.lang.ClassCastException: java.lang.Integer cannot be cast to scala.runtime.Nothing$
java.lang.ClassCastException: java.lang.Integer cannot be cast to scala.runtime.Nothing$
at fr.applicius.kyub.control.stream.Streamer$$anonfun$echo$1.apply(Streamer.scala:108)
at fr.applicius.kyub.control.stream.Streamer$$anonfun$1.apply(Streamer.scala:102)
at fr.applicius.kyub.control.stream.Streamer$$anonfun$1.apply(Streamer.scala:101)
at fr.applicius.kyub.control.stream.Step$Return.flatMap(Streamer.scala:71)
at fr.applicius.kyub.control.stream.Step$Await$$anonfun$flatMap$1.apply(Streamer.scala:76)
at fr.applicius.kyub.control.stream.Step$Await$$anonfun$flatMap$1.apply(Streamer.scala:76)
at fr.applicius.kyub.control.stream.Step$Await$$anonfun$flatMap$1.apply(Streamer.scala:76)
at fr.applicius.kyub.control.stream.Step$Await$$anonfun$flatMap$1.apply(Streamer.scala:76)
@YoEight
YoEight / cart.scala
Created May 15, 2013 16:42
cartesian product using anamorphism
object ex {
def ana[A, B](start: B)(f: B => Option[(A, B)]): List[A] = f(start) match {
case Some((a, b)) => a :: ana(b)(f)
case _ => Nil
}
def cartesian[A](xss: List[List[A]]): List[List[(A, A)]] = ana(xss) {
case xs :: vs :: rest =>
val action = for {
x <- xs