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._ | |
import scalaz._ | |
scala> import Scalaz._ | |
import Scalaz._ | |
scala> ∅[Boolean] | |
res0: Boolean = false |
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
// Ref: http://blog.xebia.com/2011/08/comparing-apples-to-pears-in-scala-or-abstract-types-to-the-rescue/ | |
scala> trait Money[C] { | |
| def amount(currency: C): Double | |
| } | |
defined trait Money | |
scala> case class Euro(amount: Double) | |
defined class Euro |
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
data Coord = Coord { x :: Double, y :: Double } | |
instance FromJSON Coord where | |
parseJSON (Object v) = | |
Coord <$> | |
v .: "x" <*> | |
v .: "y" |
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
// Ref: http://solog.co/scala/10-scala-one-liners-to-impress-your-friends.html | |
import static fj.Ord.intOrd; | |
import static fj.P.p; | |
import static fj.Show.booleanShow; | |
import static fj.Show.intShow; | |
import static fj.Show.listShow; | |
import static fj.Show.stringShow; | |
import static fj.data.List.list; |
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 collection.generic.{GenericTraversableTemplate => GTT} | |
import collection.generic.{GenericTraversableTemplate=>GTT} | |
scala> import collection.generic.{TraversableFactory => TF} | |
import collection.generic.{TraversableFactory=>TF} | |
scala> def fill[A, CC[X] <: Traversable[X] with GTT[X, CC]] | |
| (n: Int)(elem: => A)(tf: TF[CC]) = tf.fill(n)(elem) | |
fill: [A, CC[X] <: Traversable[X] with scala.collection.generic.GenericTraversab | |
leTemplate[X,CC]](n: Int)(elem: => A)(tf: scala.collection.generic.TraversableFa |
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> def fill[A, CC[_]](n: Int)(elem: => A)(implicit cbf: | |
| CanBuildFrom[Nothing, A, CC[A]]) = { | |
| val b = cbf() | |
| for(_ <- 1 to n) | |
| b += elem | |
| b.result | |
| } | |
fill: [A, CC[_]](n: Int)(elem: => A)(implicit cbf: scala.collection.generic.CanB | |
uildFrom[Nothing,A,CC[A]])CC[A] |
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 actors.Actor | |
case class Letter(sender: Actor, contents: String) | |
object Indira extends Actor { | |
private var n = 0 | |
def act: Unit = loopWhile(n < 10) { | |
n += 1 | |
react { |
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._ | |
import Scalaz._ | |
import effects._ | |
object ExperimentingWithIo { | |
def main(args: Array[String]): Unit = { | |
val i1: IO[Unit] = for { | |
_ <- putStrLn("What's your name?") |
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 inspect | |
def call(fun, **myArgs): | |
names, _, _, values = inspect.getargspec(builder) | |
defaults = zip(names, values) | |
valuesToCallWith = dict(defaults + myArgs.items()) | |
return fun(**valuesToCallWith) | |
def builder(a = 0, b = 2, c = 3): | |
return a + b + 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
scala> val z = zipper(Stream(9, 11), 7, Stream(23, 9)) | |
z: scalaz.Zipper[Int] = <zipper> | |
scala> z.shows | |
res164: String = [11,9] 7 [23,9] | |
scala> val z0 = z.tryNext | |
z0: scalaz.Zipper[Int] = <zipper> | |
scala> z0.shows |
OlderNewer