Skip to content

Instantly share code, notes, and snippets.

@seratch
Created May 12, 2011 16:30
Show Gist options
  • Save seratch/968871 to your computer and use it in GitHub Desktop.
Save seratch/968871 to your computer and use it in GitHub Desktop.
Scala 2.9.0.final Changesの確認
// ----------------------
// Scala 2.9.0.final
// http://www.scala-lang.org/node/43
// ----------------------
// REPLのexitはdeprecated
scala> exit
<console>:8: warning: method exit in object Predef is deprecated: Use sys.exit() instead
exit
^
// ----------------------
// Parallel Collections
// - bulkっぽい処理をやるやつ。
// これまでのコレクション型
Range(0,100,1) foreach { i => print(i+",") }
// 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,
// 31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,
// 61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,
// 91,92,93,94,95,96,97,98,99,
// 従来のコレクション型にparメソッドが追加された
Range(0,100,1).par foreach { i => print(i+",") }
// 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,
// 31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,75,76,77,78,79,80,81,82,83,84,85,
// 86,87,88,89,90,91,92,93,94,95,96,97,98,99,62,63,64,65,66,67,68,69,70,71,72,73,74,56,57,58,
// 59,60,61,50,51,52,53,54,55,
// parallelパッケージ
import collection.parallel.immutable._
ParRange(0,100,1,false) foreach { i => print(i+",") }
// 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,
// 31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,75,76,77,78,79,80,81,82,83,84,85,
// 86,87,88,89,90,91,92,93,94,95,96,97,98,99,62,63,64,65,66,67,68,69,70,71,72,73,74,56,57,58,
// 59,60,61,50,51,52,53,54,55,
// スレッドID出力してみた
scala> Range(0,20,1).par foreach { i => print(Thread.currentThread.getId+",") }
20,20,20,20,20,20,20,20,20,20,19,19,19,19,19,21,21,21,21,21,
scala> Range(0,20,1).par foreach { i => print(Thread.currentThread.getId+",") }
20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,
scala> Range(0,20,1).par foreach { i => print(Thread.currentThread.getId+",") }
21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,
scala> Range(0,20,1).par foreach { i => print(Thread.currentThread.getId+",") }
20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,21,21,
scala>
// ------------------------
// Application traitがdeprecatedに -> 新しいApp trait使う
// ※Application traitはスレッドセーフでなかったため
scala> class Main extends Application {
| println("hogehoge")
| }
<console>:7: warning: trait Application is deprecated: use App instead
class Main extends Application {
^
defined class Main
scala> class Main extends App {
| println("hogehoge")
| }
defined class Main
// ------------------------
// DelayedInit trait
// - インスタンス化のタイミングで呼ばれる、という認識でよいのだろうか・・
object Status extends Enumeration {
val Todo = Value("Todo")
val Doing = Value("Doing")
val Done = Value("Done")
}
class CurrentStatus extends DelayedInit {
var status: Option[Status.Value] = None
override def delayedInit(x: => Unit): Unit = {
println("delayed init...")
status = Some(Status.Todo)
}
}
val current = new CurrentStatus // delayed init...
// ------------------------
// Repl Improvements
scala> :imports
1) import java.lang._ (153 types, 158 terms)
2) import scala._ (797 types, 805 terms)
3) import scala.Predef._ (16 types, 167 terms, 96 are implicit)
scala> :implicits
No implicits have been imported other than those in Predef.
scala> :keybindings
Reading jline properties for default key bindings.
Accuracy not guaranteed: treat this as a guideline only.
1 CTRL-A: move to the beginning of the line
2 CTRL-B: move to the previous character
4 CTRL-D: close out the input stream
....
scala> case class Sample(val name: String)
defined class Sample
scala> :javap Sample
Compiled from "<console>"
public class Sample extends java.lang.Object implements scala.ScalaObject,scala.Product,scala.Serializable{
public scala.collection.Iterator productIterator();
public scala.collection.Iterator productElements();
public java.lang.String name();
public Sample copy(java.lang.String);
public java.lang.String copy$default$1();
public int hashCode();
public java.lang.String toString();
public boolean equals(java.lang.Object);
public java.lang.String productPrefix();
public int productArity();
public java.lang.Object productElement(int);
public boolean canEqual(java.lang.Object);
public Sample(java.lang.String);
}
// ------------------------
// Scala Runner
// - scala -saveでjarファイルがつくられたりする
$ cat sample.scala
case class Name(val first:String, val last:String)
val name = Name("Kazuhiro", "Sera")
println(name)
$ scala -save sample.scala
Name(Kazuhiro,Sera)
$ ls
sample.jar sample.scala
$ scala sample.jar
Name(Kazuhiro,Sera)
$
// ------------------------
// Generalized try-catch-finally:
val handler: PartialFunction[Throwable,Unit] = {
case t @ (_: RuntimeException) => t.printStackTrace
}
val cleanup = () => println("finally")
def main() {
try throw new RuntimeException
catch handler
finally cleanup()
}
main
/*
scala> main
java.lang.RuntimeException
at $line66.$read$$iw$$iw$$iw$$iw$.main(<console>:14)
at $line67.$read$$iw$$iw$$iw$$iw$.<init>(<console>:15)
at $line67.$read$$iw$$iw$$iw$$iw$.<clinit>(<console>)
at $line67.$eval$.<init>(<console>:11)
at $line67.$eval$.<clinit>(<console>)
at $line67.$eval.$export(<console>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.call(IMain.scala:592)
at scala.tools.nsc.interpreter.IMain$Request$$anonfun$10.apply(IMain.scala:828)
at scala.tools.nsc.interpreter.Line$$anonfun$1.apply$mcV$sp(Line.scala:43)
at scala.tools.nsc.io.package$$anon$2.run(package.scala:31)
at java.lang.Thread.run(Thread.java:680)
finally
*/
// ------------------------
// scala.sys and scala.sys.process, which are imported from sbt.Process.
http://code.google.com/p/simple-build-tool/wiki/Process
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment