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 scala.xml.pull._ | |
import scala.xml.UnprefixedAttribute | |
import scala.collection.mutable.ListBuffer | |
import scala.collection.mutable.Stack | |
import scala.io.Source | |
class Joint(name:String) { | |
val joints = ListBuffer[Joint]() | |
def addJoint(joint:Joint) { | |
joints += joint |
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
def yieldClass2(clazz:Class[_]): Iterator[Class[_]] = { | |
import scala.collection.{Iterator => I} | |
clazz match { | |
case null => I.empty | |
case _ => | |
val interfaces = clazz.getInterfaces map { yieldClass2 } reduceLeft (_ ++ _) | |
I.single(clazz) ++ yieldClass2(clazz.getSuperclass) ++ interfaces | |
} | |
} |
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
// The continuation code is from an answer by Rich posted at StackOverflow | |
// http://stackoverflow.com/questions/2201882/implementing-yield-yield-return-using-scala-continuations/2215182#2215182 | |
// The printClass example is from this question http://stackoverflow.com/questions/2137619/scala-equivalent-to-python-generators | |
import scala.continuations._ | |
import scala.continuations.ControlContext.{shift,reset} | |
sealed trait Iteration[+R] | |
case class Yield[+R](result: R, next: () => Iteration[R]) extends Iteration[R] |
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
// The continuation code is from an answer by Rich posted at StackOverflow | |
// http://stackoverflow.com/questions/2201882/implementing-yield-yield-return-using-scala-continuations/2215182#2215182 | |
import scala.continuations._ | |
import scala.continuations.ControlContext.{shift,reset} | |
sealed trait Iteration[+R] | |
case class Yield[+R](result: R, next: () => Iteration[R]) extends Iteration[R] | |
case object Done extends Iteration[Nothing] |
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 scala.annotation._ | |
val l = 0 until 30 toList | |
def div(i:Int) = i / 2 | |
def cluster[T,U](xs:Seq[T], f:T => U): Seq[(U, Seq[T])] = { | |
@tailrec | |
def tr_cluster(acc: Seq[(U, Seq[T])], xs:Seq[T]): Seq[(U, Seq[T])] = { | |
if (xs.isEmpty) { |
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
// Borrowed from | |
// http://stackoverflow.com/questions/3102872/what-is-the-fastest-way-to-sum-a-collection-in-scala/3103800#3103800 | |
def time[F](f: => F) = { | |
val t0 = System.nanoTime | |
val ans = f | |
printf("Elapsed: %.3f\n",1e-9*(System.nanoTime-t0)) | |
ans | |
} |
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
/** | |
* An iterator that allows retrieving elements ahead without advancing the iterator. | |
*/ | |
class LookAheadIterator[T](underlying:Iterator[T]) extends BufferedIterator[T] { | |
private val queue = collection.mutable.Queue[T]() | |
/** | |
* Returns a new iterator to read ahead element. | |
* Elements from underlying are queued so that they can be returned by the |
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 scala.util.continuations._ | |
class Generator[A] extends Iterator[A] with (A => Unit @ suspendable) { | |
private var a: Option[A] = None | |
private var k: Option[Unit => Unit] = None | |
def next = { | |
val a0 = a.get | |
val k0 = k.get | |
a = None |
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
// http://stackoverflow.com/questions/3956607/how-should-i-avoid-unintentionally-capturing-the-local-scope-in-function-literals | |
object Clos { | |
def method: Iterator[Int] = { | |
val huge = (1 to 2000000).toList | |
val n = huge.last | |
(1 to n).iterator map (_ + 1) | |
} | |
def gc() { println("GC!!"); Runtime.getRuntime.gc } |
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 -cp "jcl-core-2.2.2.jar;log4j-1.2.16.jar" | |
// http://sourceforge.net/projects/jcloader/files/jcl2/org/xeustechnologies/jcl/2.2.2/jcl-core-2.2.2.jar/download | |
// see http://jcloader.sourceforge.net/gettingstarted.html | |
import org.xeustechnologies.jcl._ | |
import java.lang.reflect.Method | |
// jline2 | |
//val t = new jline.WindowsTerminal | |
//t.getWidth |
OlderNewer