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

指定された比較関数を使用して、要素ごとに 2 つのシーケンスを比較します。

名前空間/モジュール パス: 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>

    2 番目の入力シーケンス。

例外

例外

状態

ArgumentNullException

入力シーケンスのいずれかが null の場合にスローされます。

戻り値

比較関数の最初のゼロ以外の結果を返します。シーケンスの末尾に達すると、最初のシーケンスの方が短い場合は -1、2 番目のシーケンスが短い場合は 1 を返します。

解説

この関数は、コンパイルされたアセンブリでは CompareWith という名前です。F# 以外の .NET 言語から、またはリフレクションを使用してこの関数にアクセスする場合は、この名前を使用します。

使用例

次に、Seq.compareWith を使用して、カスタムの比較関数で 2 つのシーケンスを比較する例を示します。

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#)