Skip to content

Instantly share code, notes, and snippets.

@fkmt-disk
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fkmt-disk/85048538f14f6503a729 to your computer and use it in GitHub Desktop.
Save fkmt-disk/85048538f14f6503a729 to your computer and use it in GitHub Desktop.
for式はforeach
lazy val root = (project in file(".")).settings(
scalaVersion := "2.11.5",
scalacOptions += "-Xprint:typer"
)
package sample1
object Main extends App {
val seq = Seq(1, 2, 3)
for (i <- seq) println(i)
}
[[syntax trees at end of typer]] // sample1.scala
package sample1 {
object Main extends AnyRef with App {
def <init>(): sample1.Main.type = {
Main.super.<init>();
()
};
private[this] val seq: Seq[Int] = collection.this.Seq.apply[Int](1, 2, 3);
<stable> <accessor> def seq: Seq[Int] = Main.this.seq;
Main.this.seq.foreach[Unit](((i: Int) => scala.this.Predef.println(i)))
}
}
package sample2
object Main extends App {
object Eachable {
import scala.util.Random
private val random = new Random
// ランダムな数値を引数に3回呼び出す。
def foreach(f: (Int) => Unit) = {
f(random.nextInt)
f(random.nextInt)
f(random.nextInt)
}
}
for (i <- Eachable) println(i)
}
[[syntax trees at end of typer]] // sample2.scala
package sample2 {
object Main extends AnyRef with App {
def <init>(): sample2.Main.type = {
Main.super.<init>();
()
};
object Eachable extends scala.AnyRef {
def <init>(): sample2.Main.Eachable.type = {
Eachable.super.<init>();
()
};
import scala.util.Random;
private[this] val random: scala.util.Random = new scala.util.Random();
<stable> <accessor> private def random: scala.util.Random = Eachable.this.random;
def foreach(f: Int => Unit): Unit = {
f.apply(Eachable.this.random.nextInt());
f.apply(Eachable.this.random.nextInt());
f.apply(Eachable.this.random.nextInt())
}
};
Main.this.Eachable.foreach(((i: Int) => scala.this.Predef.println(i)))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment