summaryrefslogtreecommitdiffstats
path: root/src/Utils.hs
diff options
context:
space:
mode:
authorJan Tuomi <jans.tuomi@gmail.com>2022-02-06 19:05:46 +0200
committerJan Tuomi <jans.tuomi@gmail.com>2022-02-06 20:03:27 +0200
commitd5fe35bb8a75a74c7b3616b864c4d35bafecaadd (patch)
tree410325cafa976dfa0596872d8c0582e1b36772c0 /src/Utils.hs
parentc2053ee0585280229375b10283cb9e8dbb903734 (diff)
Add proper cabal setup
Diffstat (limited to 'src/Utils.hs')
-rw-r--r--src/Utils.hs66
1 files changed, 66 insertions, 0 deletions
diff --git a/src/Utils.hs b/src/Utils.hs
new file mode 100644
index 0000000..96f5fcb
--- /dev/null
+++ b/src/Utils.hs
@@ -0,0 +1,66 @@
+module Utils where
+
+import Control.Monad.Except
+import qualified Data.Bifunctor as B
+import Data.Text (Text)
+import qualified Data.Text as T
+
+(.>) = flip (.)
+
+($>) = flip ($)
+
+infixr 6 $>
+
+countCond :: (a -> Bool) -> [a] -> Int
+countCond cond list = filter cond list $> length
+
+occursTimes :: Eq a => a -> [a] -> Int
+occursTimes needle = countCond (== needle)
+
+readMaybe :: (Read a) => String -> Maybe a
+readMaybe s = case reads s of
+ [(x, "")] -> Just x
+ _ -> Nothing
+
+isFloatStr str =
+ let ir = readMaybe str :: Maybe Double
+ in case ir of
+ Just _ -> True
+ Nothing -> False
+
+isIntStr str =
+ let ir = readMaybe str :: Maybe Integer
+ in case ir of
+ Just _ -> True
+ Nothing -> False
+
+isCharStr str =
+ case str of
+ ['\'', c, '\''] -> True
+ _ -> False
+
+breakOn :: (a -> Bool) -> [a] -> ([a], [a])
+breakOn cond xs = break cond xs $> B.second (drop 1)
+
+mix :: [a] -> [a] -> [a]
+mix (x : xs) (y : ys) = x : y : mix xs ys
+mix x [] = x
+mix [] y = y
+
+safeBreak cond ex = safeBreak' cond ex []
+
+safeBreak' _ ex _ [] = throwError ex
+safeBreak' cond ex acc lst@(x : xs)
+ | cond x = pure (reverse acc, lst)
+ | otherwise = safeBreak' cond ex (x : acc) xs
+
+fmt :: String -> [String] -> String
+fmt str values = T.unpack $ fmt' (T.pack str) (map T.pack values)
+
+fmt' :: Text -> [Text] -> Text
+fmt' text [] = text
+fmt' text (v : rest) =
+ let pattern = T.pack "%%"
+ (front, back) = T.breakOn pattern text
+ res = T.concat [front, v, T.drop (T.length pattern) back]
+ in fmt' res rest