summaryrefslogtreecommitdiffstats
path: root/src/utils.hs
blob: 7618f5b7c0db5778776e0f2df2c0485c04568b1c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
module Utils where

import qualified Data.Bifunctor as B

(.>) = 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

breakOn :: (a -> Bool) -> [a] -> ([a], [a])
breakOn cond xs = break cond xs $> B.second (drop 1)