bitsoflogic / elm-radixint / RadixInt

This library allows you to convert between Int (base 10) and a List Int (base n). It purposely does not tie you to any specific notation to represent the digits.

RadixInt.fromInt (Base 6)
    |> RadixInt.toList -- [4,5]  (54 in base6 == 34 in base10)
    |> List.indexedMap Tuple.pair -- [(0,4),(1,5)]

The List lines up its index with the position of each digit. Using Base12 as an example, lets represent 10 as A and 11 as B. Thus, these are all equivalent:

142     -- base10
BA      -- base12
[10 11] -- RadixInt.toList num
        -- Note: index 0 == position 0 (2 in 142; A in BA)
        --       index 1 == position 1 (4 in 142; B in BA)
        --       index 2 == position 2 (1 in 142)
        -- 142 = 10^2 * 1 + 10^1 *  4 + 10^0 *  2
        -- BA  =            12^1 * 11 + 12^0 * 10

Definition


type RadixInt

Represents a number with any Int base


type Base
    = Base Basics.Int

Represents the base (aka radix) of the number being used

Methods

fromInt : Base -> Basics.Int -> RadixInt

Convert an Int with a custom base into a RadixInt

fromInt (Base 6) 36 == RadixInt

toInt : RadixInt -> Basics.Int

Convert a RadixInt back into the Elm's base10 Int

toInt (RadixInt.fromInt (Base 16) 24) == 24

fromList : Base -> List Basics.Int -> Maybe RadixInt

Convert a List Int of Base to a Maybe RadixInt.

Will return Nothing if any of the Ints represent an invalid number for the given base.

fromList (Base 10) [ 9 , 1 ]  == RadixInt with decimal value of 19
fromList (Base 10) [ 10 , 1 ] == Nothing
fromList (Base 10) [ 0, 2 ]   == RadixInt with decimal value of 20

toList : RadixInt -> List Basics.Int

Convert a RadixInt into a List of Ints, where the index in the List corresponds to the position of the numbers.

toList (RadixInt.fromInt (Base 10) 123) == [3, 2, 1]
toList (RadixInt.fromInt (Base 10) -123) == [-3, -2, -1]

--   1     2     3
--   |     |     |
-- 10^2  10^1  10^0