GithubHelp home page GithubHelp logo

innoave / bounded-number Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 13 KB

A type representing a bounded number

License: BSD 3-Clause "New" or "Revised" License

Elm 100.00%
datatype immutable bounds

bounded-number's Introduction

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.

    Bounded.between 0 10
        --|> Expect.equal 0 << Bounded.value
        |> Bounded.incBy 2
        --|> Expect.equal 2 << Bounded.value
        |> Bounded.incBy 9
        --|> 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.

    Bounded.between 0 10
        --|> Expect.equal 0 << Bounded.value
        |> Bounded.tryIncBy 2
        --|> Expect.equal (Just 2) << Bounded.value
        |> Bounded.tryIncBy 9
        --|> 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.

Bounded.between 0 10
    |> Bounded.set 4
    |> Bounded.value
    --|> 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.

bounded-number's People

Contributors

haraldmaida avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.