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

使用给定比较函数逐个元素比较两个序列。

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

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

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

// Usage:
Seq.compareWith comparer source1 source2

参数

  • comparer
    类型:'T -> 'T -> int

    采用每个序列中的元素并返回 int 的函数。 如果该函数计算为一个非零值,则将停止迭代并返回该值。

  • source1
    类型:seq<'T>

    第一个输入序列。

  • source2
    类型:seq<'T>

    第二个输入序列。

异常

异常

Condition

ArgumentNullException

在任一输入序列为 null 时引发。

返回值

从比较函数返回第一个非零结果。 当到达某个序列的结尾时,如果第一个序列较短,则返回 -1;如果第二个序列较短,则返回 1。

备注

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

示例

以下示例演示如何使用 Seq.compareWith 来比较两个使用自定义函数的序列。

let sequence1 = seq { 1 .. 10 }
let sequence2 = seq { 10 .. -1 .. 1 }

// Compare two sequences element by element.
let compareSequences = Seq.compareWith (fun elem1 elem2 ->
    if elem1 > elem2 then 1
    elif elem1 < elem2 then -1
    else 0) 

let compareResult1 = compareSequences sequence1 sequence2
match compareResult1 with
| 1 -> printfn "Sequence1 is greater than sequence2."
| -1 -> printfn "Sequence1 is less than sequence2."
| 0 -> printfn "Sequence1 is equal to sequence2."
| _ -> failwith("Invalid comparison result.")
  

平台

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

版本信息

F#核心库版本

支持:2.0,4.0,可移植

请参见

参考

Collections.Seq 模块 (F#)

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