If the maximum length is a type variable,
first :
Arr (In (Nat1Plus orHigherMin) max maybeN) element
-> elementuse these operations instead of the ones in Arr or InArr
Kick out the element at an index in a direction.
removeLast arrWithAtLeast1Element =
arrWithAtLeast1Element
|> MinArr.removeAt nat0 LastToFirstPut a new element at an index in a direction.
arrWithAtLeast5Elements
|> MinArr.insertAt nat0 FirstToLast
"becomes the first"
--> : Arr (Min Nat6) StringAppend an Arr (In ...).
Arr.from3 1 2 3
|> MinArr.extend nat3 arrWithAtLeast3Elements
--> : Arr (Min Nat6) ...Compare the length to an exact Nat (ArgN ...).
Is it greater, less or equal?
min ensures that the Nat (ArgN ...) is bigger than the minimum length.
convertUserArguments :
String
-> Result String (Arr (Only Nat3))
convertUserArguments =
String.words
>> Array.fromList
>> Arr.fromArray
>> MinArr.isLength nat3
{ min = nat0 }
{ equal = Ok
, less =
\less ->
Err
("I need 3 arguments, but there are only "
++ String.fromInt (val (Arr.length less))
++ "."
)
, greater =
\more ->
Err
("I need exact 3 arguments, so "
++ String.fromInt (val (Arr.length more))
++ " is too much."
)
}Compare the length to an exact Nat (ArgN ...).
Is it equalOrGreater or less?
min ensures that the lower bound is bigger than the minimum length.
convertUserArguments :
String
-> Result String (Arr (Min Nat3))
convertUserArguments =
String.words
>> Array.fromList
>> Arr.fromArray
>> MinArr.isLengthAtLeast nat3
{ min = nat0 }
{ equalOrGreater = Ok
, less =
\less ->
Err
("I need at least 3 arguments, but there are only "
++ String.fromInt (val (Arr.length less))
++ "."
)
}Is the length
equalOrLess than an upper bound or
greater?
min ensures that the upper bound is greater than the minimum length.
-- only up to 50 tags
tag :
Arr (In min Nat50 maybeN) String
-> a
-> Tagged a
tagIfValidTags : Array String -> a -> Maybe (Tagged a)
tagIfValidTags array value =
array
|> Arr.fromArray
|> MinArr.isLengthAtMost nat50
{ equalOrLess = tag value >> Just
, greater = \_ -> Nothing
}Split
{ groups : the Arr divided into equal-sized Arrs
, less : values to one side which aren't enough
}
Arr.from7 1 2 3 4 5 6 7
|> MinArr.group nat5 FirstToLast
--> { groups = Arr.from1 (Arr.from5 1 2 3 4 5)
--> , less = Arr.from2 6 7
--> }The type of the result isn't as accurate as in the example, though!
Convert an exact Arr (In min ...) to a Nat (Min min).
between4And10Elements |> MinArr.value
--> : Arr (Min Nat4) ...There is only 1 situation you should use this.
To make these the same type.
[ atLeast1Element, between1And10Elements ]Elm complains:
But all the previous elements in the list are
Arr (Min Nat1) ...
[ atLeast1Element
, between1And10Elements |> MinArr.value
]A Codec to serialize Arrs with a minimum amount of elements.
import Serialize
-- we can't start if we have no worlds to choose from!
serializeSaves :
Serialize.Codec
String
(Arr (Min Nat1) World)
serializeSaves =
MinArr.serialize nat1 serializeWorld
encode :
Arr (In (Nat1Plus minMinus1) max maybeN) World
-> Bytes
encode =
MinArr.value
>> Serialize.encodeToBytes serializeSaves
decode :
Bytes
->
Result
(Serialize.Error String)
(Arr (Min Nat1) World)
decode =
Serialize.decodeFromBytes serializeSaves
Put a new element after the others.