Seq.findIndex<'T> 函数 (F#)

返回给定函数为其返回 true 的第一个元素的索引。

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

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

// Signature:
Seq.findIndex : ('T -> bool) -> seq<'T> -> int

// Usage:
Seq.findIndex predicate source

参数

  • predicate
    类型:'T ->bool

    用于测试是否应返回特定元素的索引的函数。

  • source
    类型:seq<'T>

    输入序列。

异常

异常

Condition

ArgumentNullException

在输入序列为 null 时引发

KeyNotFoundException

用谓词进行计算时,如果没有元素返回 true,则引发此异常

返回值

给定函数为其返回 true 的第一个元素的索引。

备注

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

示例

下面的代码示例演示如何使用 Seq.findIndex

let seqA = [| 2 .. 100 |]
let delta = 1.0e-10
let isPerfectSquare (x:int) =
    let y = sqrt (float x)
    abs(y - round y) < delta
let isPerfectCube (x:int) =
    let y = System.Math.Pow(float x, 1.0/3.0)
    abs(y - round y) < delta
let element = Seq.find (fun elem -> isPerfectSquare elem && isPerfectCube elem) seqA
let index = Seq.findIndex (fun elem -> isPerfectSquare elem && isPerfectCube elem) seqA
printfn "The first element that is both a square and a cube is %d and its index is %d." element index

Output

  

平台

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

版本信息

F#核心库版本

支持:2.0,4.0,可移植

请参见

参考

Collections.Seq 模块 (F#)

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