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
| // お題:一方通行を許可した迷路を作成 | |
| // <http://d.hatena.ne.jp/aya_eiya/20110329/1301406161> | |
| // | |
| // deve68 さんの解答をそのまま scala に移植。(自分の理解用) | |
| // 出典:<http://d.hatena.ne.jp/deve68/20110410/1302464249> | |
| import java.awt.Dimension | |
| import java.awt.geom.Point2D | |
| import javax.swing.JFrame |
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
| // お題:複数行にわたる printf | |
| object Main extends Application { | |
| def multiPrintf[T <: Product](tuples: T*) = | |
| for (t <- tuples; pi = t.productIterator; fmt = pi.next.asInstanceOf[String]) | |
| println(fmt.format(pi.toSeq :_*)) | |
| multiPrintf( | |
| ("line1: %d", 1), | |
| ("line2: %d, %d", 1, 2), |
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 FibImpModule { | |
| /** リファレンス実装 */ | |
| val fibRef: Stream[BigInt] = | |
| BigInt(0) #:: fibRef.scanLeft(BigInt(1)){_ + _} | |
| /** 逐次平方変換系の実装 */ | |
| // Generic な実装 (しかし、JVMの型消去のため、実力よりずいぶん遅くなったため | |
| // 公正なベンチマークにならないのでボツ。。。) | |
| def fibSqr[IntT](n: Int)(implicit num: Integral[IntT]): IntT = { | |
| import num._ |
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 NumUtils { | |
| // 2x2行列(乗算/冪乗のみ定義) | |
| case class m2d[T](val a: T, val b: T, val c: T, val d: T)(implicit num: Numeric[T]) { | |
| import num._ | |
| def *(that: m2d[T]) = m2d( | |
| this.a * that.a + this.b * that.c, this.a * that.b + this.b * that.d, | |
| this.c * that.a + this.d * that.c, this.c * that.b + this.d * that.d | |
| ) | |
| def pow(exp: Int): m2d[T] = | |
| if (exp == 0) m2d.eye[T] |
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
| fib = [0:0, 1:1].withDefault{ n -> | |
| n % 2 ? (fib[n >> 1] ** 2 + fib[(n >> 1) + 1] ** 2) \ | |
| : ((2 * fib[(n >> 1) - 1] + fib[n >> 1]) * fib[n >> 1]) | |
| } |
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 find2[A](x:A, xs:List[A]):Option[A] = { | |
| if(xs.nonEmpty){ | |
| if(xs.head == x) Some(xs.head) | |
| else find2(x, xs.tail) | |
| } else { | |
| None | |
| } | |
| } | |
| def find3[A](x:A, xs:List[A]):Option[A] = | |
| xs.headOption match { |
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 Main { | |
| def humming: Iterator[BigInt] = { | |
| object ForwardReferenceable { | |
| def output: Iterator[BigInt] = Iterator(BigInt(3), BigInt(4), BigInt(5)) ++ merged | |
| val Seq(result, m2, m3, m5) = tee(output, 4) | |
| val merged = merge(m2 map {_ * 2}, m3 map {_ * 3}, m5 map {_ * 5}) | |
| } | |
| Iterator(BigInt(1), BigInt(2)) ++ ForwardReferenceable.result | |
| } | |
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 Main extends App { | |
| "Method1: " +: Method1.answer take 10 foreach print | |
| println | |
| "Method2: " +: Method2.answer take 10 foreach print | |
| println | |
| assert((Method1.answer take 10000) == (Method2.answer take 10000)) | |
| println("Assertion succeeded") | |
| def time[U](proc: => U) = { |
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 Memoize { | |
| // 1変数関数をメモ化する(2変数以上は tupled/untupledで対応) | |
| def memoize[ArgT, RetT](f: ArgT => RetT): ArgT => RetT = { | |
| val memo = scala.collection.mutable.Map.empty[ArgT, RetT] | |
| arg => memo.getOrElseUpdate(arg, f(arg)) | |
| } | |
| // scala 2.9.0.RC1 or later | |
| def memoize[T1, T2, R](f: Function2[T1, T2, R]): Function2[T1, T2, R] = |
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
| // Akka's Pi Tutorial implemented with scala.collection.parallel | |
| object PiParallel extends App { | |
| import scala.collection.GenSeq | |
| val seqRng = Range(0, 10000 * 10000).view | |
| val parRng = Range(0, 10000 * 10000).par.view | |
| def calculatePi(rng: GenSeq[Int]): Double = | |
| rng.map{i => 4.0 * (1 - (i % 2) * 2) / (2 * i + 1)}.sum; |