Skip to content

Instantly share code, notes, and snippets.

View Mzk-Levi's full-sized avatar

Melchizedek Mzk-Levi

View GitHub Profile
object Trampolines {
def odd[A](as: List[A]): TailRec[Boolean] =
as match {
case Nil => Return(false)
case _ :: xs => Suspend(() => even(xs))
}
def even[A](as: List[A]) = as match {
case Nil => Return(true)
case _ :: xs => Suspend(() => odd(xs))
@runarorama
runarorama / gist:9422453
Last active August 29, 2015 13:57
Suggestion for the new representation of `IO` in Scalaz.
import scalaz._
import \/._
import Free._
import scalaz.syntax.monad._
import java.util.concurrent.atomic.AtomicReference
import java.util.concurrent.CountDownLatch
object Experiment {
sealed trait OI[A] {
def map[B](f: A => B): OI[B]
package scalaz
import scalaz.{State => _, _}
import scalaz.Free.FreeC
import scalaz.Id.Id
import FreeState.StateF
sealed abstract class FreeState[S, A] {
def map[B](f: A => B): FreeState[S, B]
}
{-# LANGUAGE RankNTypes #-}
import Prelude hiding (readFile)
import qualified Prelude as P
data Free f a =
Done a
| More (f (Free f a))
instance Functor f => Functor (Free f) where
@pchiusano
pchiusano / machines.hs
Last active August 29, 2015 14:04
Design of new basis for machines / scalaz-stream that does not require separate plan type
-- Type-aligned sequence catenable queue supporting O(1) append, snoc, uncons
-- Don't need it to be a dequeue (unsnoc not needed)
data TCQueue c a b -- c is the category, a is starting type, b is ending type
type Channel f a b = TCQueue (Transition f) a b
type Process f b = Channel f () b
data Transition f a b where
Bind :: (a -> Process f b) -> Transition f a b
OnHalt :: (Cause -> Process f b) -> Transition f a b
@conal
conal / NatStream.hs
Last active August 29, 2015 14:18
Isomorphism between streams and function-of-Nat
{-# OPTIONS_GHC -Wall #-}
module NatStream where
data Nat = Zero | Succ Nat
data Stream a = a :< Stream a
-- Stream is the trie induced by Nat.
-- Here's the isomorphism, explicitly:
@raichoo
raichoo / gist:1163522
Created August 22, 2011 20:56
Scala NList
package nlist
sealed trait Nat
sealed trait Z extends Nat
sealed trait S[A <: Nat] extends Nat
sealed trait NList[A <: Nat, +B] {
type Length = A
def zap[C](l: NList[Length, B => C]): NList[Length, C]
@jdegoes
jdegoes / DataScienceInScala.scala
Created February 8, 2013 15:11
Example code for the Creating a Data Science Platform in Scala talk.
object BenchmarkCommon {
import scala.util.Random
val DatasetSize = 10000
val Iterations = 10000
val ArrayPoolSize = 1000
val ArrayPool = {
def randomArray(): Array[Int] = {
val array = new Array[Int](DatasetSize)
import scalaz._, Scalaz._, NonEmptyList._
sealed trait Interval[+A] {
val min: A
val max: A
def map[B: Order](f: A => B): Interval[B] =
Interval(f(min), f(max))
def merge[AA >: A](i: Interval[AA])(implicit E: Enum[AA]): Option[Interval[AA]] =
@halcat0x15a
halcat0x15a / gist:5376994
Created April 13, 2013 04:57
Maybe Monad with Church Encoding in Scala
import scala.language.higherKinds
import scala.language.implicitConversions
import scala.language.reflectiveCalls
trait Forall[M[_]] {
def apply[A]: M[A]
}
object Maybe {