[racket-dev] `string-split'

From: Sam Tobin-Hochstadt (samth at ccs.neu.edu)
Date: Thu Apr 19 08:28:04 EDT 2012

On Thu, Apr 19, 2012 at 8:21 AM, Eli Barzilay <eli at barzilay.org> wrote:
>
> Two hours ago, Laurent wrote:
>> One string function that I often find useful in various scripting
>> languages is a `string-split' (explode in php).  It can be done with
>> `regexp-split', but having something more along the lines of a
>> `string-split' should belong to a racket/string lib I think.  Plus
>> it would be symmetric with `string-join', which already is in
>> racket/ string (or at least a doc line pointing to regexp-split
>> should be added there).
>
> If you mean something like this:
>
>  (define (string-split str) (regexp-match* #px"\\S+" str))
>
> ?
>
> If so, then I see a much weaker point for it -- unlike other small
> utilities, this one doesn't even compose two function calls.

It composes one function call (with an extremely complex API) with one
domain-specific language (that lots of people don't
know/understand/use) into one extremely simple but useful function.

> The very weak point here is if you want a default argument that
> specifies the gaps to split on rather than the words:
>
>  (define (string-split str [sep #px"\\s+"])
>    (remove* '("") (regexp-split sep str)))
>
> but that *does* use regexps, so I don't see the point, still...

Note that (string-split str ";") works given that implementation,
which I think makes it both easy-to-understand and useful.
-- 
sam th
samth at ccs.neu.edu


Posted on the dev mailing list.