Joy of Coding
March 07, 2014
440

# Contravariance is the Dual of Covariance - Erik Meijer

March 07, 2014

## Transcript

6. ### Side Effects val steveb: ()=>String  steveb() // “developer” steveb() //

“blah” steveb() // “Windows 8” steveb() // ☲

8. ### Side Effects val sjobs: ()=>String  sjobs() // “iPhone” sjobs() //

“iPad” sjobs() // “iCloud” sjobs() // †

11. ### Interfaces trait Enumerable[+T] {  def getEnumerator(): Enumerator[T]  } ! trait

Enumerator[+T] {  def moveNext(): Boolean  def current: T  }
12. ### Lifting trait Enumerable[+T] {  def getEnumerator(): Enumerator[T] ! def lift(f:

Enumerator[T]=>Enumerator[S]):  val that = this Enumerable[S] = {  new Enumerable[S] {  def getEnumerator() = f(that.GetEnumerator()) }  }  }
13. ### Functor val map: (A=>B)=>  Enumerable[A]=>Enumerable[B]    map f as =

as.lift(_.map)
14. ### Monad val flatMap: (A=>Enumerable[B])=>  Enumerable[A]=>Enumerable[B]    flatmap f as =

as.lift(_.flatmap)

a=>(b(f a))

kubric(†)

25. ### Interfaces trait Observable[+T] {  def Subscribe(o: Observer[T]): Unit  } !

trait Observer[-T] {  def onCompleted(): Unit  def onError(error: Throwable): Unit  def onNext(value: T): Unit  }
26. ### Lifting trait Observable[+T] {  def subscribe(o: Observer[T]) ! def lift(f:

Observer[S]=>Observer[T]):  val that = this Observable[S] = {  new Observable[S] {  def subscribe(o: Observer[S]) = that.Subscribe(f(o)) }  }  }
27. ### Functor val map: (A=>B)=>  Observable[A]=>Observable[B]    map f as =

as.lift(_.map)