Mark
Mark

Reputation: 1663

F# list find item index

So I've got 2 lists. 1 is a list booleans, the other is a list of data. Is there a way of getting the index for every false value in the boolean list, so I can then use those index's to find values in my list of data?

Upvotes: 3

Views: 2637

Answers (2)

TheQuickBrownFox
TheQuickBrownFox

Reputation: 10624

The List.choose function does a map and filter in one pass, and so avoids creating one of the intermediate lists created by Bartek's answer:

List.zip booleans values
|> List.choose (function (false, v) -> Some v | _ -> None)

If only there was a List.choose2, then we wouldn't need to zip either!

Upvotes: 8

Bartek Kobyłecki
Bartek Kobyłecki

Reputation: 2395

If your list are the same length you can zip them (make a new list that contains pairs of (switch * value) and then filter that new list grabbing only those pairs that have false in the first element.

List.zip booleans values
|> List.filter (fst >> not)
// grab only values
|> List.map snd

Upvotes: 9

Related Questions