Header menu logo TDesu.FSharp

EqualityComparer Module

Factory for ad hoc IEqualityComparer{T} instances — for when a Dictionary{TKey,TValue}, HashSet{T}, or LINQ overload needs a custom key comparer but a full named type isn't worth declaring.

Functions and values

Function or value Description

EqualityComparer.create equals getHashCode

Full Usage: EqualityComparer.create equals getHashCode

Parameters:
    equals : 'a -> 'a -> bool - The equality function.
    getHashCode : 'a -> int - The hash function.

Returns: IEqualityComparer<'a>

Creates an IEqualityComparer{T} from an explicit equality function and hash function.

equals : 'a -> 'a -> bool

The equality function.

getHashCode : 'a -> int

The hash function.

Returns: IEqualityComparer<'a>
Example

 let ordinalIgnoreCase =
     EqualityComparer.create
         (fun (a: string) b -> String.Equals(a, b, StringComparison.OrdinalIgnoreCase))
         (fun s -> s.ToUpperInvariant().GetHashCode())
 let set = HashSet<string>(ordinalIgnoreCase)
val ordinalIgnoreCase: obj
Multiple items
val string: value: 'T -> string

--------------------
type string = System.String
module String from Microsoft.FSharp.Core
val set: obj

EqualityComparer.createBy projection

Full Usage: EqualityComparer.createBy projection

Parameters:
    projection : 'a -> 'b - The function extracting the comparison key from an element.

Returns: IEqualityComparer<'a>

Creates an IEqualityComparer{T} that compares and hashes elements by a projected key, using the key's own structural equality and hash code.

projection : 'a -> 'b

The function extracting the comparison key from an element.

Returns: IEqualityComparer<'a>
Example

 let byId = EqualityComparer.createBy (fun (p: Person) -> p.Id)
 let set = HashSet<Person>(byId)
val byId: obj
val set: obj

Type something to start searching.