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

World: r3wp

[Core] Discuss core issues

Henrik
23-Oct-2009
[14918x2]
how about:

to-object now
= make object! [
	year: 2009
	month: 10
	etc...
]
I'm going to write it up as a wish to be implemented after the crash 
is fixed :-)
Geomol
23-Oct-2009
[14920x2]
REDUCE can be used to reduce blocks, and by that evaluate parens 
within blocks:

>> reduce [(1 + 2)]
== [3]

But REDUCE won't evaluate parens themselves:

>> reduce first [(1 + 2)]
== (1 + 2)

Is that a bug? (Same in R3.)
I also find this a bit strange:

>> do first [(1 + 2)]
== 3
>> do to paren! "1 + 2"
** Script Error: + word has no context
** Near: 1 + 2
Steeve
23-Oct-2009
[14922]
I see no bugs.

REDUCE,  acts on blocks, nothing else.


TO-PAREN and TO-BLOCK on strings, act like LOAD, but don't bound 
the result in any context.


Both of them have well known behavior. It's perhaps a lack of good 
documentation but no bugs.
Geomol
23-Oct-2009
[14923]
I'm probably wondering, because parens works in many ways the same 
as blocks, but not when reduced. The same can be said about paths.

and

Parens and paths are default being evaluated (by the scanner/lexical 
analysis), blocks are not. And when you reduce blocks, parens and 
paths within the block are being evaluated. On the other hand reducing 
parens and paths won't evaluate them. I tend to agree, it's not bugs 
but lack of documentation.
amacleod
26-Oct-2009
[14924x3]
What's a quick method to find duplicates in a series?
kind of the opposite of UNIQUE
given 'a' is a block of numbers:
b: unique a foreach c b [remove a c] probe a

not pretty but works
BrianH
26-Oct-2009
[14927x3]
>> a: [a a a b b c d e e] foreach x b: unique a [remove find a x] 
unique a
== [a b e]
It only removes the first of each from a - any additional ones are 
left behind. The last unique gets rid of the remaining dups.
Your version won't work since REMOVE only takes one parameter, not 
two.
amacleod
26-Oct-2009
[14930]
I was going to add that additional unique at the end also but it 
helps for me to not only know which are duplicated but how many times...

Thanks Brian
BrianH
26-Oct-2009
[14931]
>> a: [a a a b b c d e e] remove-each x b: unique a [not find find/tail 
a x x] b
== [a b e]
Doesn't modify a :)
amacleod
26-Oct-2009
[14932]
that's handy
BrianH
26-Oct-2009
[14933x4]
Should be faster too.
If a is sorted:
>> a: [a a a b b c d e e] b: [] foreach x unique a [repend b [x 1 
+ offset? find a x find/last a x]] b
== [a 3 b 2 c 1 d 1 e 2]
Or you could use REDUCE/into or map! if you are using R3.
amacleod
26-Oct-2009
[14937]
Awsome...
BrianH
26-Oct-2009
[14938x3]
If you preallocate the result it might be faster in some cases - 
it should be twice the length of the UNIQUE a (or the saame length 
in map)
...same length if map!.
Other tricks can be used if you can't sort a.
Ashley
26-Oct-2009
[14941]
unique remove-each x a [(find a x) = find/last a x]
Ladislav
27-Oct-2009
[14942]
not that I want to do nit-picking, but it should be noted, that the 
algorithms presented above are all O(n * n), while an O(n * log-e 
n) algorithm can be written, although such an algorithm is likely 
to be a bit more complicated
Gabriele
28-Oct-2009
[14943]
I'd just SORT then PARSE...
Ashley
28-Oct-2009
[14944]
Sounds like a new puzzle entry! ;)
Maxim
28-Oct-2009
[14945]
hehe
BudzinskiC
28-Oct-2009
[14946]
Puzzle entry.. Is there a code golf website for REBOL? Like codegolf.com 
? I did those with Ruby, that was pretty fun.
Sunanda
28-Oct-2009
[14947x2]
No really.....There is Puzzles and Puzzle Answers here, but they 
aren't very web-public.

There have been some nice puzzles on the mailing list:
   http://www.rebol.org/ml-topic-detail.r?l=p&topic=puzzle
The big problem with most code golf puzzles is they only ever play 
one one......It'd be better to have puzzles that are rolled out in 
stages: each stage being a modification to the previous puzzle spec. 
that way, generality and flexibility would be rewarded.
BudzinskiC
28-Oct-2009
[14949]
Yeah, usually the focus lies on code size (who writes the tiniest 
solution) which isn't always the best solution to a problem. Well, 
there aren't "best solutions" anyhow because it always depends on 
what you need (speed/stability/security/readability/etc.), often 
enough you need to make a tradeoff there so you can't really say 
one is best. But your idea would be a good start to improve code 
golfing. Now you just need to make a nice website that I can visit 
;)
Sunanda
28-Oct-2009
[14950]
...switching to [All] for the code golf discussion.....
Izkata
29-Oct-2009
[14951]
Is there a simple way to convert a number to its binary representation?

This is what I want, but without the limit of 255 that char! types 
have:
>> enbase/base to-binary to-char 7 2
== "00000111"
Sunanda
30-Oct-2009
[14952]
This does it, though there may be faster native ways for the specific 
case of decimal-->binary@
   http://www.rebol.org/documentation.r?script=base-convert.r
Maxim
30-Oct-2009
[14953x2]
found a bug in 'FOR when used in series... it becomes an endless 
loop..
>> s: "12345"
== "12345"
>> for c s tail s 1 [probe c]
12345
2345
345
45
5
; <----- it should stop here 




....
Steeve
30-Oct-2009
[14955]
Izkata, it's a little tricky with R2, but this works for any integer:

>> enbase/base debase/base to-hex 546 16 2
== "00000000000000000000001000100010"
Izkata
30-Oct-2009
[14956]
Thanks Sunanda, Steeve - I had a loop to generate it manually, but 
an enbase/debase-based solution is far faster  ;)
Geomol
30-Oct-2009
[14957]
Izkata, there's a library of bit operations with several useful functions, 
also one that can do, what you need:

>> do http://www.fys.ku.dk/~niclasen/rebol/libs/bit.r
>> enbit 546
== "00000000000000000000001000100010"

It's BSD license, so you can use it in your work.
Gabriele
31-Oct-2009
[14958]
Max, I think that has been reported many times, eg. http://www.rebol.net/cgi-bin/rambo.r?id=4121&
Henrik
2-Nov-2009
[14959]
Normal zero padding of decimals:

>> to-string .48
== "0.48"

Is it possible do to something like this:

>> magic-function .48
== ".48"


This would help in cases where you need to build a query string to 
search for plain text.
Gabriele
2-Nov-2009
[14960x2]
maybe something like:

magic-function: func [num [number!] /local result] [
    result: form num
    if #"0" = first result [remove result]
    result
]
you may want to handle cases like "0" specially, depending on what 
your exact needs are.
Henrik
2-Nov-2009
[14962x2]
hmm... thanks. it seems that the problem is a little different than 
that, however.


I'm just loading a string: "this-column contains 001" which is loaded 
incorrectly to [this-column contains 1], so I have to do this differently 
in the loading phase.
Ah, the user should just use quotes.
Graham
3-Nov-2009
[14964]
I've forgotten .. is there a sort that sorts on the basis of the 
length of strings?
BrianH
4-Nov-2009
[14965x2]
>> sort/compare ["1" "22" "333"] func [a b] [greater? length? :a 
length? :b]
== ["333" "22" "1"]
Actually, to make that a stable sort it should be greater-or-equal? 
instead.
Graham
4-Nov-2009
[14967]
cool ...