项目作者: innoave

项目描述 :
A type representing a bounded number
高级语言: Elm
项目地址: git://github.com/innoave/bounded-number.git
创建时间: 2019-06-09T13:04:25Z
项目社区:https://github.com/innoave/bounded-number

开源协议:BSD 3-Clause "New" or "Revised" License

下载


Bounded Number

The type Bounded number represent a number bounded between a minimum and a maximum.

Once the bounds are set the value of a bounded number can not get greater than its max bound
neither can it get lower than its min bound.

Functions that modify the value of a bound number come in two variants.

  1. The normal variant clips the resulting value within the defined bounds. Functions of this
    variant are inc, dec, incBy, decBy, set and map.

    1. Bounded.between 0 10
    2. --|> Expect.equal 0 << Bounded.value
    3. |> Bounded.incBy 2
    4. --|> Expect.equal 2 << Bounded.value
    5. |> Bounded.incBy 9
    6. --|> Expect.equal 10 << Bounded.value
  2. The trying variant explicitly states whether the result would overrun the defined bounds by
    returning a Maybe. These are functions which names are prefixed with “try” like tryInc,
    tryDec, trySet, tryMap.

    1. Bounded.between 0 10
    2. --|> Expect.equal 0 << Bounded.value
    3. |> Bounded.tryIncBy 2
    4. --|> Expect.equal (Just 2) << Bounded.value
    5. |> Bounded.tryIncBy 9
    6. --|> Expect.equal Nothing << Bounded.value

The value of a bounded number can be set directly by the set function and read using the value
function.

  1. Bounded.between 0 10
  2. |> Bounded.set 4
  3. |> Bounded.value
  4. --|> Expect.equal 4

The functions encode and decoder support a default JSON representation of a bounded number.

Check out the documentation of the functions as well as the fairly complete
unit tests.