{-# LANGUAGE CPP #-}
module Conjure.Utils
( module Data.List
, module Data.Function
, module Data.Maybe
, module Data.Monoid
, module Data.Tuple
, module Data.Typeable
, count
, nubOn
, nubSort
, mzip
, groupOn
#if __GLASGOW_HASKELL__ < 710
, sortOn
#endif
, idIO
, mapHead
, sets
, headOr
, allEqual
, choices
, choicesThat
, foldr0
)
where
import Data.List
import Data.Function
import Data.Maybe
import Data.Monoid
import Data.Tuple
import Data.Typeable
import System.IO.Unsafe
allEqual :: Eq a => [a] -> Bool
allEqual :: [a] -> Bool
allEqual [] = Bool
False
allEqual [a
x] = Bool
False
allEqual [a
x,a
y] = a
x a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
y
allEqual (a
x:a
y:[a]
xs) = a
x a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
y Bool -> Bool -> Bool
&& [a] -> Bool
forall a. Eq a => [a] -> Bool
allEqual (a
ya -> [a] -> [a]
forall a. a -> [a] -> [a]
:[a]
xs)
count :: (a -> Bool) -> [a] -> Int
count :: (a -> Bool) -> [a] -> Int
count a -> Bool
p = [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length ([a] -> Int) -> ([a] -> [a]) -> [a] -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Bool) -> [a] -> [a]
forall a. (a -> Bool) -> [a] -> [a]
filter a -> Bool
p
nubOn :: Eq b => (a -> b) -> [a] -> [a]
nubOn :: (a -> b) -> [a] -> [a]
nubOn a -> b
f = (a -> a -> Bool) -> [a] -> [a]
forall a. (a -> a -> Bool) -> [a] -> [a]
nubBy (b -> b -> Bool
forall a. Eq a => a -> a -> Bool
(==) (b -> b -> Bool) -> (a -> b) -> a -> a -> Bool
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` a -> b
f)
nubSort :: Ord a => [a] -> [a]
nubSort :: [a] -> [a]
nubSort = [a] -> [a]
forall a. Eq a => [a] -> [a]
nnub ([a] -> [a]) -> ([a] -> [a]) -> [a] -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [a] -> [a]
forall a. Ord a => [a] -> [a]
sort
where
nnub :: [a] -> [a]
nnub [] = []
nnub [a
x] = [a
x]
nnub (a
x:[a]
xs) = a
x a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a] -> [a]
nnub ((a -> Bool) -> [a] -> [a]
forall a. (a -> Bool) -> [a] -> [a]
dropWhile (a -> a -> Bool
forall a. Eq a => a -> a -> Bool
==a
x) [a]
xs)
mzip :: Monoid a => [a] -> [a] -> [a]
mzip :: [a] -> [a] -> [a]
mzip [] [] = []
mzip [] [a]
ys = [a]
ys
mzip [a]
xs [] = [a]
xs
mzip (a
x:[a]
xs) (a
y:[a]
ys) = a
x a -> a -> a
forall a. Semigroup a => a -> a -> a
<> a
y a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a] -> [a] -> [a]
forall a. Monoid a => [a] -> [a] -> [a]
mzip [a]
xs [a]
ys
groupOn :: Eq b => (a -> b) -> [a] -> [[a]]
groupOn :: (a -> b) -> [a] -> [[a]]
groupOn a -> b
f = (a -> a -> Bool) -> [a] -> [[a]]
forall a. (a -> a -> Bool) -> [a] -> [[a]]
groupBy (b -> b -> Bool
forall a. Eq a => a -> a -> Bool
(==) (b -> b -> Bool) -> (a -> b) -> a -> a -> Bool
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` a -> b
f)
#if __GLASGOW_HASKELL__ < 710
sortOn :: Ord b => (a -> b) -> [a] -> [a]
sortOn f = sortBy (compare `on` f)
#endif
idIO :: (a -> IO ()) -> a -> a
idIO :: (a -> IO ()) -> a -> a
idIO a -> IO ()
action a
x = IO a -> a
forall a. IO a -> a
unsafePerformIO (IO a -> a) -> IO a -> a
forall a b. (a -> b) -> a -> b
$ do
a -> IO ()
action a
x
a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return a
x
mapHead :: (a -> a) -> [a] -> [a]
mapHead :: (a -> a) -> [a] -> [a]
mapHead a -> a
f (a
x:[a]
xs) = a -> a
f a
x a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a]
xs
sets :: [a] -> [[a]]
sets :: [a] -> [[a]]
sets [] = [[]]
sets (a
x:[a]
xs) = ([a] -> [a]) -> [[a]] -> [[a]]
forall a b. (a -> b) -> [a] -> [b]
map (a
xa -> [a] -> [a]
forall a. a -> [a] -> [a]
:) [[a]]
ss [[a]] -> [[a]] -> [[a]]
forall a. [a] -> [a] -> [a]
++ [[a]]
ss
where
ss :: [[a]]
ss = [a] -> [[a]]
forall a. [a] -> [[a]]
sets [a]
xs
headOr :: a -> [a] -> a
headOr :: a -> [a] -> a
headOr a
x [] = a
x
headOr a
_ (a
x:[a]
xs) = a
x
choices :: [a] -> [(a,[a])]
choices :: [a] -> [(a, [a])]
choices [] = []
choices (a
x:[a]
xs) = (a
x,[a]
xs) (a, [a]) -> [(a, [a])] -> [(a, [a])]
forall a. a -> [a] -> [a]
: ((a, [a]) -> (a, [a])) -> [(a, [a])] -> [(a, [a])]
forall a b. (a -> b) -> [a] -> [b]
map (([a] -> [a]) -> (a, [a]) -> (a, [a])
forall t b a. (t -> b) -> (a, t) -> (a, b)
mapSnd (a
xa -> [a] -> [a]
forall a. a -> [a] -> [a]
:)) ([a] -> [(a, [a])]
forall a. [a] -> [(a, [a])]
choices [a]
xs)
where
mapSnd :: (t -> b) -> (a, t) -> (a, b)
mapSnd t -> b
f (a
x,t
y) = (a
x,t -> b
f t
y)
choicesThat :: (a -> [a] -> Bool) -> [a] -> [(a,[a])]
choicesThat :: (a -> [a] -> Bool) -> [a] -> [(a, [a])]
choicesThat a -> [a] -> Bool
(?) = ((a, [a]) -> Bool) -> [(a, [a])] -> [(a, [a])]
forall a. (a -> Bool) -> [a] -> [a]
filter ((a -> [a] -> Bool) -> (a, [a]) -> Bool
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry a -> [a] -> Bool
(?)) ([(a, [a])] -> [(a, [a])])
-> ([a] -> [(a, [a])]) -> [a] -> [(a, [a])]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [a] -> [(a, [a])]
forall a. [a] -> [(a, [a])]
choices
foldr0 :: (a -> a -> a) -> a -> [a] -> a
foldr0 :: (a -> a -> a) -> a -> [a] -> a
foldr0 a -> a -> a
f a
z [a]
xs | [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [a]
xs = a
z
| Bool
otherwise = (a -> a -> a) -> [a] -> a
forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
foldr1 a -> a -> a
f [a]
xs