diff --git a/src/Data/List/Split.hs b/src/Data/List/Split.hs index 1a12f99..46951a0 100644 --- a/src/Data/List/Split.hs +++ b/src/Data/List/Split.hs @@ -61,6 +61,7 @@ module Data.List.Split ( dropInitBlank, dropFinalBlank, dropInnerBlanks, + mapSplitter, -- ** Derived combinators -- $derived diff --git a/src/Data/List/Split/Internals.hs b/src/Data/List/Split/Internals.hs index d982137..03c3431 100644 --- a/src/Data/List/Split/Internals.hs +++ b/src/Data/List/Split/Internals.hs @@ -379,6 +379,20 @@ dropFinalBlank s = s {finalBlankPolicy = DropBlank} dropInnerBlanks :: Splitter a -> Splitter a dropInnerBlanks s = s {condensePolicy = DropBlankFields} +-- | Split over a different type of element by performing a preprocessing step. +-- +-- >>> split (mapSplitter snd $ oneOf "-_") $ zip [0..] "a-bc_d" +-- [[(0,'a')],[(1,'-')],[(2,'b'),(3,'c')],[(4,'_')],[(5,'d')]] +-- +-- >>> import Data.Char (toLower) +-- >>> split (mapSplitter toLower $ dropDelims $ whenElt (== 'x')) "abXcxd" +-- ["ab","c","d"] +mapSplitter :: (b -> a) -> Splitter a -> Splitter b +mapSplitter f (Splitter d dp cp ibp fbp) = Splitter (mapDelimiter f d) dp cp ibp fbp + where + mapDelimiter :: (b -> a) -> Delimiter a -> Delimiter b + mapDelimiter g (Delimiter xs) = Delimiter $ map (. g) xs + -- ** Derived combinators -- | Drop all blank chunks from the output, and condense consecutive