lue-bird/elm-morph/3.0.1

Natural.Morph

Morph to a Natural from an unsigned Integer

n :
MorphIndependently
(N range_ -> Result error_ Natural)
(Natural -> N (Min (Up0 minX_)))
chars : MorphRow Natural Char

Natural MorphRow

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
--> Nothing
Endianness
-> N
(In
(Up bitCountMinX_ To bitCountMinPlusX_)
(Up bitCountMaxX_ To bitCountMaxPlusX_)
)
-> MorphRow Natural Bit

MorphRow 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

bitsVariableCount : MorphRow Natural Bit

Unlike bits which takes a concrete bit count, bitsVariableCount can preserve any length