Created
January 31, 2013 14:52
-
-
Save ubourdon/4683358 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object MyApp extends App { | |
case class Bag[+A](value: A) { // covariance | |
//override def toString = "toto" | |
//def copy | |
//equals | |
//hashcode | |
// + elem de Product | |
// functor dans la théorie des catégories | |
def map[B](f: A => B): Bag[B] = Bag(f(value)) | |
// bind dans la théorie des catégories | |
def flatMap[B](f: A => Bag[B]): Bag[B] = f(value) | |
// Monad+ | |
//def filter(f: Int => Boolean): Bag | |
// Bag doit avoir un élément neutre au cas ou je filtre un Bag qui ne réponds pas au filtre | |
// Bag doit etre un type algébrique = union de plusieurs concept => Bag = bag withValue U EmptyBag | |
// 1 Monad+ = map + bind + filter + point (= truc qui permet de construire Bag a partir d'une valeur => ici le constructeur) | |
// Monad = Monad + sans filter | |
} | |
val plus1 = (b: Bag[Int]) => b.copy(b.value + 1) // Bag => Bag | |
println(plus1(Bag(12))) | |
println( Bag(2).map( v => v * 2 ) ) | |
println(for( v <- Bag(2) ) yield v+1) // <=> Bag(2) map (v => v+1) | |
val t = for { | |
v <- Bag(2) | |
w <- Bag(3) | |
} yield (v + w) // flatMap | |
println(t) | |
val y = for (b <- Bag(2)) yield (b + "titi"); println(y) | |
val z = for (b <- Bag(2); c <- Bag("tata")) yield (b + c); println(z) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment