module Text.PercentFormat.Utils
( maybeRead
, align
, rightAlign
, leftAlign
, showWithBase
, applyWhen
, intsToDigits
, theLast
, loop
, none
, integerToDigits
)
where
import Data.Maybe (listToMaybe)
import Data.List (unfoldr)
import Data.Char (intToDigit)
maybeRead :: Read a => String -> Maybe a
maybeRead :: forall a. Read a => String -> Maybe a
maybeRead = forall a. [a] -> Maybe a
listToMaybe forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> a
fst forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Read a => ReadS a
reads
align :: Bool -> Char -> Int -> String -> String
align :: Bool -> Char -> Int -> String -> String
align Bool
left = if Bool
left
then Char -> Int -> String -> String
leftAlign
else Char -> Int -> String -> String
rightAlign
rightAlign :: Char -> Int -> String -> String
rightAlign :: Char -> Int -> String -> String
rightAlign Char
c Int
width String
s | Int
width forall a. Ord a => a -> a -> Bool
<= Int
len = String
s
| Bool
otherwise = forall a. Int -> a -> [a]
replicate (Int
width forall a. Num a => a -> a -> a
- Int
len) Char
c forall a. [a] -> [a] -> [a]
++ String
s
where
len :: Int
len = forall (t :: * -> *) a. Foldable t => t a -> Int
length String
s
leftAlign :: Char -> Int -> String -> String
leftAlign :: Char -> Int -> String -> String
leftAlign Char
c Int
width String
s | Int
width forall a. Ord a => a -> a -> Bool
<= Int
len = String
s
| Bool
otherwise = String
s forall a. [a] -> [a] -> [a]
++ forall a. Int -> a -> [a]
replicate (Int
width forall a. Num a => a -> a -> a
- Int
len) Char
c
where
len :: Int
len = forall (t :: * -> *) a. Foldable t => t a -> Int
length String
s
showWithBase :: Integral a => Int -> a -> String
showWithBase :: forall a. Integral a => Int -> a -> String
showWithBase Int
b a
0 = String
"0"
showWithBase Int
b a
n | a
n forall a. Ord a => a -> a -> Bool
< a
0 = Char
'-'forall a. a -> [a] -> [a]
:forall a. Integral a => Int -> a -> String
showWithBase Int
b (forall a. Num a => a -> a
abs a
n)
| Bool
otherwise = forall a b. (a -> b) -> [a] -> [b]
map Int -> Char
intToDigit forall a b. (a -> b) -> a -> b
$ forall a. Integral a => Int -> a -> [Int]
integerToDigits Int
b a
n
integerToDigits :: Integral a => Int -> a -> [Int]
integerToDigits :: forall a. Integral a => Int -> a -> [Int]
integerToDigits Int
b =
forall a b. (a -> b) -> [a] -> [b]
map forall a b. (Integral a, Num b) => a -> b
fromIntegral
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. [a] -> [a]
reverse
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall b a. (b -> Maybe (a, b)) -> b -> [a]
unfoldr (\a
n -> forall a. [a] -> Maybe a
listToMaybe [forall {b} {a}. (b, a) -> (a, b)
swap forall a b. (a -> b) -> a -> b
$ a
n forall a. Integral a => a -> a -> (a, a)
`divMod` forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
b | a
n forall a. Eq a => a -> a -> Bool
/= a
0])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Num a => a -> a
abs
where
swap :: (b, a) -> (a, b)
swap (b
x,a
y) = (a
y,b
x)
applyWhen :: Bool -> (a -> a) -> a -> a
applyWhen :: forall a. Bool -> (a -> a) -> a -> a
applyWhen Bool
True a -> a
f a
x = a -> a
f a
x
applyWhen Bool
False a -> a
f a
x = a
x
intsToDigits :: [Int] -> String
intsToDigits :: [Int] -> String
intsToDigits = forall a b. (a -> b) -> [a] -> [b]
map Int -> Char
intToDigit
theLast :: Int -> [a] -> [a]
theLast :: forall a. Int -> [a] -> [a]
theLast Int
n [a]
xs = forall a. Int -> [a] -> [a]
drop (forall (t :: * -> *) a. Foldable t => t a -> Int
length [a]
xs forall a. Num a => a -> a -> a
- Int
n) [a]
xs
loop :: [a] -> [a]
loop :: forall a. [a] -> [a]
loop [] = []
loop [a]
xs = forall a. [a] -> [a]
cycle [a]
xs
none :: (a -> Bool) -> [a] -> Bool
none :: forall a. (a -> Bool) -> [a] -> Bool
none a -> Bool
p = Bool -> Bool
not forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *). Foldable t => t Bool -> Bool
or forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map a -> Bool
p