List.tryFindIndex<'T> 函数 (F#)

返回满足给定谓词的列表中第一个元素的索引。 如果不存在此类元素,则返回 None。

命名空间/模块路径: Microsoft.FSharp.Collections.List

程序集:FSharp.Core(在 FSharp.Core.dll 中)

// Signature:
List.tryFindIndex : ('T -> bool) -> 'T list -> int option

// Usage:
List.tryFindIndex predicate list

参数

  • predicate
    类型:'T -> bool

    用于测试输入元素的函数。

  • list
    类型:'T list

    输入列表。

返回值

谓词为其返回 true 的第一个元素的索引;如果每个元素的计算结果都为 false,则返回 None。

备注

此函数在编译的程序集中名为 TryFindIndex。 如果从 F# 以外的语言中访问函数,或通过反射访问成员,请使用此名称。

示例

下面的代码示例阐释了 List.tryFindList.tryFindIndex 的用法:

let list1d = [1; 3; 7; 9; 11; 13; 15; 19; 22; 29; 36]
let isEven x = x % 2 = 0
match List.tryFind isEven list1d with
| Some value -> printfn "The first even value is %d." value
| None -> printfn "There is no even value in the list."

match List.tryFindIndex isEven list1d with
| Some value -> printfn "The first even value is at position %d." value
| None -> printfn "There is no even value in the list."

Output

  
  

平台

Windows 8,Windows 7,Windows server 2012中,Windows server 2008 R2

版本信息

F#核心库版本

支持:2.0,4.0,可移植

请参见

参考

Collections.List 模块 (F#)

Microsoft.FSharp.Collections 命名空间 (F#)