Morph for an arbitrary-sized Natural
Morph from a fixed-size N.
import Morph
import List.Morph
import N
import Natural
"123"
|> Morph.toNarrow
(Natural.Morph.chars |> Morph.rowFinish |> Morph.over List.Morph.string)
|> Result.map (Natural.toN >> N.toInt)
--> Ok 123
-- a negative integer is not a natural
"-123"
|> Morph.toNarrow
(Natural.Morph.chars |> Morph.rowFinish |> Morph.over List.Morph.string)
|> Result.toMaybe
--> Nothing
-- a decimal number is not a natural
"3.14"
|> Morph.toNarrow (Natural.Morph.chars |> Morph.rowFinish |> Morph.over List.Morph.string)
|> Result.toMaybe
--> Nothing
-- letters etc are not accepted
"3e10"
|> Morph.toNarrow (Natural.Morph.chars |> Morph.rowFinish |> Morph.over List.Morph.string)
|> Result.toMaybe
--> NothingMorphRow for a Natural
from a given count of Bits
and with a given endianness.
Note that the bit count can be any number like n7 or n128. Numbers greater than n16
can quickly be generated locally, see this section in the N module documentation
For toBroad: If the number is greater than the capacity possible with the given bit count,
the greatest possible value will be returned instead.
To keep the whole range → bitsVariableCount
Unlike bits which takes a concrete bit count,
bitsVariableCount can preserve any length
Morphto aNaturalfrom an unsignedInteger