Generic.binpow: def of bend-collections-laws-math@1.0.0.0 Import: import bend-collections-laws-math@1.0.0.0/spec/math/generic.bend as Generic Lines 263-271 of bend-collections-laws-math@1.0.0.0/spec/math/generic.bend: https://jend.leeeet.dev/read.txt?path=bend-collections-laws-math@1.0.0.0/spec/math/generic.bend&start_line=263&end_line=271 import Base import bend-collections-laws-math@1.0.0.0/src/math/num.bend as N # right-to-left binary exponentiation: acc *= base on a 1 bit, base *= base # while bits remain, each product rounded (k = 0 is a fixed point, so any # fuel past k's bit length gives the same result) def binpow(~T: Data, ~op: N.Op -> T, fuel: Nat, +k: Nat, +base: T, +acc: T) -> T: match fuel: case 0n: acc case 1n+f: binpow(~T, ~op, f, Nat.div(k, 2n), pick(T, Nat.is_lt(1n, k), op(N.Mul{base, base}), base), pick(T, Nat.is_eq(Nat.mod(k, 2n), 1n), op(N.Mul{acc, base}), acc)) Uses (7): https://jend.leeeet.dev/definition.txt?path=Base/base.bend&name=Nat https://jend.leeeet.dev/definition.txt?path=Base/base.bend&name=Nat.div https://jend.leeeet.dev/definition.txt?path=Base/base.bend&name=Nat.is_eq https://jend.leeeet.dev/definition.txt?path=Base/base.bend&name=Nat.is_lt https://jend.leeeet.dev/definition.txt?path=Base/base.bend&name=Nat.mod https://jend.leeeet.dev/definition.txt?path=bend-collections-laws-math@1.0.0.0/spec/math/generic.bend&name=pick https://jend.leeeet.dev/definition.txt?path=bend-collections-laws-math@1.0.0.0/src/math/num.bend&name=Op