Skip to content

Instantly share code, notes, and snippets.

View kmizu's full-sized avatar

Kota Mizushima kmizu

View GitHub Profile
@kmizu
kmizu / Foo.scala
Created July 10, 2011 07:36
A case that "private" method of Scala is compiled into "public" method of class file.
class Foo {
private def foo {
Foo.foo
}
}
object Foo {
private def foo {
println("foo")
}
}
@kmizu
kmizu / Sum.scala
Created July 24, 2011 08:22
An example of context bounds.
def sum[T:Numeric](xs: List[T]): T = {
val ev = implicitly[Numeric[T]]
xs.foldLeft(ev.zero){(acc, e) => ev.plus(acc, e)}
}
@kmizu
kmizu / Iterator_map_2_7_7.scala
Created July 24, 2011 09:35
Iterator#map (Scala 2.8.0.final and Scala 2.7.7.final)
def map[B](f: A => B): Iterator[B] = new Iterator[B] {
def hasNext = Iterator.this.hasNext
def next() = f(Iterator.this.next)
}
@kmizu
kmizu / Generator.scala
Created July 24, 2011 13:07
An example of generator library. This is used for explaining shift/reset and this code cannot be compiled Because @susp is eliminated.
import scala.continuations.ControlContext._
import scala.continuations._
import scala.collection._
/*
* C#形式のイテレータを表現したトレイト
*/
trait Generator[+A] extends Traversable[A] {
def moveNext(): Boolean
def current: A
@kmizu
kmizu / BinTreeIterator.scala
Created July 24, 2011 14:57
Create Iterator which traverse binary tree nodes.
object BinTreeIterator {
sealed trait Tree
case class Node(v: Int, l: Tree, r: Tree) extends Tree
case object Leaf extends Tree
def toIterator(node: Tree): Iterator[Int] = {
def toStream(n: Tree): Stream[Int] = n match {
case Node(v, l, r) => v #:: toStream(l) #::: toStream(r)
case Leaf => Stream.empty
}
@kmizu
kmizu / StreamProblem.scala
Created July 24, 2011 23:37
An example program about a problem in Stream.
object StreamProblem {
def main(args: Array[String]) {
/*
val large = Stream.continually(new Array[Byte](10000000))
//Streamのheadをlargeで参照したままになっているので、
//OutOfMemoryErrorで死ぬ
large take 100 foreach {bytes=>
println(bytes)
}
*/
@kmizu
kmizu / RightUsageOfStream.scala
Created July 24, 2011 23:58
Stream usage not to consume O(n) memory.
object RightUsageOfStream {
def main(args: Array[String]) {
//Stream.continuallyで作成したStreamのheadをローカル変数で
//参照していないので入力サイズに比例したメモリ領域を食わない
//ただし、readLine()を生で使って巨大ファイル読むと時間かかるので注意
Stream.continually(readLine()) foreach {line =>
//nothing to do
}
//OutOfMemoryErrorで死ぬ例(標準入力から読み込んだサイズが大きい場合)
/*
@kmizu
kmizu / BinTreeIteratorBenchmark.scala
Created July 25, 2011 02:41
Benchmark for comparing between Iterator and Stream. Because StreamIterator has bug, I created fixed version of StreamIterator.
object BinTreeIteratorBenchmark {
final class BugFixedStreamIterator[A] (private[this] var self: Stream[A]) extends Iterator[A] {
// A call-by-need cell.
class LazyCell(st: => Stream[A]) {
lazy val v = st
}
private var these = {
val stream = self
val result = new LazyCell(stream)
@kmizu
kmizu / clas.scala
Created July 31, 2011 04:54
This program compiles and is executable as scala foo.clas
package foo
object clas {
def main(args: Array[String]) {
println("Hello")
}
}
@kmizu
kmizu / foo.scala
Created July 31, 2011 05:04
This program compiles and is executable as scala foo.class
package foo
object `class` {
def main(args: Array[String]) {
println("foo")
}
}