Created
January 13, 2015 14:15
-
-
Save ryoppy/c5c1fb036ee5a092b3e1 to your computer and use it in GitHub Desktop.
めも
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
// 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