[Prev][Next][Index][Thread]
Re: Strtok/split?
In article <3A3F90D8.69545A8A@h2g2.com>,
robm@h2g2.com wrote:
>
> What's the best way of reproducing the functionality of C's strtok()
> and/or Perl's split() in Dylan?
> I need to tokenize a string.
There is a function tokenize-environment-variable in
"Functional Developer\Sources\lib\operating-system\tokenize-
variable.dylan"
which does nearly the same as strtok, you only need to copy it and give
more universal names to the function, the parameter, and the delimiter.
It's working, but I don't like it very much, because it is implemented
in a pure imperative style. Here is a different version in a pure
functional style:
define method tokenize-string
(string :: <string>, #key delimiter :: <character> = ' ')
=> (strings :: <sequence>);
let len = size (string);
for (i from len to -1 by -1,
match? :: <boolean> = #f then i < 1 | string[i - 1] = delimiter,
collect :: <list> = #() then
if (match? & i + 1 < bound)
let token = copy-sequence (string, start: i+1, end: bound);
pair (token, collect)
else
collect
end,
bound :: <integer> = len then if (match?) i else bound end)
finally collect
end
end method;
Surely someone with more experience in functional programming can do it
better. If anybody has any ideas for improvements, please post them, so
that I can learn from it.
---
Helmut
Sent via Deja.com
http://www.deja.com/
References: