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_) -> Charyou should use InNat operations.
If the maximum isn't known, use the operations in MinNat.
If the Nat is less than a Nat, return that number instead.
nat5 |> InNat.atLeast nat10
--> Nat 10
nat15 |> InNat.atLeast nat10
--> Nat 15Is 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 age ->
toy { age = age }
Nat.Greater age ->
experience { age = age }
Nat.Equal _ ->
bigPresent
toy : { age : Nat (ArgIn min Nat17 ifN_) } -> Toy
experience :
{ age : Nat (ArgIn (Nat19Plus orOlder) max ifN_) }
-> ExperienceCompared to a range from a lower to an upper bound, is the Nat BelowOrInOrAboveRange?
lowest can be a number <= the minimum.
justIfBetween3And10 nat =
case nat |> InNat.isInRange nat3 nat10 { lowest = nat0 } of
Nat.InRange ->
Just
_ ->
Nothing
justIfBetween3And10 nat9
--> Just (Nat 9)
justIfBetween3And10 nat123
--> NothingIs the Nat BelowOrAtLeast as big as a given number?
lowest can be a number <= the minimum.
vote :
{ age : Nat (ArgIn (Nat18Plus orOlder) max ifN_) }
-> Vote
tryToVote { age } =
case age |> InNat.isAtLeast nat18 { lowest = nat0 } of
Nat.Below _ ->
--😓
Nothing
Nat.EqualOrGreater age ->
Just (vote { age = age })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 age ->
Just (goToU18Party { age = age })
Nat.Above ->
NothingAdd a Nat (N ...).
between70And100
|> InNat.add nat7
--> : Nat (In Nat77 (Nat107Plus a_))Subtract an exact Nat (N ...) value.
between7And10
|> InNat.sub nat7
--> : Nat (In Nat0 (Nat3Plus a_))Add a Nat (In ...).
The first 2 arguments are
the minimum added value
the maximum added value
between3And10
|> InNat.addIn nat1 nat12 between1And12
--> : Nat (In Nat4 (Nat22Plus a_))Subtract a Nat (ArgIn ...).
The first 2 arguments are
the minimum subtracted value
the maximum subtracted value
between6And12
|> InNat.subIn nat1 nat5 between1And5
--> : Nat (In Nat1 (Nat5Plus a_))Convert it to a Nat (In min max).
InNat.value nat4
--> : Nat (In Nat4 (Nat4Plus a_))Example
[ in3To10, nat3 ]Elm complains:
But all the previous elements in the list are:
Nat (In Nat3 Nat10)
[ in3To10
, nat3 |> InNat.value
]A Codec to serialize Nats within a lower & upper bound.
import Serialize
serializePercent :
Serialize.Codec
String
(Nat (In Nat0 (Nat100Plus a_)))
serializePercent =
InNat.serialize nat0 nat100
encode : Nat (ArgIn min_ Nat100 ifN_) -> Bytes
encode =
InNat.value
>> Serialize.encodeToBytes serializePercent
decode :
Bytes
->
Result
(Serialize.Error String)
(Nat (In Nat0 (Nat100Plus a_)))
decode =
Serialize.decodeFromBytes serializePercentFor decoded Ints out of the expected bounds, the Result is an error message.
Cap the
Natto at most a number.lowestcan be a number <= the minimum.