Skip to content

Instantly share code, notes, and snippets.

@ToJans
Created April 1, 2014 10:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ToJans/9911763 to your computer and use it in GitHub Desktop.
Save ToJans/9911763 to your computer and use it in GitHub Desktop.
This should do it for ranges
type Range(min: decimal,max: decimal) =
member this.min = min
member this.max = max
static member (*) (r: Range, d: decimal) = Range(r.min * d,r.max * d)
static member (*) (d: decimal, r: Range) = r*d
static member (/) (r: Range, d: decimal) = Range(r.min / d,r.max / d)
static member (/) (d: decimal, r: Range) = r/d
static member (+) (r: Range, d: decimal) = Range(r.min + d,r.max + d)
static member (+) (d: decimal, r: Range) = r+d
static member (-) (r: Range, d: decimal) = Range(r.min - d,r.max - d)
static member (-) (d: decimal, r: Range) = r-d
static member (*) (l: Range, r: Range) = Range(Seq.min [l.min;r.min], Seq.max [l.max;r.max])
static member (/) (l: Range, r: Range) = Range(Seq.max [l.min;r.min], Seq.min [l.max;r.max])
member this.bounded(d: decimal) = [Seq.max [this.min;d] ;this.max] |> Seq.min
type Position(value: decimal, range: Range) =
member this.value = value
member this.range = range
static member (*) (r:Position, d: decimal) = Position(r.range.bounded(r.value*d), r.range)
static member (*) (d: decimal, r:Position) = r * d
static member (/) (r:Position, d: decimal) = Position(r.range.bounded(r.value/d), r.range)
static member (/) (d: decimal, r:Position) = r / d
static member (+) (r:Position, d: decimal) = Position(r.range.bounded(r.value+d), r.range)
static member (+) (d: decimal, r:Position) = r + d
static member (-) (r:Position, d: decimal) = Position(r.range.bounded(r.value-d), r.range)
static member (-) (d: decimal, r:Position) = r - d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment