Skip to content

Instantly share code, notes, and snippets.

View yuroyoro's full-sized avatar
🍣
🍣

しいたけ yuroyoro

🍣
🍣
View GitHub Profile
@yuroyoro
yuroyoro / DelayedInitProblem.scala
Created May 17, 2011 03:11
DelayedInitを継承したクラスでは初期化ブロック内からコンストラクタ引数を参照できないんだけど、これはどうにかして欲しい。
scala> class Foo(i:Int) extends DelayedInit{
| println("aaaaaa:" + i )
| val d = new java.util.Date
| println( d )
|
| def delayedInit(x: => Unit){
| println("delayed init")
| println("-" * 80)
| x
@yuroyoro
yuroyoro / TypedIterableProxy.scala
Created May 18, 2011 05:58
特定の型のIterableへProxyするクラス
package com.yuroyoro.util.collection
import scala.collection.{Iterable, IterableLike}
trait TypedIterableProxy[A, Repr<: Iterable[A]] extends Iterable[A] with IterableLike[A, Repr]{
import scala.collection.generic.CanBuildFrom
import scala.collection.mutable.{ListBuffer, Builder}
val self:Iterable[A]
def newTo(from:Iterable[A]):Repr
def iterator = self.iterator
# Sleep sortに対抗してアホなソートアルゴリズム作った。
# いちおうsleepはしない
# 負の数が含まれていると対応できない(これはちょっと改良すればできるが)
module ArrayIndexSort
def self.sort(xs)
Array.new(xs.max + 1){Array.new()}.tap{|ys| xs.each{|x| ys[x] << x}}.flatten!
end
end
p ArrayIndexSort.sort([23, 1, 53, 3, 1, 3, 6, 13, 3, 6, 1, 3, 7, 1, 3, 6, 1, 3, 7, 133, 63, 137])
@yuroyoro
yuroyoro / Eval.scala
Created May 23, 2011 10:28
Scalaでevalする。2.7の頃よりだいぶ楽になった。遅いけど、な。
import scala.tools.nsc.Settings
import java.io.{PrintWriter, ByteArrayOutputStream, FileOutputStream}
import scala.tools.nsc.interpreter.{ IMain, Results => IR }
object Eval extends App{
val settings = new Settings
settings.usejavacp.value = true
// interpret one line
@yuroyoro
yuroyoro / MyREPL.scala
Created May 23, 2011 10:50
REPLを組み込む方法。
import scala.tools.nsc.interpreter._
import scala.tools.nsc.Settings
import java.io.{PrintWriter, FileOutputStream}
object MyREPL extends App{
val settings = new Settings
val out = new PrintWriter(new FileOutputStream("output.txt"))
val iLoop = new ILoop(Console.in, out)
iLoop process settings
}
@yuroyoro
yuroyoro / Evaluator.scala
Created May 23, 2011 11:19
evaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaallllllllllllllllllllllllllllll
import scala.tools.nsc.Settings
import java.io.{PrintWriter, ByteArrayOutputStream, FileOutputStream}
case class EvalException (msg: String, cause:Throwable = null) extends
RuntimeException(msg, cause)
class Evaluator( val settings:Settings = new Settings ){
import scala.tools.nsc.interpreter.{ IMain, Results => IR }
settings.usejavacp.value = true
@yuroyoro
yuroyoro / OptManifestAntNoManifest.scala
Created May 26, 2011 04:47
OptManifest[A]とかNoManifestとかの話をしよう
/*
OptManifest[A]とかNoManifestとかの話をしよう
OptManifest[A]ってのは、Manifestがあるかないか分からない場合に使う。
値があるかないかをOption[A]で表すように、
型パラメータAに対するManifestがあるかないかわからない場合はOptManifest[A]をつかう
Manifestの継承関係は以下の通り。OptManifestは全てのManifestの親traitになっている。
trait OptManifest [+T]
object NoManifest extends OptManifest[Nothing]
/**
* This library can be used to color/uncolor strings using ANSI escape sequences.
*
* To implementation, referenced Ruby's "Term::ANSIColor"(http://term-ansicolor.rubyforge.org)
*
* NOTE
* this version's implementation is using Dynamic trait and applyDynamic.
* "applyDynamic" is not supported yet.
* to enable this feature, you should specify -Xexperimental option when running scala/scalac command.
*
@yuroyoro
yuroyoro / retry.scala
Created May 27, 2011 17:17
retry機能着きLoan pattern
scala> def retry[T](p: => Boolean)(f: => T):T =
| try{ f } catch { case e => if(p) retry(p)(f) else throw e }
retry: [T](p: => Boolean)(f: => T)T
scala> retry( util.Random.nextInt(3) == 0 ){ println("trying..."); "a".toInt }
trying...
java.lang.NumberFormatException: For input string: "a"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:449)
@yuroyoro
yuroyoro / Home2Lang.scala
Created June 1, 2011 06:05
プログラミング言語「ほむほむ」
import java.io.File
import scala.io.Source
import scala.util.matching.Regex
import scala.util.parsing.combinator._
import scala.util.parsing.input.{Position, NoPosition}
sealed abstract class Insn extends ( CED => CED ){
val pos:Position
}
case class App( m:Int, n:Int, pos:Position ) extends Insn{