jschomay / elm-bounded-number / Number.Bounded

A type representing bounded numbers. Once a bound is set, the bounded value can never go out of this range.

between 1 10
    |> set 7
    |> inc 5
    |> value
-- (equals 10)


type Bounded number

between : number -> number -> Bounded number

Initialize a bounded number by giving it a min and max for the bounds (inclusive). The value will be initialized as the provided min. The min will always be the lower number, regardless of which order you provide the arguments.

set : number -> Bounded number -> Bounded number

Set the value manually. If you try to set a value greater than the max bound, it will "clip" at the max. Likewise, if you try to set a value less than the min bound, it will clip at the min.

inc : number -> Bounded number -> Bounded number

Increments the value by the given amount, "clipping" at the max bound if it passes it.

dec : number -> Bounded number -> Bounded number

Decrements the value by the given amount, "clipping" at the min bound if it passes it.

value : Bounded number -> number

Get the value

minBound : Bounded number -> number

Get the min bound

maxBound : Bounded number -> number

Get the max bound

map : (number -> number) -> Bounded number -> Bounded number

Transforms a Bounded value with a given function. If the value returned by the given function is greater than the max bound, it will "clip" at the max. Likewise, if the value returned by the given function is less than the min bound, it will clip at the min.

between 1 10
    |> set 9
    |> map sqrt
    |> value
-- (equals 3)