Working with list of digits, e.g. padding digits and grouping digits.
import UInt64
import UInt64.Digits as Digits
UInt64.fromInt 7654321
|> UInt64.toDigits Digits.decimal
|> Digits.groupToString 3 ' '
--> "7 654 321"
UInt64.fromInt 0xABCDE
|> UInt64.toDigits Digits.hexLower
|> Digits.padToPowerOfTwo '0'
|> Digits.toString
|> (++) "0x"
--> "0x000abcde"
UInt64.fromInt 0x10F
|> UInt64.toDigits Digits.binary
|> Digits.padToMultipleOf 4 '0'
|> Digits.pad 32 '.'
|> Digits.groupToString 4 ' '
--> ".... .... .... .... .... 0001 0000 1111"
UInt64.fromInt 0x1FF
|> UInt64.toIntDigits Digits.octal
|> Digits.toList
--> [ 7, 7, 7 ]
UInt64.Internal.Digits a
List of digits.
See also UInt64.toDigits
and UInt64.toIntDigits
.
empty : Digits a
Empty Digits
.
This can be useful e.g. when converting Maybe UInt64
to Digits
,
so you can use empty
for Nothing
.
import UInt64.Digits as Digits
Digits.empty
|> Digits.toList
--> []
UInt64.Internal.Base
Base used with toDigits
and toIntDigits
to convert UInt64
to Digits
.
decimal : Base
Decimal aka base 10.
toDigits
: 0123456789
hex : Base
Hexadecimal aka base 16, using uppercase characters.
toDigits
: 0123456789ABCDEF
octal : Base
Octal aka base 8.
toDigits
: 01234567
binary : Base
Binary aka base 2.
toDigits
: 01
hexUpper : Base
Hexadecimal aka base 16, using uppercase characters.
This is same as hex
.
toDigits
: 0123456789ABCDEF
hexLower : Base
Hexadecimal aka base 16, using lowercase characters.
toDigits
: 0123456789abcdef
The padding value given to these functions is not checked or validated in any way. It can be any value and doesn't need to be a valid digit.
pad : Basics.Int -> a -> Digits a -> Digits a
Pad to given number of digits.
Int
: 1 <= x <= 64
See argument handling.
import UInt64
import UInt64.Digits as Digits
UInt64.fromInt 271
|> UInt64.toDigits Digits.binary
|> Digits.pad 32 '0'
|> Digits.toString
--> "00000000000000000000000100001111"
padToEven : a -> Digits a -> Digits a
Pad to even number of digits.
import UInt64
import UInt64.Digits as Digits
UInt64.fromInt 271
|> UInt64.toDigits Digits.binary
|> Digits.padToEven '0'
|> Digits.toString
--> "0100001111"
padToMultipleOf : Basics.Int -> a -> Digits a -> Digits a
Pad to multiple of given number of digits.
For example padToMultipleOf 16
would pad to 0, 16, 32, 48 or 64 digits.
Int
: 1 <= x <= 64
See argument handling.
import UInt64
import UInt64.Digits as Digits
UInt64.fromInt 271
|> UInt64.toDigits Digits.binary
|> Digits.padToMultipleOf 4 '0'
|> Digits.toString
--> "000100001111"
padToPowerOfTwo : a -> Digits a -> Digits a
Pad to 2^n
digits, i.e. to 0, 1, 2, 4, 8, 16, 32 or 64 digits.
import UInt64
import UInt64.Digits as Digits
UInt64.fromInt 271
|> UInt64.toDigits Digits.binary
|> Digits.padToPowerOfTwo '0'
|> Digits.toString
--> "0000000100001111"
map : (a -> b) -> Digits a -> Digits b
Apply a function to every digit.
If Digits
has been padded, then function is also applied to added padding, if any.
import UInt64
import UInt64.Digits as Digits
UInt64.fromInt 11
|> UInt64.toIntDigits Digits.binary
|> Digits.map ((==) 1)
|> Digits.toList
--> [ True, False, True, True ]
toString : Digits Char -> String
Convert Digits
of Char
to String
.
import UInt64
import UInt64.Digits as Digits
UInt64.fromInt 7654321
|> UInt64.toDigits Digits.decimal
|> Digits.toString
--> "7654321"
groupToString : Basics.Int -> Char -> Digits Char -> String
Convert Digits
of Char
to String
of grouped digits.
Groups are separated by given separator
.
groupSize
: 1 <= x <= 64
See argument handling.
import UInt64
import UInt64.Digits as Digits
UInt64.fromInt 7654321
|> UInt64.toDigits Digits.decimal
|> Digits.groupToString 3 ' '
--> "7 654 321"
toList : Digits a -> List a
Convert Digits
to List
of digits.
import UInt64
import UInt64.Digits as Digits
UInt64.fromInt 7654321
|> UInt64.toDigits Digits.decimal
|> Digits.toList
--> [ '7', '6', '5', '4', '3', '2', '1' ]
toListWithLength : Digits a -> ( Basics.Int, List a )
Convert Digits
to List
of digits with list length.
This is O(1) while List.length
is O(n), so if you need list length then
using this is faster than toList
followed by List.length
.
import UInt64
import UInt64.Digits as Digits
UInt64.fromInt 7654321
|> UInt64.toDigits Digits.decimal
|> Digits.toListWithLength
--> ( 7, [ '7', '6', '5', '4', '3', '2', '1' ] )
groupToFlatList : Basics.Int -> a -> Digits a -> List a
Convert Digits
to flat List
of grouped digits.
Groups are separated by given separator
.
groupSize
: 1 <= x <= 64
See argument handling.
import UInt64
import UInt64.Digits as Digits
UInt64.fromInt 7654321
|> UInt64.toDigits Digits.decimal
|> Digits.groupToFlatList 3 ' '
--> [ '7', ' ', '6', '5', '4', ' ', '3', '2', '1' ]
groupToList : Basics.Int -> Digits a -> List (List a)
Convert Digits
to List
of grouped digits.
Each group will be a separate sub-List
.
groupSize
: 1 <= x <= 64
See argument handling.
import UInt64
import UInt64.Digits as Digits
UInt64.fromInt 7654321
|> UInt64.toDigits Digits.decimal
|> Digits.groupToList 3
--> [ [ '7' ], [ '6', '5', '4' ], [ '3', '2', '1' ] ]