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

Add a function to split text at the first occurrence of a character #625

Open
vsannier opened this issue Jan 12, 2025 · 1 comment
Open

Comments

@vsannier
Copy link

I was surprised not to find a function in the library to split a text on the first match of a character/text/predicate, with the matched part discarded.
This is not difficult to write using breakOn and drop, but I think a function implemented with the internals of the library might have better performance.

split1 :: Char -> Text -> (Text, Text)
split1 c = (id *** drop 1) . (breakOn $ singleton c)

See a similar function (previously called splitOnce) in the byteslice package, and a question on Stack Overflow about splitAtfirst.

@Bodigrim
Copy link
Contributor

IMHO split1 c = fmap (drop 1) . break (== c) is short enough and does not suffer from any significant performance penalty: drop 1 is constant-time, negligible in comparison to the linear time of break.

Note that the function from byteslice has a different signature, with Maybe.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants