Skip to content

Instantly share code, notes, and snippets.

@ryoppy
Created January 13, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryoppy/c5c1fb036ee5a092b3e1 to your computer and use it in GitHub Desktop.
Save ryoppy/c5c1fb036ee5a092b3e1 to your computer and use it in GitHub Desktop.
めも
// stream
// ======
locally {
val a: Stream[Int] = unfold(10) { x => if (x == 0) none else (x, x - 1).some }
assert(a.toList === List(10,9,8,7,6,5,4,3,2,1))
}
// Memo
// メモ化
// ======
locally {
val f = { x: Int => Thread.sleep(3000); x + 1 }
// collection.mutable.HashMapが使われる
val m1 = Memo.mutableHashMapMemo[Int, Int](f)
println(m1(1))
println(m1(1)) // すぐに結果が返る
// collection.immutable.HashMapが使われる(varで更新)
// ほかにもListMap, TreeMapがある
val m2 = Memo.mutableHashMapMemo[Int, Int](f)
println(m2(1))
println(m2(1)) // すぐに結果が返る
// Array
// arrayMemoはClassManifestを使っているのでスルーする
// doubleArrayMemo
// Int => Double用
val m3 = Memo.doubleArrayMemo(10) { x: Int => (x * 100).toDouble }
m3(1)
m3(1)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment