F# transforming and aggregating a list of lists -
i have following set of data
let results = [[true;false;true];[true;true;false];[false;false;false]]
i want turn into
let summary = [2;1;1]
is can done of box? thinking list.collect can't work.
thanks in advance
based on sample, suppose want sum number of true
values in 1st, 2nd, 3rd etc. elements of input lists, respectively.
one way turn list of booleans list of numbers containing ones or zeros , aggregate lists. so, input, list numbers be:
[[1; 0; 1]; [1; 1; 0]; [0; 0; 0]]
this can using nested list.map
:
results |> list.map (list.map (fun b -> if b 1 else 0))
now need zip lists , add corresponding numbers. given first 2 lists, can using list.map2
follows:
list.map2 (+) [1; 0; 1] [1; 1; 0] = [2; 1; 1]
the whole thing can written single nice pipeline using partial application:
results |> list.map (list.map (fun b -> if b 1 else 0)) |> list.reduce (list.map2 (+))
Comments
Post a Comment