lue-bird/elm-bounded-nat/19.0.0

NOTE: the latest version is 35.0.0

InNat

Operations when you know the maximum of the Nat (ArgIn minimum maximum ifN_).

For example for the argument in

toHexChar : Nat (ArgIn min_ Nat15 ifN_) -> Char

you should use InNat operations.

If the maximum isn't known, use the operations in MinNat.

compare

is :
Nat
(N
(Nat1Plus valueMinus1)
atLeastValue
(Is a_ To (Nat1Plus atLeastValueMinus1))
(Is valueToMax_ To max)
)
-> { lowest :
Nat
(N
lowest
atLeastLowest_
(Is lowestToMin_ To min)
(Is minToValue_ To (Nat1Plus valueMinus1))
)
}
-> Nat (ArgIn min max ifN_)
(Nat (In lowest atLeastValueMinus1))
(Nat (In (Nat1Plus valueMinus1) atLeastValue))
(Nat (In (Nat2Plus valueMinus1) max))

Is the Nat LessOrEqualOrGreater than a given number?

lowest can be a number <= the minimum.

giveAPresent { age } =
    case age |> InNat.is nat18 { lowest = nat0 } of
        Nat.Less younger ->
            toy { age = younger }


        Nat.Greater older ->
            book { age = older }


        Nat.Equal _ ->
            bigPresent


toy : { age : Nat (ArgIn min_ Nat17 ifN_) } -> Toy


book :
    { age : Nat (ArgIn (Nat19Plus minMinus19_) max_ ifN_) }
    -> Book
Nat
(N
lowerBound
(Nat1Plus atLeastLowerBoundMinus1)
(Is lowerBoundToUpperBound_ To upperBound)
lowerBoundIs_
)
-> Nat
(N
upperBound
atLeastUpperBound
(Is upperBoundToMax_ To max)
upperBoundIs_
)
-> { lowest :
Nat
(N
lowest
atLeastLowest_
(Is lowestToMin_ To min)
(Is minToLowerBound_ To lowerBound)
)
}
-> Nat (ArgIn min max ifN_)
(Nat (In lowest atLeastLowerBoundMinus1))
(Nat (In lowerBound atLeastUpperBound))
(Nat (In (Nat1Plus upperBound) max))

Compared to a range from a lower to an upper bound, is the Nat BelowOrInOrAboveRange?

lowest can be a number <= the minimum.

ifBetween3And10 nat =
    case nat |> InNat.isInRange nat3 nat10 { lowest = nat0 } of
        Nat.InRange inRange ->
            Just inRange


        _ ->
            Nothing


ifBetween3And10 nat9
--> Just (Nat 9)


ifBetween3And10 nat123
--> Nothing
Nat
(N
lowerBound
(Nat1Plus atLeastLowerBoundMinus1)
(Is atLeastRange_ To max)
is_
)
-> { lowest :
Nat
(N
lowest
atLeastLowest_
(Is lowestToMin_ To min)
(Is (Nat1Plus lowestToLowerBound_) To lowerBound)
)
}
-> Nat (ArgIn min max ifN_)
(Nat (In lowest atLeastLowerBoundMinus1))
(Nat (In lowerBound max))

Is the Nat BelowOrAtLeast as big as a given number?

lowest can be a number <= the minimum.

vote :
    { age : Nat (ArgIn (Nat18Plus minMinus18_) max_ ifN_) }
    -> Vote


tryToVote { age } =
    case age |> InNat.isAtLeast nat18 { lowest = nat0 } of
        Nat.Below _ ->
            --😓
            Nothing


        Nat.EqualOrGreater oldEnough ->
            Just (vote { age = oldEnough })
Nat
(N
upperBound
atLeastUpperBound
(Is (Nat1Plus greaterRange_) To max)
is_
)
-> { lowest :
Nat
(N
lowest
atLeastLowest_
(Is lowestToMin_ To min)
(Is minToUpperBound_ To upperBound)
)
}
-> Nat (ArgIn min max ifN_)
(Nat (In lowest atLeastUpperBound))
(Nat (In (Nat1Plus upperBound) max))

Is the Nat AtMostOrAbove a given number?

lowest can be a number <= the minimum.

goToU18Party : { age : Nat (ArgIn min_ Nat17 ifN_) } -> Snack


tryToGoToU18Party { age } =
    case age |> InNat.isAtMost nat17 { lowest = nat0 } of
        Nat.EqualOrLess u18 ->
            Just (goToU18Party { age = u18 })


        Nat.Above _ ->
            Nothing

clamp

Nat (ArgIn minNewMin max lowerIfN_)
-> Nat (ArgIn min_ max ifN_)
-> Nat (In minNewMin max)

Return the given number if the Nat is less.

between5And9 |> InNat.atLeast nat10
--> Nat 10 : Nat (In Nat10 (Nat10Plus a_))


nat15 |> InNat.atLeast nat10
--> Nat 15

modify

add :
Nat
(N added_ atLeastAdded_ (Is min To sumMin) (Is max To sumMax))
-> Nat (ArgIn min max ifN_)
-> Nat (In sumMin sumMax)

Add a Nat (N ...).

between70And100
    |> InNat.add nat7
--> : Nat (In Nat77 (Nat107Plus a_))

Use addIn if you want to add a Nat that isn't a Nat (N ...).

sub :
Nat
(N
subbed_
atLeastSubbed_
(Is differenceMin To min)
(Is differenceMax To max)
)
-> Nat (ArgIn min max ifN_)
-> Nat (In differenceMin differenceMax)

Subtract an exact Nat (N ...) value.

between7And10
    |> InNat.sub nat7
--> : Nat (In Nat0 (Nat3Plus a_))

Use subIn if you want to subtract a Nat that isn't a Nat (N ...).

Nat
(N minAdded atLeastMinAdded_ (Is min To sumMin) minAddedIs_)
-> Nat
(N maxAdded atLeastMaxAdded_ (Is max To sumMax) maxAddedIs_)
-> Nat (ArgIn minAdded maxAdded addedIfN_)
-> Nat (ArgIn min max ifN_)
-> Nat (In sumMin sumMax)

Add a Nat that isn't a Nat (N ...).

The first 2 arguments are

  • the minimum added value

  • the maximum added value

between3And10
    |> InNat.addIn nat1 nat12 between1And12
--> : Nat (In Nat4 (Nat22Plus a_))
Nat
(N
minSubbed
atLeastMinSubbed_
(Is differenceMax To max)
minSubbedIs_
)
-> Nat
(N
maxSubbed
atLeastMaxSubbed_
(Is differenceMin To min)
maxSubbedIs_
)
-> Nat (ArgIn minSubbed maxSubbed subbedIfN_)
-> Nat (ArgIn min max ifN_)
-> Nat (In differenceMin differenceMax)

Subtract a Nat taht isn't a Nat (N ...).

The first 2 arguments are

  • the minimum subtracted value

  • the maximum subtracted value

between6And12
    |> InNat.subIn nat1 nat5 between1And5
--> : Nat (In Nat1 (Nat5Plus a_))

extra

Nat (ArgIn minLowerBound maxLowerBound lowerBoundIfN_)
-> Nat (ArgIn maxLowerBound maxUpperBound upperBoundIfN_)
-> Codec Error (Nat (In minLowerBound maxUpperBound))

A Codec to serialize Nats within a lower & upper bound.

import Serialize exposing (Codec)


serializePercent :
    Codec
        String
        (Nat (In Nat0 (Nat100Plus a_)))
serializePercent =
    InNat.serialize nat0 nat100
        >> Serialize.mapError InNat.errorToString

The encode/decode functions can be extracted if needed.

encodePercent : Nat (ArgIn min_ Nat100 ifN_) -> Bytes
encodePercent =
    Nat.toIn
        >> Serialize.encodeToBytes serializePercent


decodePercent :
    Bytes
    ->
        Result
            (Serialize.Error String)
            (Nat (In Nat0 (Nat100Plus a_)))
decodePercent =
    Serialize.decodeFromBytes serializePercent

For decoded Ints out of the expected bounds, the Result is an error message.

type alias Error =
{ expected : Expectation
, actual : Int
}

An error for when a decoded int is outside the expected bounds.

You can transform it into a message with errorToString.

See serialize.

= ExpectAtLeast (Nat (Min Nat0))
| ExpectAtMost (Nat (Min Nat0))

An expectation for the decoded int that hasn't been met.

  • ExpectAtLeast some minimum in a range
  • ExpectAtMost some maximum in a range

See errorToString and serialize.

errorToString : Error -> String

Convert an Error to a readable message.

{ expected = InNat.ExpectAtLeast nat11
, actual = 10
}
    |> InNat.errorToString
--> "expected an int >= 11 but the actual int was 10"

(example won't compile because nat11 isn't of type Nat (Min Nat0))