Re: Elementary but surprisingly difficult.



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
.



Relevant Pages

  • Re: Operator overloading in C
    ... All development of C as an independent language has ... making any changes or improvements to the standard ... The lack of a counted string data structure, ... Pointers can't be used for arg1 or arg2. ...
    (comp.std.c)
  • Re: get text from listbox
    ... Dim nCount As Long ... Dim Buffer As String ... The first character is the low byte of the low word of the DWORD item data ... To prove that these are pointers get the item data of the list items as ...
    (microsoft.public.vb.winapi)
  • Re: Comments requested: brief summary of C
    ... ``A char holds a single byte. ... ``A string is a contiguous sequence of characters terminated ... pointed to by city. ... pointers and arrays are often used ...
    (comp.lang.c)
  • Re: Another Understanding Pointers Question
    ... > string functions to test. ... Again, why increment j? ... a good use of pointers. ... darrell at cs dot toronto dot edu ...
    (comp.lang.c)
  • Re: get text from listbox
    ... The first character is the low byte of the low word of the DWORD item data ... addressing memory in a virtual address space of 2 GB (memory allocation ... To prove that these are pointers get the item data of the list items as ... string pointer; check it by reading some bytes from the memory to which the ...
    (microsoft.public.vb.winapi)