module Data.List.Extra where
import Data.List ( isSuffixOf )
headEither :: [a] -> Either String a
headEither :: forall a. [a] -> Either String a
headEither (a
a:[a]
_) = a -> Either String a
forall a b. b -> Either a b
Right a
a
headEither [] = String -> Either String a
forall a b. a -> Either a b
Left String
"Empty list"
toHead :: (a -> a) -> [a] -> [a]
toHead :: forall a. (a -> a) -> [a] -> [a]
toHead a -> a
f (a
x:[a]
xs) = a -> a
f a
x a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a]
xs
toHead a -> a
_ [a]
xs = [a]
xs
toTail :: (a -> a) -> [a] -> [a]
toTail :: forall a. (a -> a) -> [a] -> [a]
toTail a -> a
f (a
x:[a]
xs) = a
x a -> [a] -> [a]
forall a. a -> [a] -> [a]
: (a -> a) -> [a] -> [a]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
f [a]
xs
toTail a -> a
_ [a]
xs = [a]
xs
stripSuffix :: String -> String -> String
stripSuffix :: String -> String -> String
stripSuffix String
suffix String
string
| String -> String -> Bool
forall a. Eq a => [a] -> [a] -> Bool
isSuffixOf String
suffix String
string = Int -> String -> String
forall a. Int -> [a] -> [a]
take (String -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length String
string Int -> Int -> Int
forall a. Num a => a -> a -> a
- String -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length String
suffix) String
string
| Bool
otherwise = String
string