lue-bird / elm-morph / Natural.Morph

Morph for an arbitrary-sized Natural

integer : Morph Natural Integer

Morph to a Natural from an unsigned Integer

n : MorphIndependently (N range_ -> Result error_ Natural) (Natural -> N (N.Min (N.Up0 minX_)))

Morph from a fixed-size N.

Inverse of N.Morph.natural

row

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

bits : Bytes.Endianness -> N (N.In (N.Up bitCountMinX_ N.To bitCountMinPlusX_) (N.Up bitCountMaxX_ N.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