Skip to content

Instantly share code, notes, and snippets.

View tpolecat's full-sized avatar
🔥
Dijkstra would not have liked this.

Rob Norris tpolecat

🔥
Dijkstra would not have liked this.
View GitHub Profile

Hi, looking for scalac flags?

This gist has been upgraded to a blog post here.

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)
}
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)
> 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
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
^
@tpolecat
tpolecat / infset.scala
Created December 22, 2013 17:29 — forked from non/infset.scala
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]]
@tpolecat
tpolecat / gist:7554206
Last active December 28, 2015 19:59
Having a bit of a problem deriving implicit instances for product types.
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
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)
// 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]
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"
)