2017-05-16 03:32:17 +00:00
|
|
|
{- split utility functions
|
|
|
|
-
|
|
|
|
- Copyright 2017 Joey Hess <id@joeyh.name>
|
|
|
|
-
|
|
|
|
- License: BSD-2-clause
|
|
|
|
-}
|
|
|
|
|
|
|
|
{-# OPTIONS_GHC -fno-warn-tabs #-}
|
|
|
|
|
2019-11-23 15:07:22 +00:00
|
|
|
module Utility.Split (
|
|
|
|
split,
|
|
|
|
splitc,
|
|
|
|
replace,
|
|
|
|
dropFromEnd,
|
|
|
|
) where
|
2017-05-16 03:32:17 +00:00
|
|
|
|
|
|
|
import Data.List (intercalate)
|
|
|
|
import Data.List.Split (splitOn)
|
|
|
|
|
|
|
|
-- | same as Data.List.Utils.split
|
|
|
|
--
|
|
|
|
-- intercalate x . splitOn x === id
|
|
|
|
split :: Eq a => [a] -> [a] -> [[a]]
|
|
|
|
split = splitOn
|
|
|
|
|
|
|
|
-- | Split on a single character. This is over twice as fast as using
|
|
|
|
-- split on a list of length 1, while producing identical results. -}
|
|
|
|
splitc :: Eq c => c -> [c] -> [[c]]
|
|
|
|
splitc c s = case break (== c) s of
|
|
|
|
(i, _c:rest) -> i : splitc c rest
|
|
|
|
(i, []) -> i : []
|
|
|
|
|
|
|
|
-- | same as Data.List.Utils.replace
|
|
|
|
replace :: Eq a => [a] -> [a] -> [a] -> [a]
|
|
|
|
replace old new = intercalate new . split old
|
2018-11-23 15:24:05 +00:00
|
|
|
|
2019-11-27 20:38:18 +00:00
|
|
|
-- | Only traverses the list once while dropping the last n items.
|
2018-11-23 15:24:05 +00:00
|
|
|
dropFromEnd :: Int -> [a] -> [a]
|
|
|
|
dropFromEnd n l = zipWith const l (drop n l)
|