4 people like it.

Active pattern for comparison

An active pattern for comparing two objects of the same type and implementing IComparable. Modeled on Standard ML comparison.

1: 
2: 
3: 
4: 
5: 
6: 
/// An active pattern that compares two objects of the same type.
let (|Less|Equal|Greater|) (x, y) =
    let cmp = (x :> System.IComparable).CompareTo(y)
    if cmp < 0 then Less
    elif cmp > 0 then Greater
    else Equal
val x : System.IComparable
val y : 'a
val cmp : int
namespace System
Multiple items
type IComparable<'T> =
  member CompareTo : other:'T -> int

Full name: System.IComparable<_>

--------------------
type IComparable =
  member CompareTo : obj:obj -> int

Full name: System.IComparable
Next Version Raw view Test code New version

More information

Link:http://fssnip.net/5y
Posted:12 years ago
Author:petebu
Tags: active pattern , comparison , icomparable