Operations when you know the maximum of the Nat (In minimum maximum maybeN).
For example for the argument in
toHexChar : Nat (In min Nat15 maybeN) -> Charyou should use InNat operations.
If the maximum isn't known, use the operations in MinNat.
If the Nat (In ...) is less than a Nat, return that number instead.
nat5 |> InNat.atLeast nat10
--> Nat 10
nat15 |> InNat.atLeast nat10
--> Nat 15Compare the Nat (In ...) to an exact Nat (N ...).
Is it greater, less or equal?
min ensures that the Nat (N ...) is greater than the minimum.
present =
Nat.lowerMin nat0
>> InNat.is nat18
{ min = nat0 }
{ less = \age -> toy { age = age }
, greater = \age -> experience { age = age }
, equal = \() -> bigPresent
}
toy : { age : Nat (In min Nat17 maybeN) } -> Toy
experience :
{ age : Nat (In (Nat19Plus orOlder) max maybeN) }
-> ExperienceCompared to a range from a lower to an upper bound, is the Nat (In ...)
inRange
greater than the upper bound or
less than the lower bound?
min ensures that the lower bound is greater than the minimum.
justIfBetween3And10 =
Nat.lowerMin nat0
>> InNat.isInRange nat3 nat10
{ min = nat0 }
{ less = \_ -> Nothing
, greater = \_ -> Nothing
, inRange = Just
}
justIfBetween3And10 nat9
--> Just (Nat 9)
justIfBetween3And10 nat123
--> NothingIs the Nat (In ...)
equalOrGreater than a lower bound or
less?
min ensures that the lower bound is greater than the minimum.
vote : { age : Nat (In (Nat18Plus orOlder) max maybeN) } -> Vote
tryToVote =
Nat.lowerMin nat0
>> InNat.isAtLeast nat18
{ min = nat0 }
{ less = Nothing --😓
, equalOrGreater = \age -> Just (vote { age = age })
}Is the Nat (In ...)
equalOrLess than an upper bound or
greater?
min ensures that the upper bound is greater than the minimum.
goToU18Party : { age : Nat (In min Nat17 maybeN) } -> Snack
tryToGoToU18Party =
Nat.lowerMin nat0
>> InNat.isAtMost nat17
{ min = nat0 }
{ equalOrLess =
\age -> Just (goToU18Party { age = age })
, greater = Nothing
}Add a fixed Nat (N ...) value.
between70And100
|> InNat.addN nat7
--> : Nat (In Nat77 (Nat107Plus a))Subtract an exact Nat (N ...) value.
between7And10
|> InNat.subN nat7
--> : Nat (ValueIn Nat0 (Nat3Plus a))Add a Nat (In ...).
between3And10
|> InNat.add between1And12 nat1 nat12
--> : Nat (ValueIn Nat4 (Nat22Plus a))Subtract a Nat (In ...).
The 2 following arguments are
the minimum subtracted value
the maximum subtracted value
between6And12
|> InNat.sub between1And5 nat1 nat5
--> : Nat (ValueIn Nat1 (Nat5Plus a))Convert it to a Nat (ValueIn min max).
nat4 |> InNat.value
--> : Nat (ValueIn Nat4 (Nat4Plus a))Example
[ in3To10, nat3 ]Elm complains:
But all the previous elements in the list are:
Nat (ValueIn Nat3 Nat10)
[ in3To10
, nat3 |> InNat.value
]A Codec to serialize Nats within a lower & upper bound.
import Serialize
serializePercent :
Serialize.Codec
String
(Nat (ValueIn Nat0 (Nat100Plus a)))
serializePercent =
InNat.serialize nat0 nat100
encode : Nat (In min Nat100 maybeN) -> Bytes
encode =
InNat.value
>> Serialize.encodeToBytes serializePercent
decode :
Bytes
->
Result
(Serialize.Error String)
(Nat (ValueIn Nat0 (Nat100Plus a)))
decode =
Serialize.decodeFromBytes serializePercentFor decoded Ints out of the expected bounds, the Result is an error message.
Cap the
Nat (In ...)to at most a number.minensures that that number is at least the minimum.