This file contains hidden or 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 stripNamespaces(node : Node) : Node = { | |
node match { | |
case e : Elem => | |
e.copy(scope = TopScope, child = e.child map (stripNamespaces)) | |
case _ => node; | |
} | |
} |
This file contains hidden or 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 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)) |
This file contains hidden or 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 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 |
This file contains hidden or 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
class Foo { | |
def bar = { | |
def baz(n:Int) = n + 1 | |
baz(1) | |
} | |
} | |
becomes |
This file contains hidden or 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 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]]] |
This file contains hidden or 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
class Curry { | |
def foo(n:Int, s:String) = false | |
def bar(n:Int)(s:String) = false | |
} | |
becomes | |
cookie:crap rnorris$ javap -classpath bin Curry |
This file contains hidden or 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 WAT extends App { | |
object Foo { | |
def !:(n: => Any) = "foo" | |
} | |
lazy val a = { println("AAA"); 1 } | |
lazy val b = { println("BBB"); 1 } | |
Foo.`!:`(a) // ok |
This file contains hidden or 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 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? |
This file contains hidden or 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 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 |
This file contains hidden or 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 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] = |