You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add a function to split text at the first occurrence of a character #625
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***drop1) . (breakOn $ singleton c)
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.
Activity
Bodigrim commentedon Jan 13, 2025
IMHO
split1 c = fmap (drop 1) . break (== c)is short enough and does not suffer from any significant performance penalty:drop 1is constant-time, negligible in comparison to the linear time ofbreak.Note that the function from
byteslicehas a different signature, withMaybe.sol commentedon Apr 5, 2025
@Bodigrim comparing
vs
The first option, using
breakOn, should be significantly faster than the second option, no?Rational:
breakOncan look at the needle and choose the optimal strategy (specifically ifcis in the ASCII range, it can just do a sequential scan).breakon the other hand always needs to decode toChar.sol commentedon Apr 5, 2025
Corresponding code:
text/src/Data/Text/Internal/Search.hs
Lines 49 to 56 in 5e57460
text/src/Data/Text/Internal/Search.hs
Lines 93 to 100 in 5e57460
Bodigrim commentedon Apr 5, 2025
@sol yes, I'd expect
breakOnto be faster.