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]])
}Install
Download zipData structures and algorithms the MoonBit core library leaves out.