Re: Elementary but surprisingly difficult.
- From: Doug Hoffman <dhoffman@xxxxxxxxxxxxxxx>
- Date: Sat, 24 May 2008 19:52:43 -0700 (PDT)
On May 24, 9:18 pm, Doug Hoffman <no.spam> wrote:
Slava Pestov wrote:
On May 24, 4:36 pm, Bernd Paysan <bernd.pay...@xxxxxx> wrote:
: uniquify ( addr u -- addr u' )
over >r bounds dup dup c@ 2swap 1+ ?DO
I c@ tuck <> IF swap 1+ 2dup c! swap THEN LOOP
drop r> swap over - ;
This is why stack-based languages have a reputation of unreadability.
I both agree and disagree with you in this case. It is not the kind of
code I would normally write, especially if in a hurry to get the job
done. Of course I could spend the time to pick it apart and see exactly
what is going (on). But why bother? The word works. Some people, like
Bernd, can probably glance at code like this and easily/quickly see
exactly what makes it tick. I am not one of those people, even though I
like and use Forth.
Having said that, I thought I should show the way I would normally
attack this kind of problem (context is resource-rich pc
emvironment). The Mops Neon-based object library has some powerful
string handling methods. So I would start with that and come up with
something like the following. It may not be as readable to you as
your Factor solution. But then for *me* this is far more readable
than the Factor code because I know nothing about Factor.
(Note that Mops string objects maintain 2 pointers into the string,
position and limit, or pos and lim. Manipulation of these pointers is
the key to using string objects.)
:class ustring super{ string+ }
:m 2nd: ( -- c) \ 2nd char
1 skip: self 1st: self ;m
:m del1: 1 deleten: self ;m
:m backup1: pos: self 1 - >pos: self ;m
:m unique:
BEGIN
pos: self 1 + lim: self <
WHILE
1st: self 2nd: self =
IF del1: self backup1: self THEN
REPEAT ;m
;class
ustring s
" abbcdefffghiijkkkkkkklmnooz" put: s
unique: s
all: s type
abcdefghijklmnoz ok
-Doug
.
- References:
- Elementary but surprisingly difficult.
- From: Albert van der Horst
- Re: Elementary but surprisingly difficult.
- From: Slava Pestov
- Re: Elementary but surprisingly difficult.
- From: Bernd Paysan
- Re: Elementary but surprisingly difficult.
- From: Slava Pestov
- Re: Elementary but surprisingly difficult.
- From: Doug Hoffman
- Elementary but surprisingly difficult.
- Prev by Date: Re: Elementary but surprisingly difficult.
- Next by Date: Re: Elementary but surprisingly difficult.
- Previous by thread: Re: Elementary but surprisingly difficult.
- Next by thread: Re: Elementary but surprisingly difficult.
- Index(es):
Relevant Pages
|