Im trying to make a rust version of this F# snippet:
let sequence list =
let folder (item: _ list) (state: _ list list) =
[ for x in item do
for xs in state -> x::xs ]
List.foldBack folder list [[]]
Which is essentially taking a list of lists and creating a cartesian product of each list, and yes I know I can just use the itertools crate like this:
let x = vec![1, 2, 3];
let y = vec![10, 20, 30];
let z = vec![100, 200, 300];
let all = vec![x, y, z];
let all_combinations = all.iter().multi_cartesian_product().collect::<Vec<_>>();
Which produces the output:
[[1, 10, 100], [1, 10, 200], [1, 10, 300], [1, 20, 100], [1, 20, 200], [1, 20, 300], [1, 30, 100], [1, 30, 200], [1, 30, 300], [2, 10,
100], [2, 10, 200], [2, 10, 300], [2, 20, 100], [2, 20, 200], [2, 20, 300], [2, 30, 100], [2, 30, 200], [2, 30, 300], [3, 10, 100], [
3, 10, 200], [3, 10, 300], [3, 20, 100], [3, 20, 200], [3, 20, 300], [3, 30, 100], [3, 30, 200], [3, 30, 300]]
But I want to do this with the standard library and just fold if possible?