Last active
October 21, 2015 22:48
-
-
Save non/99bab21d235fc3f5dff6 to your computer and use it in GitHub Desktop.
This file contains 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
package zg | |
import spire.random.Generator | |
/** | |
* ImGen is an immutable RNG. | |
* | |
* It "hides" Generator's mutability through next. | |
*/ | |
class ImGen[G <: Generator](gen: G) { | |
def next[A](f: G => A): (ImGen[G], A) = { | |
val gen2 = gen.copy | |
val a = f(gen2) | |
(new ImGen(gen2), a) | |
} | |
def nextInt: (ImGen[G], Int) = next(_.nextInt) | |
// and so on... | |
} | |
// alternately, we can just allow any Generator to be wrapped | |
class ImGen2(gen: Generator) { | |
def next[A](f: Generator => A): (ImGen2, A) = { | |
val gen2 = gen.copy | |
val a = f(gen2) | |
(new ImGen2(gen2), a) | |
} | |
def nextInt: (ImGen2, Int) = next(_.nextInt) | |
// and so on... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks Erik - this was actually a drop-in replacement for the methods defined at these signatures (on the companion)