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

World: r3wp

[Core] Discuss core issues

Cyphre
21-Jul-2005
[1555x2]
In this case it would work:
>> x: context [e: 5 d: does [print e]]
>> y: make x [e: 1]
>> y/d
1
because the 'e in function d would be bound to the context of the 
object (self)
Rebolek
21-Jul-2005
[1557x2]
yes I know, I needed it when extending objects
so I'll use 'self or define everything in advance
Cyphre
21-Jul-2005
[1559]
If you use 'self you are safe rearding contexts IMO.
Ladislav
21-Jul-2005
[1560x2]
if you want to extend context, you might want to try associative 
array instead
or a "dynamic" object like o: make object! [data: make object! [x: 
1]]

usage:

>> o/data/x
== 1
>> o/data: make o/data [y: 5]
>> o/data/x
== 1
>> o/data/y
== 5
Joe
23-Jul-2005
[1562]
.
Ingo
25-Jul-2005
[1563x4]
Q1: I want to replace all 'none in a block with _different_ empty 
strings, what's the fastest way to do this?

(replace/all BLOCK none ""       replaces all 'none with the same 
empty string)
Q2: I have to blocks containing strings, and want to find out which 
of these strings differ (I need all differing positions), what do 
you think is the fastest way to achieve this?
Thanks in advance for all ideas!
PS. Do you, like me, feel that the replace way of doing things is 
questionable?
Volker
25-Jul-2005
[1567]
b: ["s1" "s2" "s3" "s1"]
parse b[any[to "s1" p: (p/1: copy "t0") skip]]
? b
Ingo
25-Jul-2005
[1568]
Q2 again: Sorry, my axplanaition was a little unclear: 

I have a row from a database, and store away one block, and display 
the other for the user. The user may, or may not, change the data.
>> orig: ["Mr" "Petr" "Ustinov"]
>> data: ["Sir" "Peter" "Ustinov"]
>> magic-changed-func orig data
== [1 2]
Volker
25-Jul-2005
[1569]
orig: ["Mr" "Petr" "Ustinov"]
data: ["Sir" "Peter" "Ustinov"]
; same length !
out: copy[]
repeat i length? orig [ 
	if orig/:i <> data/:i [
		append out i
	]
] 
? out
BrianH
25-Jul-2005
[1570x3]
Q1:use [x] [
Sorry
Q1:
use [x] [
    x: block
    while [x: find x none!] [change x copy ""]
]
Anton
26-Jul-2005
[1573x2]
Ingo, I have similar feelings too sometimes. I need a higher level 
function to do some things like that, but it's not there....
I often wanted reduce and compose to not make a copy for you, but 
work directly on the block. (Perhaps new functions "induce" and "impose" 
?)
Sunanda
26-Jul-2005
[1575]
induce should be fairly easy -- walk the block with 'for and 'poke 
back the value
Anton
26-Jul-2005
[1576x3]
blk: [a b c] 
format: [(1 + 2) (random 100) c]

impose: func [blk format][repeat n length? format [if paren? format/:n 
[poke blk n do format/:n]]]
impose blk format
;blk == [3 95 c]
For induce I think I would use do/next.
induce: func [blk format][change blk reduce format]  ; (except, as 
a native!, without creating a temporary block)
induce blk [1 + 2 random 100 'c]
; == [3 67 c]
Ingo
26-Jul-2005
[1579x2]
Hi Voker, BrianH, thanks for your ideas ... they look mighty long 
compared to normal Rebol code ;-)
Sorry, I wanted to type Volker, of course!
Brett
26-Jul-2005
[1581x3]
; Q1: copy/deep will give you new strings:
block: [none none none]
new-block: copy/deep replace/all block 'none {}
; Q1 Or if you know you can reduce it you could do something obtuse 
like:
block: ["a" none "b" none none]
use [none][none: does [copy {}] bind block 'none]
reduce block
But if you do need to do an actual replace on the original block 
- see the other solutions :-)
Ingo
27-Jul-2005
[1584]
Hi Brett, your first version looks pretty good ... now I'll have 
to dig out my little profiler, and see what's the fastest of all 
of these ;-)
Anton
27-Jul-2005
[1585x4]
I reckon the parse will win.
Brett's is the nicest looking though.
Brett, your obtuse way will only work if you are replacing 'none 
(as a word), but not none! values.
eg. reduce your input block:
	block: reduce ["a" none "b" none none]
Brett
27-Jul-2005
[1589]
Agreed Anton and perhaps that shows the obtuse method is not a great 
idea for this situation  - though perhaps an interesting one ;-)
Gabriele
29-Jul-2005
[1590]
In case anyone is interested... http://www.rebol.org/cgi-bin/cgiwrap/rebol/view-script.r?script=reboldiff.r
Henrik
30-Jul-2005
[1591]
Are there any other instances where the a: copy [] vs. a: [] "problem" 
appears?

I'm have a bug where two arrays:

1. an array with data that can be manipulated

2. a similar one containing default data, which is used whenever 
I want to reset the first one


They apparently "stick" together with synchronized values. When I 
manipulate array 1, array 2 changes too. This would be the old COPY 
problem, but I use COPY everywhere, whenever I need to create array 
2.


However I do frequently PICK values from array 2 and POKE it in array 
1 at the same location to reset a specific location to a default 
value. Would that create a similar problem?
Gabriele
30-Jul-2005
[1592x2]
it depends on the kind of value you are picking/poking.
i guess you need copy/deep and poke ... copy pick ...
Henrik
30-Jul-2005
[1594]
I pick and poke arrays
Gabriele
30-Jul-2005
[1595]
i mean, what is the result of PICK? i.e. what is inside the arrays?
Volker
30-Jul-2005
[1596]
if the arrays have multiple dimensions, you need copy/deep. And if 
there are objects inside, those objects are not copied, then you 
need explicit work (copying all objects in a loop using 'make)
Henrik
30-Jul-2005
[1597x2]
Volker, the arrays are indeed both 2 dimensional and contain objects. 
I'll try a different init method.
gabriele, the arrays contain other arrays, hence PICK returns arrays 
of objects.
Gabriele
30-Jul-2005
[1599]
so, copy/deep, and, as volker suggests, you're likely to need to 
clone the objects too.
Henrik
31-Jul-2005
[1600]
thanks, both. problem solved. :-)
Henrik
6-Aug-2005
[1601]
a one liner I didn't see on rebol.com: Sum of all numbers in a block!:

do do replace/all mold [1 2 3 4 5] " " " + "
Sunanda
7-Aug-2005
[1602]
Clever!
But crucially dependent on the block being contained on one line:
 xx: {
 do do replace/all mold [1 2 3
  4 5] " " " + "
 }
 do xx

But then you did call it a one-liner :-)
Henrik
7-Aug-2005
[1603]
do trim/lines xx could fix that
Volker
7-Aug-2005
[1604]
Is there a function which cuts the suffix, so i can go from %file.txt 
to %file.html ? I  remember darkly there is one now.