module Data.StorableVector.Utility where
import qualified Data.List as List
viewListL :: [a] -> Maybe (a, [a])
viewListL [] = Nothing
viewListL (x:xs) = Just (x,xs)
viewListR :: [a] -> Maybe ([a], a)
viewListR =
List.foldr (\x -> Just . maybe ([],x) (mapFst (x:))) Nothing
nest :: Int -> (a -> a) -> a -> a
nest 0 _ x = x
nest n f x = f (nest (n1) f x)
mapPair :: (a -> c, b -> d) -> (a,b) -> (c,d)
mapPair ~(f,g) ~(x,y) = (f x, g y)
mapFst :: (a -> c) -> (a,b) -> (c,b)
mapFst f ~(x,y) = (f x, y)
mapSnd :: (b -> d) -> (a,b) -> (a,d)
mapSnd g ~(x,y) = (x, g y)
toMaybe :: Bool -> a -> Maybe a
toMaybe False _ = Nothing
toMaybe True x = Just x