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
// Inspired by http://dave.fayr.am/posts/2012-10-4-finding-fizzbuzz.html | |
object FizzBuzz extends App { | |
import scalaz._ | |
import Scalaz._ | |
def fizzbuzz(i: Int) = ((i % 3 == 0) option "fizz") |+| ((i % 5 == 0) option "buzz") | i.shows | |
for (i <- 1 to 100) {println(i + " " + fizzbuzz(i))} | |
} |
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 stereotypes | |
import shapeless._ | |
import Searchable._ | |
object FunctionThisUp { | |
// pretend we have some case classes - they needn't be ours of course | |
case class Name(name: String) | |
case class Description(description: String) |
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
trait FooComponent { | |
def bar: BarComponent#Bar | |
case class Foo(foofy: String) | |
} | |
trait BarComponent { | |
def foo: FooComponent#Foo |
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
def setL[T, A, B](la: Lens[T, A], lb: Lens[T, B])(a:A, b: B)(t: T): T = la.set(lb.set(t, b), a) | |
def setL[T, A, B, C](la: Lens[T, A], lb: Lens[T, B], lc: Lens[T, C])(a:A, b: B, c: C)(t: T): T = lc.set(setL(la, lb)(a,b)(t), c) | |
def setL[T, A, B, C, D](la: Lens[T, A], lb: Lens[T, B], lc: Lens[T, C], ld: Lens[T, D])(a:A, b: B, c: C, d: D)(t: T): T = ld.set(setL(la, lb, lc)(a,b, c)(t), d) |
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 MonadWriterExample extends App { | |
import scalaz._ | |
import Scalaz._ | |
implicit val monadWriter = EitherT.listenableMonadWriter[Writer, String, String] | |
case class Person(firstName: String, age: Int, car: Option[String]) | |
def notEmpty(s: String) = Option(s).filter(x ⇒ !x.isEmpty && !x.forall(_.isWhitespace)) match { | |
case Some(r) ⇒ monadWriter.right[String](r) |
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 play { | |
import scalaz._ | |
import Scalaz._ | |
def foo(i: Int) = if (i > 0) Some(i) else None | |
def boo(i: Int) = if (i <= 0) Some(0) else None | |
val l = foo _ :: boo _ :: Nil | |
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 PartialLensExample extends App { | |
import scalaz._ | |
import Lens._ | |
import PLens._ | |
case class Bar(blub: Option[String]) | |
case class Foo(bar: Option[Bar]) |
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 Trees extends App { | |
def zero = '0' | |
def repeat(c: Char)(n: Int) = c.toString * n | |
def reflect(centre: Char)(s: String) = s.reverse + centre + s | |
def pad(w: Int, c: Char)(s: String) = s.padTo(w - 1, c) |
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
// ------------------------------------------------------ | |
// Combined List/Option/Either | |
// ------------------------------------------------------ | |
object MonadTransformers extends App { | |
import scalaz._, Scalaz._ | |
import EitherT._ | |
type OptionTList[α] = ({ type λ[α] = OptionT[List, α] })#λ[α] | |
val c1: EitherT[OptionTList, String, String] = eitherT[OptionTList, String, String](OptionT[List, \/[String, String]](List(some(\/-("c1a")), some(\/-("c1b"))))) |
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 reactive | |
import java.util.concurrent.TimeUnit | |
import rx.lang.scala.{Observable, Subject} | |
import scala.concurrent.duration.Duration | |
/** | |
* The problem: can two threads banging away from two sources be merged and have predictable results? |