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 { | |
private def foo { | |
Foo.foo | |
} | |
} | |
object Foo { | |
private def foo { | |
println("foo") | |
} | |
} |
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
def sum[T:Numeric](xs: List[T]): T = { | |
val ev = implicitly[Numeric[T]] | |
xs.foldLeft(ev.zero){(acc, e) => ev.plus(acc, e)} | |
} |
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
def map[B](f: A => B): Iterator[B] = new Iterator[B] { | |
def hasNext = Iterator.this.hasNext | |
def next() = f(Iterator.this.next) | |
} |
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.continuations.ControlContext._ | |
import scala.continuations._ | |
import scala.collection._ | |
/* | |
* C#形式のイテレータを表現したトレイト | |
*/ | |
trait Generator[+A] extends Traversable[A] { | |
def moveNext(): Boolean | |
def current: A |
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 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 | |
} |
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 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) | |
} | |
*/ |
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 RightUsageOfStream { | |
def main(args: Array[String]) { | |
//Stream.continuallyで作成したStreamのheadをローカル変数で | |
//参照していないので入力サイズに比例したメモリ領域を食わない | |
//ただし、readLine()を生で使って巨大ファイル読むと時間かかるので注意 | |
Stream.continually(readLine()) foreach {line => | |
//nothing to do | |
} | |
//OutOfMemoryErrorで死ぬ例(標準入力から読み込んだサイズが大きい場合) | |
/* |
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 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) |
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 foo | |
object clas { | |
def main(args: Array[String]) { | |
println("Hello") | |
} | |
} |
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 foo | |
object `class` { | |
def main(args: Array[String]) { | |
println("foo") | |
} | |
} |