{-# LANGUAGE Safe #-}
module Relude.List
( module Relude.List.Reexport
, module Relude.List.NonEmpty
, (!!?)
, maybeAt
, partitionWith
) where
import Relude.Base ((<))
import Relude.Bool (otherwise)
import Relude.Function (flip, (.))
import Relude.List.NonEmpty
import Relude.List.Reexport
import Relude.Monad (Either, Maybe (..), partitionEithers)
import Relude.Numeric (Int, (-))
infix 9 !!?
(!!?) :: [a] -> Int -> Maybe a
!!? :: forall a. [a] -> Int -> Maybe a
(!!?) [a]
xs Int
i
| Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0 = Maybe a
forall a. Maybe a
Nothing
| Bool
otherwise = Int -> [a] -> Maybe a
forall a. Int -> [a] -> Maybe a
go Int
i [a]
xs
where
go :: Int -> [a] -> Maybe a
go :: forall a. Int -> [a] -> Maybe a
go Int
0 (a
x:[a]
_) = a -> Maybe a
forall a. a -> Maybe a
Just a
x
go Int
j (a
_:[a]
ys) = Int -> [a] -> Maybe a
forall a. Int -> [a] -> Maybe a
go (Int
j Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) [a]
ys
go Int
_ [] = Maybe a
forall a. Maybe a
Nothing
{-# INLINE (!!?) #-}
maybeAt :: Int -> [a] -> Maybe a
maybeAt :: forall a. Int -> [a] -> Maybe a
maybeAt = ([a] -> Int -> Maybe a) -> Int -> [a] -> Maybe a
forall a b c. (a -> b -> c) -> b -> a -> c
flip [a] -> Int -> Maybe a
forall a. [a] -> Int -> Maybe a
(!!?)
{-# INLINE maybeAt #-}
partitionWith :: (a -> Either b c) -> [a] -> ([b], [c])
partitionWith :: forall a b c. (a -> Either b c) -> [a] -> ([b], [c])
partitionWith a -> Either b c
f = [Either b c] -> ([b], [c])
forall a b. [Either a b] -> ([a], [b])
partitionEithers ([Either b c] -> ([b], [c]))
-> ([a] -> [Either b c]) -> [a] -> ([b], [c])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Either b c) -> [a] -> [Either b c]
forall a b. (a -> b) -> [a] -> [b]
map a -> Either b c
f
{-# INLINE partitionWith #-}