lue-bird/elm-bounded-nat/13.0.0

NOTE: the latest version is 35.0.0

MinNat

2 situations where you use these operations instead of the ones in Nat or InNat:

  1. Your value is of type Nat (Min ...)

    nat3 |> Nat.mul nat3
    --> Nat (Min Nat3)
  2. The maximum is a type variable

    divideBy : Nat (ArgIn (Nat1Plus minMinus1) max maybeN) -> --...
    divideBy atLeast1 =
        --...

compare

is :
Nat
(ArgN
(Nat1Plus valueMinus1)
(Is a To (Nat1Plus valueMinus1PlusA))
x
)
-> { lowest :
Nat
(ArgN
lowest
(Is lowestToMin To min)
(Is minToValueMinus1 To valueMinus1)
)
}
-> Nat (ArgIn min max maybeN)
(Nat (In lowest valueMinus1PlusA))
()
(Nat (Min (Nat2Plus valueMinus1)))

Is the Nat LessOrEqualOrGreater than a given number?

lowest can be a number <= the minimum.

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


        Nat.Greater age ->
            experience { age = age }


        Nat.Equal () ->
            bigPresent


toy : { age : Nat (ArgIn Nat0 Nat17 maybeN) } -> Toy


experience : { age : Nat (ArgIn Nat19 max maybeN) } -> Experience
Nat
(ArgN lowerBound (Is a To (Nat1Plus lowerBoundMinus1PlusA)) x)
-> { lowest :
Nat
(ArgN
lowest
(Is lowestToMin To min)
(Is lowestToLowerBound To lowerBound)
)
}
-> Nat (ArgIn min max maybeN)
(Nat (In lowest lowerBoundMinus1PlusA))
(Nat (Min lowerBound))

Is the Nat BelowOrAtLeast a given number?

factorial : Nat (ArgIn min max maybeN) -> Nat (Min Nat1)
factorial =
    factorialBody


factorialBody : -- as in factorial
factorialBody =
    case x |> MinNat.isAtLeast nat1 { lowest = nat0 } of
        Nat.Below _ ->
            MinNat.value nat1


        Nat.EqualOrGreater atLeast1 ->
            Nat.mul atLeast1
                (factorial
                    (atLeast1 |> MinNat.subN nat1)
                )
Nat (ArgN upperBound (Is a To upperBoundPlusA) x)
-> { lowest :
Nat
(ArgN
lowest
(Is lowestToMin To min)
(Is minToAtMostMin To upperBound)
)
}
-> Nat (ArgIn min max maybeN)
(Nat (In lowest upperBoundPlusA))
(Nat (Min (Nat1Plus upperBound)))

Is the Nat AtMostOrAbove a given number?

goToU18Party : { age : Nat (ArgIn min Nat17 maybeN) } -> Snack


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


        Greater _ ->
            Nothing

modify

Nat (ArgN subbed (Is differenceMin To min) x)
-> Nat (ArgIn min max maybeN)
-> Nat (In differenceMin max)

Subtract an exact Nat (ArgN ...).

atLeast7 |> MinNat.subN nat2
--> : Nat (Min Nat5)
add :
Nat (ArgIn minAdded maxAdded addedMaybeN)
-> Nat (ArgN minAdded (Is min To sumMin) x)
-> Nat (ArgIn min max maybeN)
-> Nat (Min sumMin)

Add a Nat (ArgIn ...). The second argument is the minimum added value.

atLeast5 |> MinNat.add atLeast2 nat2
--> : Nat (Min Nat7)
sub :
Nat (ArgIn minSubbed maxSubbed subbedMaybeN)
-> Nat (ArgN maxSubbed (Is differenceMin To min) x)
-> Nat (ArgIn min max maybeN)
-> Nat (In differenceMin max)

Subtract a Nat (ArgIn ...). The second argument is the maximum of the subtracted Nat (ArgIn ...).

atLeast6 |> MinNat.sub between0And5 nat5
--> : Nat (Min Nat1)

If you have don't the maximum subtracted value at hand, use subLossy.

Nat (ArgN added (Is min To sumMin) x)
-> Nat (ArgIn min max maybeN)
-> Nat (Min sumMin)

Add a fixed Nat (ArgN ...) value.

atLeast70 |> InNat.addN nat7
--> : Nat (Min Nat77)

drop information

value : Nat (ArgIn min max maybeN) -> Nat (Min min)

Convert a Nat (ArgIn min ...) to a Nat (Min min).

between3And10 |> MinNat.value
--> : Nat (Min Nat4)

There is only 1 situation you should use this.

To make these the same type.

[ atLeast1, between1And10 ]

Elm complains:

But all the previous elements in the list are: Nat (Min Nat1)

[ atLeast1
, between1And10 |> MinNat.value
]

extra

Nat (ArgIn minLowerBound maxLowerBound lowerBoundMaybeN)
-> Codec String (Nat (Min minLowerBound))

A Codec to serialize Nats with a lower bound.

import Serialize


serializeNaturalNumber :
    Serialize.Codec
        String
        (Nat (Min Nat0))
serializeNaturalNumber =
    MinNat.serialize nat0


encode : Nat (ArgIn min max maybeN) -> Bytes
encode =
    MinNat.value
        >> Serialize.encodeToBytes serializeNaturalNumber


decode :
    Bytes
    -> Result (Serialize.Error String) (Nat (Min Nat0))
decode =
    Serialize.decodeFromBytes serializeNaturalNumber