Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement "mapSplitter" #22

Merged
merged 2 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Data/List/Split.hs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ module Data.List.Split (
dropInitBlank,
dropFinalBlank,
dropInnerBlanks,
mapSplitter,

-- ** Derived combinators
-- $derived
Expand Down
14 changes: 14 additions & 0 deletions src/Data/List/Split/Internals.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading