This gist has been upgraded to a blog post here.
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
sealed trait MaybeNumeric[A] | |
case class YesNumeric[A](n: Numeric[A]) extends MaybeNumeric[A] | |
case class NoNumeric[A]() extends MaybeNumeric[A] | |
object MaybeNumeric { | |
implicit def instance[A](implicit ev: Numeric[A]): MaybeNumeric[A] = | |
YesNumeric(ev) | |
} |
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
implicit def treeEncode[A: EncodeJson]: EncodeJson[Tree[A]] = | |
EncodeJson(t => (t.rootLabel, t.subForest).asJson) | |
implicit def treeDecode[A: DecodeJson]: DecodeJson[Tree[A]] = | |
DecodeJson(c => | |
for { | |
rootLabel <- (c =\ 0).as[A] | |
subForest <- (c =\ 1).as[Stream[Tree[A]]] | |
} yield Tree.node(rootLabel, subForest) |
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
> runMain doobie.hi.Test | |
[info] Compiling 37 Scala sources to /Users/rnorris/Scala/doobie/target/scala-2.10/classes... | |
[info] Running doobie.hi.Test | |
The answer was: java.lang.RuntimeException: Bogus country: NCL | |
nyQL log for hi.examples | |
+- [ok] getConnection(jdbc:h2:mem:test;DB_CLOSE_DELAY=-1, sa, ***) | conn1: url=jdbc:h2:mem:test us 304.109 ms | |
`- [ex] database session | Bogus country: NCL 1496.911 ms | |
+- [ok] populate database from /Users/rnorris/Scala/doobie/world.sql | false 1378.663 ms | |
| +- [ok] prepareStatement(RUNSCRIPT FROM ? CHARSET 'UTF-8') | prep1: RUNSCRIPT FROM ? CHARSE 3.950 ms |
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> class Foo(val n: Int) | |
defined class Foo | |
scala> class Bar(n: Int) extends Foo(n) | |
defined class Bar | |
scala> List[Foo]().sorted | |
<console>:9: error: No implicit Ordering defined for Foo. | |
List[Foo]().sorted | |
^ |
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 spire.examples | |
import spire.algebra._ | |
import spire.math.{Natural, UInt} | |
import scala.collection.mutable | |
object SetUtil { | |
def powerStream[A](members: Stream[A]): Stream[Set[A]] = { | |
val done = Stream.empty[Set[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 shapeless._ | |
// typeclass for total number of fields (just an example) | |
class Width[A](val toInt: Int) | |
object Width { | |
// Get an instance | |
implicit def apply[A](implicit A: Width[A]) = 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
scala> :pa | |
// Entering paste mode (ctrl-D to finish) | |
def subsets[A](as: List[A]): List[List[A]] = { | |
def sub0(accum: List[List[A]], as: List[A]): List[List[A]] = | |
as match { | |
case Nil => accum | |
case a :: as => sub0(accum ++ accum.map(a :: _), as) | |
} | |
sub0(List(Nil), as) |
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
// 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 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" | |
) |