Implement combining of items
This commit is contained in:
@@ -3,6 +3,7 @@ module ListHelp
|
||||
,initOrNull
|
||||
, insertOver
|
||||
, swapIndices
|
||||
, (!?)
|
||||
)where
|
||||
import Data.List
|
||||
|
||||
@@ -15,3 +16,18 @@ insertOver i x xs = take i xs ++ x : drop (i+1) xs
|
||||
|
||||
swapIndices :: Int -> Int -> [a] -> [a]
|
||||
swapIndices i j xs = insertOver i (xs !! j) . insertOver j (xs !! i) $ xs
|
||||
|
||||
--copied from Data.List.Extra:
|
||||
-- | A total variant of the list index function `(!!)`.
|
||||
--
|
||||
-- > [2,3,4] !? 1 == Just 3
|
||||
-- > [2,3,4] !? (-1) == Nothing
|
||||
-- > [] !? 0 == Nothing
|
||||
(!?) :: [a] -> Int -> Maybe a
|
||||
xs !? n
|
||||
| n < 0 = Nothing
|
||||
-- Definition adapted from GHC.List
|
||||
| otherwise = foldr (\x r k -> case k of
|
||||
0 -> Just x
|
||||
_ -> r (k-1)) (const Nothing) xs n
|
||||
{-# INLINABLE (!?) #-}
|
||||
|
||||
Reference in New Issue
Block a user