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