10 people like it.

Between operators

A couple of operators for 'between, inclusive' and 'between, exclusive'.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
/// Exclusive 'between' operator:
let (><) x (min, max) =
    (x > min) && (x < max)

/// Inclusive 'between' operator:
let (>=<) x (min, max) =
    (x >= min) && (x <= max)

// Examples
let four = [0..9] |> List.filter (fun x -> x >< (3, 5))
// val four : int list = [4]

let threeToFive = [0..9] |> List.filter (fun x -> x >=< (3, 5))
// val threeToFive : int list = [3; 4; 5]
val x : 'a (requires comparison)
val min : 'a (requires comparison)
val max : 'a (requires comparison)
val four : int list

Full name: Script.four
Multiple items
module List

from Microsoft.FSharp.Collections

--------------------
type List<'T> =
  | ( [] )
  | ( :: ) of Head: 'T * Tail: 'T list
  interface IEnumerable
  interface IEnumerable<'T>
  member Head : 'T
  member IsEmpty : bool
  member Item : index:int -> 'T with get
  member Length : int
  member Tail : 'T list
  static member Cons : head:'T * tail:'T list -> 'T list
  static member Empty : 'T list

Full name: Microsoft.FSharp.Collections.List<_>
val filter : predicate:('T -> bool) -> list:'T list -> 'T list

Full name: Microsoft.FSharp.Collections.List.filter
val x : int
val threeToFive : int list

Full name: Script.threeToFive
Raw view Test code New version

More information

Link:http://fssnip.net/9y
Posted:12 years ago
Author:Kit Eason
Tags: operator overloading