Header menu logo TDesu.FSharp

Comparisons Module

Curried comparison active patterns for use directly in a match expression.

Marked with the auto-open attribute: opening TDesu.FSharp.ActivePatterns (or this module directly) brings Eq, NEq, Lt, Gt, LtEq, GtEq and Between into scope unqualified. Auto-open only makes sense here because these names are useless qualified — nobody wants to write Comparisons.Lt inside a match arm. Each pattern takes its comparand(s) first and the matched value last, so match x with | Lt 10 -> … reads as "x is less than 10". They rely only on F#'s built-in structural equality/comparison constraints, so they work for any comparable type — numbers, strings, DateTime, tuples, comparable records — not just numbers. Each pattern is [<return: Struct>], so a match arm costs no allocation: a plain partial active pattern would allocate an FSharpOption every time the arm is evaluated, which for a one-comparison pattern is the whole cost of the match. The use site is unchanged either way.

Active patterns

Active pattern Description

(|Between|_|) lo hi value

Full Usage: (|Between|_|) lo hi value

Parameters:
    lo : 'a - Inclusive lower bound.
    hi : 'a - Inclusive upper bound.
    value : 'a - The value being matched.

Returns: bool
Modifiers: inline
Type parameters: 'a

Matches when the matched value lies within [lo, hi], inclusive on both ends.

If lo is greater than hi the range is empty, so the pattern never matches.

lo : 'a

Inclusive lower bound.

hi : 'a

Inclusive upper bound.

value : 'a

The value being matched.

Returns: bool
Example

 match httpStatus with
 | Between 200 299 -> "success"
 | Between 400 499 -> "client error"
 | _ -> "other"

(|Eq|_|) comparand value

Full Usage: (|Eq|_|) comparand value

Parameters:
    comparand : 'a - The value to compare against.
    value : 'a - The value being matched.

Returns: bool
Modifiers: inline
Type parameters: 'a

Matches when the matched value is structurally equal to comparand.

comparand : 'a

The value to compare against.

value : 'a

The value being matched.

Returns: bool
Example

 let describe x =
     match x with
     | Eq 0 -> "zero"
     | _ -> "nonzero"
val describe: x: 'a -> string
val x: 'a

(|GtEq|_|) comparand value

Full Usage: (|GtEq|_|) comparand value

Parameters:
    comparand : 'a - The value to compare against.
    value : 'a - The value being matched.

Returns: bool
Modifiers: inline
Type parameters: 'a

Matches when the matched value is greater than or equal to comparand.

comparand : 'a

The value to compare against.

value : 'a

The value being matched.

Returns: bool
Example

 match score with
 | GtEq 60 -> "pass"
 | _ -> "fail"

(|Gt|_|) comparand value

Full Usage: (|Gt|_|) comparand value

Parameters:
    comparand : 'a - The value to compare against.
    value : 'a - The value being matched.

Returns: bool
Modifiers: inline
Type parameters: 'a

Matches when the matched value is strictly greater than comparand.

comparand : 'a

The value to compare against.

value : 'a

The value being matched.

Returns: bool
Example

 match age with
 | Gt 17 -> "adult"
 | _ -> "minor"

(|LtEq|_|) comparand value

Full Usage: (|LtEq|_|) comparand value

Parameters:
    comparand : 'a - The value to compare against.
    value : 'a - The value being matched.

Returns: bool
Modifiers: inline
Type parameters: 'a

Matches when the matched value is less than or equal to comparand.

comparand : 'a

The value to compare against.

value : 'a

The value being matched.

Returns: bool
Example

 match retries with
 | LtEq 3 -> "still retrying"
 | _ -> "gave up"

(|Lt|_|) comparand value

Full Usage: (|Lt|_|) comparand value

Parameters:
    comparand : 'a - The value to compare against.
    value : 'a - The value being matched.

Returns: bool
Modifiers: inline
Type parameters: 'a

Matches when the matched value is strictly less than comparand.

comparand : 'a

The value to compare against.

value : 'a

The value being matched.

Returns: bool
Example

 match temperature with
 | Lt 0.0 -> "freezing"
 | _ -> "above freezing"

(|NEq|_|) comparand value

Full Usage: (|NEq|_|) comparand value

Parameters:
    comparand : 'a - The value to compare against.
    value : 'a - The value being matched.

Returns: bool
Modifiers: inline
Type parameters: 'a

Matches when the matched value is not structurally equal to comparand.

comparand : 'a

The value to compare against.

value : 'a

The value being matched.

Returns: bool
Example

 match status with
 | NEq "ok" -> escalate ()
 | _ -> ()

Type something to start searching.