File: Split.hs

package info (click to toggle)
git-annex 10.20250416-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 73,572 kB
  • sloc: haskell: 90,656; javascript: 9,103; sh: 1,469; makefile: 211; perl: 137; ansic: 44
file content (39 lines) | stat: -rw-r--r-- 950 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
{- split utility functions
 -
 - Copyright 2017 Joey Hess <id@joeyh.name>
 -
 - License: BSD-2-clause
 -}

{-# OPTIONS_GHC -fno-warn-tabs #-}

module Utility.Split (
	split,
	splitc,
	replace,
	dropFromEnd,
) where

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

-- | Only traverses the list once while dropping the last n items.
dropFromEnd :: Int -> [a] -> [a]
dropFromEnd n l = zipWith const l (drop n l)