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
<!doctype html> | |
<html> | |
<head> | |
<title></title> | |
<style> | |
body { | |
background: white; | |
text-align: center; | |
padding: 20px; | |
font-family: Georgia, serif; |
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 FizzBuzz { | |
val defaultRules = List( | |
Rule(3, "fizz"), | |
Rule(5, "buzz") | |
) | |
def main(args: Array[String]) = { | |
fizzBuzz() foreach(println) | |
} |
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
import shapeless._ | |
trait ZipConst[C, L <: HList] { | |
type Out | |
def apply(c: C, l: L): Out | |
} | |
trait ZipConstAux[C, L <: HList, O <: HList] { | |
def apply(c: C, l: L): O | |
} |
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
scala> val vs = 1.success[String] :: 2.success[String] :: Nil | |
vs: List[scalaz.Validation[String,Int]] = List(Success(1), Success(2)) | |
scala> vs.traverseU(_.toValidationNel) | |
res9: List[scalaz.Validation[scalaz.NonEmptyList[String],Int]] = List(Success(1), Success(2)) |
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
scala> import scalaz.syntax.id._ // for .right | |
import scalaz.syntax.id._ | |
scala> import scalaz.EitherT | |
import scalaz.EitherT | |
scala> import scalaz.NonEmptyList | |
import scalaz.NonEmptyList | |
scala> import scala.concurrent.Future |
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
scala> import scalaz.{ @@, Tag } | |
import scalaz.{$at$at, Tag} | |
scala> sealed trait LowerCase | |
defined trait LowerCase | |
// as an optimization you could also define a TrustMeLowerCase method that doesn't call toLowerCase if you know your input is already lower case | |
scala> def LowerCase(s: String): String @@ LowerCase = Tag[String, LowerCase](s.toLowerCase) | |
LowerCase: (s: String)scalaz.@@[String,LowerCase] |
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
package typelevelfizzbuzz | |
import Nat._ | |
sealed trait Nat | |
object Nat { | |
sealed trait _0 extends Nat | |
sealed trait Succ[T <: Nat] extends Nat | |
type _1 = Succ[_0] |
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
/* traverse takes an F[A] and an A => G[B] and returns a G[F[B]]. | |
* In this case List[Long] and Long => Option[String] result in Option[List[String]] | |
*/ | |
scala> val usernames = Map(1L -> "susie", 2L -> "bob") | |
usernames: scala.collection.immutable.Map[Long,String] = Map(1 -> susie, 2 -> bob) | |
scala> List(1L, 2L).traverse(usernames.get) | |
res2: Option[List[String]] = Some(List(susie, bob)) | |
scala> List(1L, 2L, 3L).traverse(usernames.get) |
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
scala> case class UserId(id: Long) | |
defined class UserId | |
scala> def isSusan(id: UserId): Boolean = id == 3L // see the bug? | |
isSusan: (id: UserId)Boolean | |
scala> isSusan(UserId(3L)) | |
res31: Boolean = false// shouldn't this be true? | |
// let's show the compiler how to check whether two UserIds are equal |
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
import scalaz.std.list._ | |
import scalaz.std.function._ | |
import scalaz.syntax.traverse._ | |
val add1: Int => Int = _ + 1 | |
val times2: Int => Int = _ * 2 | |
val squared: Int => Int = n => n * n | |
// sequenceU requires that List has a Traverse instance | |
// and that Function1 (for Int => Int in this case) has an Applicative instance | |
// luckily, scalaz provides both |
OlderNewer