aboutsummaryrefslogtreecommitdiff
path: root/src/Chelleport/Utils.hs
blob: c15a3e8339aab2f0596fd7312553bedd5995c4f4 (plain) (blame)
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
36
37
module Chelleport.Utils where

import Control.Monad.IO.Class (MonadIO (liftIO))
import Data.List (nub)
import Data.Time.Clock.System (SystemTime (systemNanoseconds), getSystemTime)
import qualified Debug.Trace as Debug
import Foreign.C (CInt)

intToCInt :: Int -> CInt
intToCInt = fromIntegral

cIntToInt :: CInt -> Int
cIntToInt = fromIntegral

findWithIndex :: (x -> Maybe r) -> Int -> [x] -> Maybe (Int, r)
findWithIndex _predicate _index [] = Nothing
findWithIndex predicate index (x : ls) =
  case predicate x of
    Just item -> Just (index, item)
    Nothing -> findWithIndex predicate (index + 1) ls

uniq :: (Eq a) => [a] -> [a]
uniq = nub

isEmpty :: [a] -> Bool
isEmpty = null

isNotEmpty :: [a] -> Bool
isNotEmpty = not . isEmpty

benchmark :: (MonadIO m) => String -> m a -> m a
benchmark msg m = do
  start <- systemNanoseconds <$> liftIO getSystemTime
  result <- m
  end <- systemNanoseconds <$> liftIO getSystemTime
  Debug.traceM $ msg ++ " (ms): " ++ show (fromIntegral (end - start) / 1_000_000.0 :: Double)
  pure result