F# I want to filter my output -


i have problem have simplified purpose of question.
let's have 2 lists. first actually represents list of classes but purpose, let's represents list of ints (2,4,6,8,10). have list of ints represents flags, indicating if wish include/exclude corresponding values first set.
(it's not best example should suffice helping me solve real problem.)

let set1 = [2;4;6;8;10] let set2 = [1;0;0;1;1] 

my desired output set is:

[2;8;10] 

this code:

let solution =      list.map2 (fun b ->          match b         | 1 ->          | _ -> 0     ) set1 set2 

this renders following output:

val solution : int list = [2; 0; 0; 8; 10] 

how can filter out these undesired zeros?
instead of | _ -> 0 ideally want return null , filter out nulls.

your appreciated!

this 1 seems reasonable easy:

let filterwith set2 set1 =     list.zip set1 set2      |> list.filter (fun (_,x) -> x=1)      |> list.map fst 

usage:

let set1 = [2;4;6;8;10] let set2 = [1;0;0;1;1]  set1 |> filterwith set1 

if choose use list of bools set2 bit nicer:

let filterwith set2 set1 =     list.zip set1 set2      |> list.filter snd     |> list.map fst 

usage:

let set1 = [2;4;6;8;10] let set2 = [true;false;false;true;true]  set1 |> filterwith set1 

Comments

Popular posts from this blog

javascript - oscilloscope of speaker input stops rendering after a few seconds -

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -