-
Notifications
You must be signed in to change notification settings - Fork 40
Example: Fast exponentiation, streamingly 🌊 #925
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
| def prettyBits(bits: List[Bool]): String = | ||
| "0b" ++ bits.map { b => if (b) "1" else "0"}.join("") | ||
|
|
||
| def testBits(n: Int) = { | ||
| println(n) | ||
| println(collectList[Bool] {n.eachLSB}.prettyBits) | ||
| println(collectList[Bool] {n.eachMSB}.prettyBits) | ||
| } | ||
|
|
||
| testBits(0) | ||
| testBits(1) | ||
| testBits(2) | ||
| testBits(3) | ||
| testBits(4) | ||
| testBits(123456789) | ||
|
|
||
| println("") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This part is just my sanity check for eachLSB and eachMSB. I'm happy to (re)move it after rebasing on top of #923 :)
| /// Computes n^exp for integers. (from #923!) | ||
| def pow(n: Int, exp: Int): Int = { | ||
| def go(n: Int, exp: Int, acc: Int): Int = { | ||
| if (exp == 0) { | ||
| acc | ||
| } else if (mod(exp, 2) == 0) { | ||
| go(n * n, exp / 2, acc) | ||
| } else { | ||
| go(n * n, exp / 2, acc * n) | ||
| } | ||
| } | ||
| go(n, exp, 1) | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Taken verbatim from #923, I really ought to rebase :)
By the way, as an exercise in algorithms, notice that the pow here and the fastexp above are completely isomorphic, always doing the exact same steps :)
|
TODO(Self): def takeWhile[A] { cond: A => Bool } { stream: => Unit / emit[A] }: Unit / emit[A] =
try stream() with emit[A] {
case value and cond(value) => do emit(value); resume(())
case _ => ()
}
def last[A] { stream: => Unit / emit[A] }: A / Exception[MissingValue] = {
var latest = None()
for[A] {stream} { value =>
latest = Some(value)
}
latest.value
}
def digits(n: Int) = last[Indexed[Int]] {
with index[Int]
with takeWhile { k => k <= n }
iterate(1) { k => k * 10 }
}.index + 1
def main() = {
with on[MissingValue].report
println(digits(12345)) // ~> 5
} |
| def iterate[A](initial: A) { step: A => A }: Unit / emit[A] = { | ||
| var current = initial | ||
| while (true) { | ||
| do emit(current) | ||
| current = step(current) | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice... This looks a lot like unfolding a coalgebra :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, I just wanted to use the Haskell-style name which (weirdly!) is a bit friendlier than unfold
Tiny & fast exponentiation using streams:

The approach is simple: to calculate
n^k, zip squares ofn(n,n^2,n^4, ...) with the bits ofk.Sketch:
Also drags a few other changes along:
productanditerateinstreameachLSBandeachMSB-- these would probably be better off using the toolchain from Stdlib: Binary/Byte stream utilities #923, but I don't want to forget about the exampleI'm adding it because: