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
scala> :pa
// Entering paste mode (ctrl-D to finish)
def stripNamespaces(node : Node) : Node = {
node match {
case e : Elem =>
e.copy(scope = TopScope, child = e.child map (stripNamespaces))
case _ => node;
}
}
scala> val hist = List(3,2,2).groupBy(identity).mapValues(_.length)
hist: scala.collection.immutable.Map[Int,Int] = Map(2 -> 2, 3 -> 1)
scala> ((hist, List[Int]()) /: List(4, 3, 2, 2, 2, 1)) {
| case ((h, as), a) => h.get(a).map {
| case 0 => (h, a :: as)
| case n => (h + (a -> (n - 1)), as)
| }.getOrElse((h, a :: as)) }
res25: (scala.collection.immutable.Map[Int,Int], List[Int]) = (Map(2 -> 0, 3 -> 0),List(1, 2, 4))
trait Fresh[N] {
def fresh:N
}
class Term[N](implicit F: Fresh[N]) {
def foo = "made a " + F.fresh
}
val ti = new Term[Int] // won't compile
class Foo {
def bar = {
def baz(n:Int) = n + 1
baz(1)
}
}
becomes
trait Ref[A] extends java.io.Externalizable
trait Binary[A]
def serializable[A <: java.io.Serializable]:Binary[A] = null
implicit def ref[A]: Binary[Ref[A]] = serializable[Ref[A]]
implicitly[Binary[Ref[Int]]]
class Curry {
def foo(n:Int, s:String) = false
def bar(n:Int)(s:String) = false
}
becomes
cookie:crap rnorris$ javap -classpath bin Curry
object WAT extends App {
object Foo {
def !:(n: => Any) = "foo"
}
lazy val a = { println("AAA"); 1 }
lazy val b = { println("BBB"); 1 }
Foo.`!:`(a) // ok
trait Foo[A]
implicit val c: Foo[Int] = ???
implicit val d: Foo[String] = ???
def bar[A: Foo]: A = ???
bar: Int // compiles only if Foo is covariant in A ... why?
package org.tpolecat.basic
object Test extends App {
val count = new BASIC {
10 FOR I IN 1 TO 2
15 FOR J IN 1 TO 3
20 GOSUB 100
25 NEXT J
import scala.annotation.tailrec
object FirstDup extends App {
def unfold[A, B](b: B)(f: B => Option[(A, B)]): Stream[A] = f(b) match {
case None => Stream.empty
case Some((a, b)) => a #:: unfold(b)(f)
}
def prefix[A](as: Traversable[A]): Stream[A] =