Skip to content

Instantly share code, notes, and snippets.

View chrilves's full-sized avatar

Christophe Calvès chrilves

View GitHub Profile
@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))
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 {
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]
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
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] {
import scala.language.implicitConversions
object CHK {
/* Quand on sait convertir un type
A dans B et réciproquement
*/
trait Conversion[A,B] { self =>
def to: A => B
def from: B => A
import scala.language.higherKinds
object Toto {
trait Monad[M[_]] {
def pure[A](value: A): M[A]
def flatMap[A,B](ma: M[A], f: A => M[B]): M[B]
final def map[A,B](ma: M[A], f: A => B): M[B] =
flatMap(ma, f `andThen` pure)
Informations de l'ordinateur :
Fabricant : Unknown
Modèle : Unknown
Type : Ordinateur de bureau
Aucun écran tactile détecté
Processeur :
Fabricant du CPU : GenuineIntel
Marque du processeur : Intel(R) Core(TM) i5-4670K CPU @ 3.40GHz
Famille du processeur : 0x6
import scala.language.higherKinds
import scala.language.implicitConversions
import scala.util.Random
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits._
/** A simple trait telling how to save in DB */
sealed trait DBMode {
type Op[_]
def saveInDB[A](a: A): Op[A]
(** We aim to show that the classic Reader monad is equivalent
to an ADT (more precisely a GADT (Generalized Algebraic Data Types).
*)
Section ForStephane.
(** The reader config type (what's read)
*
* trait Config
*)