Skip to content

Instantly share code, notes, and snippets.

@xuwei-k
xuwei-k / gist:966171
Created May 11, 2011 09:21
scala2.8.1のlibrary中でEitherが使用されている箇所
src2.8.1\scala-library-src\scala\actors\Future.scala"
157,12 def awaitEither[A, B >: A](ft1: Future[A], ft2: Future[B]): B = {
src2.8.1\scala-library-src\scala\collection\immutable\List.scala"
549,32 def lefts[A, B](es: Iterable[Either[A, B]]) =
559,33 def rights[A, B](es: Iterable[Either[A, B]]) =
571,34 def separate[A,B](es: Iterable[Either[A, B]]): (List[A], List[B]) =
src2.8.1\scala-library-src\scala\concurrent\ops.scala"
28,40 private def tryCatch[A](body: => A): Either[Throwable, A] =
31,48 private def getOrThrow[T <: Throwable, A](x: Either[T, A]): A =
56,25 val y = new SyncVar[Either[Throwable, B]]
//2.9.0以上じゃないと動かないよーーー
//ぱふ のalias
type -->[A,B] = PartialFunction[A,B]
//Throwableを受け取って、エラーメッセージを返す ぱふ
val getErrorMsg: Throwable --> String = {case e:Throwable => e.getMessage }
//関数合成で、エラーメッセージをプリントする関数作成
val printMsg = getErrorMsg.andThen(print)
@xuwei-k
xuwei-k / gist:968045
Created May 12, 2011 06:30
Scalaでnetstatするだけのもの
//こんなものをいちいちScalaで作っちゃうほどにScala中毒((((;゚Д゚))))
def netstat = {
val p = Runtime.getRuntime.exec("netstat")
val lines = io.Source.fromInputStream( p.getInputStream ).getLines.toSeq
( lines.size , lines )
@xuwei-k
xuwei-k / gist:977994
Created May 18, 2011 04:36
ScalaのREPL上から、Web browser上で指定したソースコードのページ開くスクリプト
object SourceCode{
val browser = """ "C:\Documents and Settings\scalaちゃん\Local Settings\Application Data\Google\Chrome\Application\chrome.exe" """
val srcURL = "https://lampsvn.epfl.ch/trac/scala/browser/scala/tags/R_2_8_1_final/src//library/"
def apply(clazz:Class[_]){
val url = srcURL + clazz.getName.replace('.','/') + ".scala#L1"
@xuwei-k
xuwei-k / gist:982512
Created May 20, 2011 07:45
Scala Sleep Sort
Seq( 5, 3, 6, 3, 6, 3, 1, 4, 7 ).map{ i =>
() => concurrent.ops.spawn{
Thread.sleep(i * 100)
print(i+",")
}
}.foreach(_.apply)
//1,3,3,3,4,5,6,6,7,
@xuwei-k
xuwei-k / gist:988340
Created May 24, 2011 08:35
DRYとか型安全を求めすぎて厨二病的なことになってる感じ
sealed trait ReturnType{
type A
val func:(String => A)
}
object ReturnType{
object IntType extends ReturnType{ type A = Int ; val func = (_:String).toInt }
object LongType extends ReturnType{ type A = Long ; val func = (_:String).toLong }
object StringType extends ReturnType{ type A = String ; val func = identity[String] _ }
}
object Test{
def hoge [@specialized(AnyRef) T](arr: Array[T]){
println(arr)
}
}
@xuwei-k
xuwei-k / gist:992147
Created May 25, 2011 22:31
2.9.0でのOptManifestとか、NoManifestとかClassManifestが使用されている箇所
./scala/Predef.scala: type OptManifest[T] = scala.reflect.OptManifest[T]
./scala/Predef.scala: def optManifest[T](implicit m: OptManifest[T]) = m
./scala/reflect/ClassManifest.scala:trait ClassManifest[T] extends OptManifest[T] with Equals with Serializable {
./scala/reflect/ClassManifest.scala: private def subargs(args1: List[OptManifest[_]], args2: List[OptManifest[_]]) = (args1 corresponds args2) {
./scala/reflect/ClassManifest.scala: def typeArguments: List[OptManifest[_]] = List()
./scala/reflect/ClassManifest.scala: def classType[T <: AnyRef](clazz: JClass[_], arg1: OptManifest[_], args: OptManifest[_]*): ClassManifest[T] =
./scala/reflect/ClassManifest.scala: def classType[T <: AnyRef](prefix: OptManifest[_], clazz: JClass[_], args: OptManifest[_]*): ClassManifest[T] =
./scala/reflect/ClassManifest.scala: def arrayType[T](arg: OptManifest[_]): ClassManifest[Array[T]] = arg match {
./scala/reflect/ClassManifest.scala: def abstractType[T](prefix: OptManifest[_], name: String, clazz: JClass[
Welcome to Scala version 2.9.0.r0-b20110520175005 (Java HotSpot(TM) Client VM, Java 1.6.0_22).
Type in expressions to have them evaluated.
Type :help for more information.
scala> :power
** Power User mode enabled - BEEP BOOP WHIR **
** scala.tools.nsc._ has been imported **
** global._ and definitions._ also imported **
** New vals! Try repl, intp, global, power **
** New cmds! :help to discover them **
@xuwei-k
xuwei-k / gist:992740
Created May 26, 2011 08:00
InterruptedExceptionも他のものも全部Catchするやつ
scala> import scala.util.control.Exception._
import scala.util.control.Exception._
scala> val superCatcher = catchingPromiscuously(classOf[InterruptedException]).or(allCatch)
superCatcher: util.control.Exception.Catch[Nothing] = Catch()
scala> superCatcher.either{ "hoge" toInt }
res0: Either[Throwable,Int] = Left(java.lang.NumberFormatException: For input string: "hoge")
scala> superCatcher.either{ throw new InterruptedException }