bitsoflogic/elm-radixint/1.0.0

NOTE: the latest version is 2.0.0

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.

base6 = 6
RadixInt.fromInt 34 base6
    |> 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
        -- 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

Represents a number with any Int RadixInt

Methods

fromInt : Int -> Int -> RadixInt

Convert an Int with a custom base into a RadixInt

base6 = 6
fromInt 36 base6 == RadixInt
toInt : RadixInt -> Int

Convert a RadixInt back into the Elm's base10 Int

base16 = 16
toInt (RadixInt.fromInt 24 base16) == 24
toList : RadixInt -> List Int

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

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


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