This file contains hidden or 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
scala> case class Bad(a: Int) { override def equals(a:Any) = true } | |
scala> val f = (n:Int) => Bad(n) | |
scala> val g = (b:Bad) => b.a | |
... | |
scala> Set(1,2,3).map(f andThen g) | |
res2: scala.collection.immutable.Set[Int] = Set(1, 2, 3) | |
scala> Set(1,2,3).map(f).map(g) |
This file contains hidden or 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
// Where the hell does B come from? Looks like a type variable is escaping. | |
scala> Left(1).disjunction | |
res14: scalaz.\/[Int,B] = -\/(1) | |
// It's not Nothing | |
scala> Left(1).disjunction : (Int \/ Nothing) | |
<console>:17: error: type mismatch; | |
found : scalaz.\/[Int,B] |
This file contains hidden or 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
name := "just-scalaz" | |
version := "0.1" | |
scalaVersion := "2.10.2" | |
libraryDependencies ++= Seq( | |
"org.scalaz" % "scalaz-core_2.10" % "7.0.3", | |
"org.scalaz" % "scalaz-effect_2.10" % "7.0.3" | |
) |
This file contains hidden or 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
// Scala widens Char to Int here, rather than unifying to AnyVal | |
scala> List('a', 1) | |
res7: List[Int] = List(97, 1) | |
// And even widens to Double. Nutty | |
scala> List('a', 3.14) | |
res14: List[Double] = List(97.0, 3.14) |
This file contains hidden or 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
// Eta-expansion turns A* to Seq[A] | |
scala> def foo(ns:Int*) = ns.sum | |
foo: (ns: Int*)Int | |
scala> foo(1,2,3) | |
res0: Int = 6 | |
scala> val foov = foo _ | |
foov: Seq[Int] => Int = <function1> |
This file contains hidden or 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
class Animal(val name: String) { | |
override def equals(a: Any): Boolean = | |
a match { | |
case a: Animal if a.name == name => true | |
case _ => false | |
} | |
} | |
class Cat(name: String, val color: String) extends Animal(name) { | |
override def equals(a: Any): Boolean = |
This file contains hidden or 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
// rank 1 | |
type num[a] = (a => a) => a => a | |
def zero[a]: num[a] = f => x => x | |
def succ[a](n: num[a]): num[a] = f => x => f(n(f)(x)) | |
def eval(n: num[Int]): Int = n(_ + 1)(0) | |
// rank 2 |
This file contains hidden or 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
def exists[A](as: List[A], f: A => Boolean): Boolean = | |
as match { | |
case Nil => false | |
case a :: as0 => if (f(a)) true else exists(as0, f) | |
} | |
def forall[A](as: List[A], f: A => Boolean): Boolean = | |
!exists(as, (a: A) => !f(a)) | |
This file contains hidden or 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
scala> def a: Any = List(1,2,3).map(x => x + 1) | |
a: Any | |
scala> def b: Any = List(1,2,3).map(x => return x + 1) | |
b: Any | |
scala> a | |
res1: Any = List(2, 3, 4) | |
scala> b |
This file contains hidden or 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
scala> def foo[A[_]] = 1 | |
foo: [A[_]]=> Int | |
scala> foo[List] | |
res11: Int = 1 | |
scala> foo[Int] | |
<console>:24: error: Int takes no type parameters, expected: one | |
foo[Int] | |
^ |