获取具有特定值的数组元素的索引

Sal*_*ara 1 f#

考虑我有一个类似的选项数组,[|Some 1;Some0;None;None;Some0|] 我将获得具有None值的元素的索引,在这种情况下,正确的答案将是[|2;3|].

我目前的想法是将数组更改为列表然后使用递归函数抛出它但在这种情况下我将需要mutable value计算索引,我不想使用可变?

还有其他解决方案

Gus*_*Gus 7

这是另一个解决方案:

[|Some 1;Some 0;None;None;Some 0|]
    |> Array.indexed
    |> Array.filter (fun (i, x) -> x.IsNone)
    |> Array.map fst
Run Code Online (Sandbox Code Playgroud)

这是使用序列表达式的另一种方式:

let x = [|Some 1;Some 0;None;None;Some 0|]
[|for i = 0 to x.Length-1 do
     if x.[i].IsNone then yield i|]
Run Code Online (Sandbox Code Playgroud)