moonbitstack/moonstl/algo does not have a README file
test {
let combinations : @algo.Combinations[Int32] = @algo.Combinations::new(
[1, 2, 3, 4],
k=2,
)
@test.assert_eq(combinations.iter().to_array(), [
[1, 2],
[1, 3],
[1, 4],
[2, 3],
[2, 4],
[3, 4],
])
}test {
let with_repeats : @algo.CombinationsWithReplacement[Int32] = @algo.CombinationsWithReplacement::new(
[1, 2],
k=2,
)
@test.assert_eq(with_repeats.iter().to_array(), [[1, 1], [1, 2], [2, 2]])
}fn[T] CombinationsWithReplacement::new(pool : ArrayView[T], k~ : Int) -> CombinationsWithReplacement[T]pub struct GrayCode {
bits : Int
step : Int
done : Bool
}test {
let code : @algo.GrayCode = @algo.GrayCode::new(2)
@test.assert_eq(code.iter().take(4).to_array(), [
[false, false],
[false, true],
[true, true],
[true, false],
])
}test {
let permutations : @algo.Permutations[Int32] = @algo.Permutations::new(
[1, 2, 3],
length=2,
)
@test.assert_eq(permutations.iter().to_array(), [
[1, 2],
[1, 3],
[2, 1],
[2, 3],
[3, 1],
[3, 2],
])
}test {
let powerset : @algo.Powerset[Int32] = @algo.Powerset::new([1, 2])
@test.assert_eq(powerset.iter().to_array(), [[], [1], [2], [1, 2]])
}test {
let pools : Array[ArrayView[Int32]] = [[1, 2][:], [3, 4][:]]
let product : @algo.Product[Int32] = @algo.Product::new(pools)
@test.assert_eq(product.iter().to_array(), [[1, 3], [1, 4], [2, 3], [2, 4]])
}test {
let run : Array[Int32] = [10, 20, 30, 40, 50]
let all = @algo.reservoir_sample(run.iter(), k=9, seed=1UL)
assert_eq(all, [10, 20, 30, 40, 50])
let one = @algo.reservoir_sample(run.iter(), k=2, seed=1UL)
assert_eq(one.length(), 2)
assert_eq(one, @algo.reservoir_sample(run.iter(), k=2, seed=1UL))
}test {
let run : Array[Int32] = [1, 2, 3, 4, 5]
assert_eq(@algo.windows(run[:], 3).to_array(), [
[1, 2, 3],
[2, 3, 4],
[3, 4, 5],
])
assert_eq(@algo.windows(run[:], 5).to_array().length(), 1)
assert_eq(@algo.windows(run[:], 6).to_array().length(), 0)
}Install
Download zipData structures and algorithms the MoonBit core library leaves out.