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.
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
--> NothingIs 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 })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 _ ->
NothingReturn the given number if the Nat is less.
between5And9 |> InNat.atLeast nat10
--> Nat 10 : Nat (In Nat10 (Nat10Plus a_))
nat15 |> InNat.atLeast nat10
--> Nat 15Add 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 ...).
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 ...).
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_))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_))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.errorToStringThe 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 serializePercentFor decoded Ints out of the expected bounds, the Result is an error message.
An error for when a decoded int is outside the expected bounds.
You can transform it into a message with errorToString.
See serialize.
An expectation for the decoded int that hasn't been met.
ExpectAtLeast some minimum in a rangeExpectAtMost some maximum in a rangeSee errorToString and serialize.
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))
Is the
NatLessOrEqualOrGreaterthan a given number?lowestcan be a number <= the minimum.