r3wp [groups: 83 posts: 189283]
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

World: r3wp

[Core] Discuss core issues

Maxim
27-Oct-2006
[5885]
for a more unified conversion you could do this:


data: replace/all replace/all to-string data "^M^/" "^/" "^M" "^/"


this way if its CRLF it will strip them and if its only CR it will 
convert them.
Anton
27-Oct-2006
[5886x2]
That looks better.
(Yes, looks like to-string doesn't really do anything except change 
the datatype from binary to string.)
Maxim
27-Oct-2006
[5888x6]
that's a good thing.
otherwise, some operations within rebol would be just to crazy to 
handle.
working on EDI data, for example, the binary data is converted to 
string by PARSE... so if it tried to "fix" stuff, I could not properly 
parse it...  :-(
and EDI uses non-printable characters as separators...
and btw, I love REBOL's extended character set for words.  being 
able to use - + = ' &, etc in words just makes words so much more 
expressive.
I just discovered this week that we can use the tick in words!
Anton
27-Oct-2006
[5894]
Yes, pretty handy sometimes.
Maxim
27-Oct-2006
[5895x2]
as in :

value': 99
so this allows us to define words which pretty much equate to themselves. 
 

blue': 'blue

hehe
Gregg
27-Oct-2006
[5897x2]
Also handy for more readable English contractions. e.g. can't-do-it?: 
true
I got two REBOL functions, say, f1 and f2, which are both time-consuming.
How 
can I make them run simultaneous in the same process?


We don't have threads in REBOL (though the rumored task type in R3 
may give us this capability), so you need to do big tasks in little 
chunks, maybe FSM driven, to simulate things like co-routines.
Anton
27-Oct-2006
[5899x4]
This parse rule appears to be over twice as fast:
parse/all str [
	any [
		to "^M" here: ["^M^/" (remove here) | "^M" (change here "^/")]
	]
	to end
]
I hope it's correct.
Compared 10000 iterations on a 67k script.
Maxim
27-Oct-2006
[5903]
Carl has once said that most string iterating funcs are about equally 
fast in REBOL... the difference in this is that parse is only parsing 
the string once, whereas the previous one-liner does two replaces. 
  So I guess that's the main reason.
Anton
28-Oct-2006
[5904x5]
Makes sense.
This version corrects a bug in the above parse rule:
to-rebol-line-terminators: func [
	string [any-string!] /local here
][
	parse/all string [
		any [

   to "^M" here: ["^M^/" (remove here) :here | "^M" (change here "^/")]

   ;thru "^M" here: ["^/" (remove back here) :here | (change back here 
   "^/")]
		]
		to end
	]
]
to-rebol-line-terminators bin: #{0d} bin
(and has an alternative way, but I think it will operate minutely 
slower.)
Actually, parse version looks only about 10% faster. When there is 
no work to do it is about twice as fast.
Gabriele
28-Oct-2006
[5909x2]
if you are doing to string! instead of as-string, then a copy version 
could be faster, when there are characters to remove.
alternatively you could create a dummy port handler to let the native 
code do the job for you.
Anton
28-Oct-2006
[5911]
Hmm, both interesting ideas - much more than what I need for now 
- if the code gets any longer it's too unwieldy to be really useful. 
I will add these ideas as possible areas for investigation.
Henrik
31-Oct-2006
[5912]
anyone built a function to find duplicate elements in a block and 
return them in a separate block?
Geomol
31-Oct-2006
[5913x2]
blk: [a b b c b a]
head foreach e unique blk [alter blk e]
== [b b a]
Ups, not separate block. Use copy!
MikeL
31-Oct-2006
[5915]
Henrik.   

Andrew created Tally.r    It wasn't exactly what you asked but maybe 
there's some value there "Tallies up the values in a series,  producing 
a block of [Value Count] pairs"

http://www.rebol.org/cgi-bin/cgiwrap/rebol/view-script.r?script=tally.r
Gregg
1-Nov-2006
[5916]
; Something like this?


    split-unique: func [block [any-block!] /local uniq dupe dest] [
        uniq: copy []
        dupe: copy []
        foreach item block [
            dest: either find/only uniq item [dupe] [uniq]
            append/only dest item
        ]
        reduce [uniq dupe]
    ]
Maxim
1-Nov-2006
[5917x3]
Henrik:  are you talking about intersect ?  (part of rebol core)
>> intersect [1 2 3 4 5] [4 5 6 7 8 9]
== [4 5 6]
just reread your post... you want duplicates in the same block... 
 sorry.
Henrik
1-Nov-2006
[5920]
yes, like:
>> duplicates [1 1 2 2 3 4 5 5 6 7 8]
== [1 2 5]
Maxim
1-Nov-2006
[5921]
you got me into a spin, I rarely do this kind of fun , here is my 
best and shortest solution:

(its also 3 times faster than Geomol's, using his block data ;-)

duplicates: func [serie /local word][

 remove-each word copy serie [same? find serie word find/last serie 
 word] 
]

>> duplicates [a b b c b a]
== [a b b b a]

you can add the 'unique call to clean up the block if you like 
>> unique duplicates  [1 1 2 2 3 4 5 5 6 7 8]
== [1 2 5]
Geomol
1-Nov-2006
[5922]
This one does what you want, I think:
>> blk: [a b b c b a]
>> unique head foreach e unique blk [alter copy blk e]
== [a b]
blk is untouched.
Maxim
1-Nov-2006
[5923x2]
the competition is on  ;-)
the foreach is slow though, when compared to remove-each.
Geomol
1-Nov-2006
[5925x2]
And as a function:

find-dups: func [blk] [unique head foreach e unique blk [alter copy 
blk e]]
Maxim: Henrik didn't ask for speed! ;-)
Maxim
1-Nov-2006
[5927x2]
hehe... but I know he is addicted to it  ;-)  I had found my solution 
when I saw yours...  so I just did a benchmark for the fun of it.
you'll be intrigued to know that the unique call, is VERY slow.  
it was as long as my duplicates itself.
Geomol
1-Nov-2006
[5929]
Funny how the same thing can be done in so different ways with REBOL. 
I think, it's good. Choose the way, you like. Not many languages 
are like that. Again REBOL is much like a spoken language. You can 
say the same thing in lots of ways. Some say it with many words, 
some is short and precise, etc.
Maxim
1-Nov-2006
[5930]
you have seen the old REBOL poetry discussion?  IIRC they where on 
the mailing list... but not sure.
Geomol
1-Nov-2006
[5931]
nope
Maxim
1-Nov-2006
[5932]
some people where amazing.  using only rebol core words and valid 
syntax...
Geomol
1-Nov-2006
[5933]
:-)
Maxim
1-Nov-2006
[5934]
writing subtle lines which often had nice printed results.