A natural number within a minimum & maximum.
A InNat from an Int, clamped between a minimum & maximum.
Int < minimum, minimum is returnedInt > maximum, maximum is returned9 |> InNat.intInRange nat3 nat12 --> InNat 9
0 |> InNat.intInRange nat3 nat12 --> InNat 3
99 |> InNat.intInRange nat3 nat12 --> InNat 12If you want to handle the cases < minimum & > maximum explicitly, use [isIntInRange][InNat#isIntInRange]
If the InNat is less than a number, return that number instead.
InNat.n nat5
|> InNat.atLeast nat10 { min = nat5 }
--> InNat 10
InNat.n nat15
|> InNat.lowerMin nat5
|> InNat.atLeast nat10 { min = nat5 }
--> InNat 15Cap the InNat to at most a number.
InNat.n nat5
|> InNat.atMost nat10 { min = nat5 }
--> InNat 5
InNat.n nat15
|> InNat.lowerMin nat5
|> InNat.atMost nat10 { min = nat5 }
--> InNat 10min ensures that that number is at least the minimum.
Compare the InNat to a NNat. Is it greater, less or equal to that NNat?
min ensures that the NNat is bigger than the minimum.
justIfGreater4 =
InNat.minIs nat0
>> is ( nat4, nat4 )
{ min = nat0 }
{ greater = Just
, less = \_ -> Nothing
, equal = \() -> Nothing
}
justIfGreater4 (InNat.n nat5)
--> Just (InNat 5)
justIfGreater4 (InNat.n nat3)
--> NothingCompared to a range first to last, is the InNat
inRange
greater than the last or
less than the first?
justIfBetween3And10 =
InNat.lowerMin nat0
>> InNat.isInRange
{ first = ( nat3, nat3 ), last = ( nat10, nat10 ) }
{ min : nat0 }
{ less = \_ -> Nothing
, greater = \_ -> Nothing
, inRange = Just
}
justIfBetween3And10 (InNat.n nat9)
--> Just (InNat 9)
justIfBetween3And10 (InNat.n nat123)
--> NothingAn Int compared to a range from first to last.
rejectOrAcceptUserInt =
isIntInRange { first = nat1, last = nat100 }
{ less = Err "must be >= 1"
, more = Err "must be <= 100"
, inRange = Ok
}
rejectOrAcceptUserInt (InNat.n nat0)
--> Err "must be >= 1"Is the InNat
equalOrGreater than a NNat or
less?
vote : { a | age : InNat (Nat18Plus orOlder) max } -> Vote
tryToVote =
InNat.lowerMin nat0
>> InNat.isAtLeast ( nat18, nat18 )
{ min = nat0 }
{ less = Nothing --😓
, equalOrGreater = Just << vote
}Is the InNat
equalOrLess than a NNat or
greater?
goToU18Party : { a | age : InNat min Nat17 } -> Snacks
tryToGoToU18Party =
InNat.lowerMin nat0
>> InNat.isAtMost ( nat17, nat17 )
{ min = nat0 }
{ equalOrLess = Just << goToU18Party
, greater = Nothing
}Add a fixed NNat value.
InNat.n nat70
|> InNat.addN ( nat7, nat7 )
--> is of type InNat Nat77 (Nat77Plus a)Subtract a fixed NNat value.
InNat.n nat7
|> InNat.subN ( nat7, nat7 )
--> InNat.n nat0Add a InNat.
InNat.n nat3
|> InNat.add between1And12 nat1 nat12
--> of type InNat Nat4 (Nat15Plus a)Subtract a InNat.
InNat.n nat6
|> InNat.sub between1And5 nat1 nat5
--> is of type InNat Nat1 (Nat5Plus a)Divide (//) by a MinNat. div 0 is impossible.
InNat.n nat7 |> InNat.div (MinNat.n nat3)
--> InNat 2 of type InNat Nat0 (Nat7Plus a)The remainder after division. remainderBy 0 is impossible.
InNat.n nat7 |> InNat.remainderBy (MinNat.n nat3)
--> InNat Nat0 (Nat7Plus a)If you want other operations like multiply, use [dropMax][InNat#dropMax].
A InNat from an exact NNat n.
The minimum is n, the maximum >= n.
Convert the value in a InNat to an Int.
Set the minimum lower.
[ InNat.n nat3, InNat.n nat4 ]Elm complains:
But all the previous elements in the list are:
InNat Nat3 ...
[ InNat.n nat3
, InNat.n nat4 |> InNat.lowerMin nat3
]Set the maximum higher.
You should design type annotations as general as possible.
onlyAtMost18 : InNat min Nat18
onlyAtMost18 between3And8 --fineBut once you implement onlyAtMost18, you might use the value in onlyAtMost19.
onlyAtMost18 value =
-- onlyAtMost19 value --error :(
onlyAtMost19
(value |> InNat.maxIs nat18 {- works :) -})Convert a InNat to a MinNat.
squareAInNat =
MinNat.dropMax >> MinNat.toPower (MinNat.n nat2)Generate a random InNat in a range.
InNats from a first to an exact last value.
from3To10 =
InNat.range nat3 nat10
A natural number somewhere within a minimum & maximum.
Note: maximum >= minimum for every existing
InNat:→
minimum <= Nat100