我想知道是否有一个便宜的(性能明智的)选项来搜索从索引开始满足某些条件的数组元素的索引?
Array.tryFindIndex方法没有参数startIndex。我可以做Array.skip(n)然后在那里搜索,但是创建一个仅用于搜索的数组似乎很昂贵。我该怎么做呢?
我看了一下List也没有那个说法。我需要在...期间使用吗?有更好的方法吗?
The base libraries try to provide functions for your convenience but they cannot possibly anticipate all use cases. Nothing wrong with writing your own if need be:
module Array =
let tryFindIndexFrom i p (a : _ []) =
let rec loop k =
if k >= a.Length then None
elif p a.[k] then Some k
else loop (k + 1)
if i < 0 then None else loop i
Run Code Online (Sandbox Code Playgroud)
EDIT: p is the predicate testing the array elements. tryFindIndexFrom has the same signature as tryFindIndex but with the starting index added as first parameter.
EDIT 2: Added test for k < 0 for fool-proof usage.
EDIT 3: Moved test for k < 0 out of the loop as it needs to be checked only once.