Skip to content

Instantly share code, notes, and snippets.

View chrilves's full-sized avatar

Christophe Calvès chrilves

View GitHub Profile
trait Stream[A] { self =>
def head : A
def tail : Stream[A]
def map[B](f : A => B) : Stream[B] = new Stream[B] {
val head = f(self.head)
lazy val tail = self.tail.map(f)
}
def extend[B](f : Stream[A] => B) : Stream[B] = new Stream[B] {
trait NaturalTransformation[F[_], G[_]] {
import cats.syntax.eq._
import cats.Functor
implicit val F : Functor[F]
implicit val G : Functor[G]
def apply[A](fa : F[A]) : G[A]
/** For an implementation to be valid, this function must
object SimpleGeneralizedZipper {
sealed abstract class Direction
final case object Haut extends Direction
final case object Gauche extends Direction
final case object Droite extends Direction
sealed abstract class Stream[-I, +A] {
def focus : A
def cont : I => Stream[I, A]
object FreeMonadExample {
import scala.language.higherKinds
/** The Functor type class */
trait Functor[F[_]] {
def map[A,B](fa : F[A])(f : A => B) : F[B]
}
implicit final class FunctorOps[F[_],A](val self : F[A]) extends AnyVal {
@chrilves
chrilves / composition.scala
Last active November 22, 2016 17:57
Composition that have less chance to blow the stack.
object StackTest {
def main(args : Array[String]) : Unit = {
val f1 = (1 to 100000).foldLeft(ComposeQueue(identity[Int])) { (f, _) => f.andThen(_ + 1) }
println(f1(0))
val f2 = (1 to 1000000).foldLeft(ComposeQueue(identity[Int])){ (f, _) => f.andThen(_ + 1) }
println(f2(0))
val f3 = (1 to 1000000).foldLeft(ComposeQueue(identity[Int])) { (f, _) => f.compose(_ + 1) }
println(f3(0))