Skip to content

Instantly share code, notes, and snippets.

@akihiro4chawon
akihiro4chawon / maze_spanning.scala
Created April 11, 2011 12:34
deve68さんの解答(往路と復路に相当する全域木を重ねる手法)をscalaでやってみた。
// お題:一方通行を許可した迷路を作成
// <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
@akihiro4chawon
akihiro4chawon / multiprintf.scala
Created April 11, 2011 14:22
複数行に渡る printf
// お題:複数行にわたる 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),
@akihiro4chawon
akihiro4chawon / fibimpl.scala
Created April 18, 2011 14:21
O(log(N)) な フィボナッチ数の計算方法ベンチマーク
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._
@akihiro4chawon
akihiro4chawon / linear_algebra_view.scala
Created April 18, 2011 15:15
Binet公式 == 対角化;また、逐次平方変換も行列表記すると分かりやすい。
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]
@akihiro4chawon
akihiro4chawon / fib_logn.groovy
Created April 18, 2011 17:26
Groovy way to compute fibonacci number in O(log N)
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])
}
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 {
@akihiro4chawon
akihiro4chawon / cyclical.scala
Created April 24, 2011 05:44
Hamming's Problem in scala
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
}
@akihiro4chawon
akihiro4chawon / Main.scala
Created April 29, 2011 17:10
直角三角形
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) = {
@akihiro4chawon
akihiro4chawon / Memoize.scala
Created May 5, 2011 13:54
関数の自動メモ化 (>scala 2.9.0)
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] =
@akihiro4chawon
akihiro4chawon / PiParallel.scala
Created May 15, 2011 06:36
scala.collection.parallel implementations of Akka's Pi Tutorial
// 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;