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

World: r3wp

[Rebol School] Rebol School

BrianH
8-Mar-2010
[2933]
How to read the profiles above: The first is nearly twice the speed 
of the second, but creates more temporary memory.
PatrickP61
8-Mar-2010
[2934]
Ok, I get the if x is modified it won't change the original,

What I don't get is that and empty block [ ] is just empty.  It is 
not like a word or anything is it?

Yes, i did see the performance numbers.  that is good to see!
BrianH
8-Mar-2010
[2935x2]
The empty block is a value, even if it doesn't contain other values, 
and it is a value that can be modified.
I profile code patterns all the time, and when writing functions 
I use the best code patterns. This leads to better functions, even 
if you don't profile the whole function (which you can't always do).
PatrickP61
8-Mar-2010
[2937x2]
Ok just so I have this. 
x:	copy [ ]    will copy an empty block that x is refered to
y:	[ ] 	   will be a reference to an empty block


how is it possible to modify an empty block without referencing it?

 y: [ ]		<-- ref empty
== []

>> append "hi" y	<-- changes that empty block
== "hi"

>> x: [ ]			<-- X is now that same empty block
== []			But I don't see the "hi" value.  -- What am I missing?
OOOOOOHHHHHH
BrianH
8-Mar-2010
[2939]
You are missing reuse. It doesn't matter from the console, it matters 
in reused code, i.e. in functions.
PatrickP61
8-Mar-2010
[2940]
I have a typo in my example.  Now I see
BrianH
8-Mar-2010
[2941x4]
x is not the same empty block, it is a new block.
>> a: does [append copy [] 1]
>> b: does [append [] 1]
>> a
== [1]
>> a
== [1]
>> b
== [1]
>> b
== [1 1]
>> source a
a: make function! [[][append copy [] 1]]
>> source b
b: make function! [[][append [1 1] 1]]
This is in R3, but the rules are the same in R2.
PatrickP61
8-Mar-2010
[2945x6]
These examples are good to see
a: does>> a: does [append copy [ ] 1]
>> b: does [append [ ] 1]

>> c: does [append x: [ ] 1]         ;<--   is this the same as b?
Is C the same as B except that you can reference that memory pointer 
by using X -- is that right?
Ok so back to my question.

A will reference a specific memory and each time it is eval, a new 
empty block is setup.

B will reference a different memory place, and each time it is eval, 
that same memory can be modified.

C will reference a different memory place, that can also be modified 
by either using C or changing X.


But is there any significant difference to the following, if both 
reference a NEW memory location that is empty?
x:	copy [ ]
y:	[ ]

Sorry, I am really trying to understand
I think I understand now Brian,

In terms of just initializing a value, both of these are only done 
once, then they are essentially the same,

But if at any time you eval X or Y a second time, then you get different 
results!!!

Thank you for explaining it to me!
(after you have modified some values into x and Y, I mean)  -- hope 
I didn't confuse others out there.
Steeve
9-Mar-2010
[2951]
with pattern matching

>> attempt [to-date replace/all form reverse parse copy/part find/tail 
"*[YY-MM-DD=03-06-30]*" "YY-MM-DD="  8 "-" " " "-"]
== 30-Jun-2003
PatrickP61
10-Mar-2010
[2952]
I have a question about the APPEND function.

>> loop 10 [x: "a" append x "b"]
== "abbbbbbbbbb"


I would have expected the the final result to be just "ab" (after 
the 10th iteration).


But in this example, X has been assigned to the string "a" and then 
"b" is appended to it 10 times.

If X has been "reset" to the letter "a" again in each interation, 
why doesn't the previous "b" also go away since X has been reinitialized 
to just the letter "a"?
Henrik
10-Mar-2010
[2953x2]
you fell into the copy trap :-) x is not reset by assigning "a" to 
it.
x: copy "a" would solve that. that's why it's called the copy trap.
PatrickP61
10-Mar-2010
[2955]
You mean like this:

>> loop 10 [x: copy "a" append x "b"]
== "ab"
Henrik
10-Mar-2010
[2956x2]
with this, you can provide some interesting tricks to building series 
without assigning them to words:

loop 10 [append "a" "b"]
== "abbbbbbbbbb"
beware of this when making objects with series or just series that 
are copied. if you find that your series are changing in funny ways, 
it may be that you forgot to copy them.
Sunanda
10-Mar-2010
[2958]
Effectively, you are making x the _same_ as the string "a" so when 
one changes, they "both" do. as Henrik says, you want to initialise 
x to the _value_ of the string "a" instead.

It may be clearer like this
     A: copy "a"
    loop 10 [

        x: A    ;; x is the _same_ as A ... you want [x: copy A] to get its 
        value instead 
        append x "b"
    ]
Henrik
10-Mar-2010
[2959x2]
as a rule of thumb, REBOL tries to reuse series as much as it can.
use SAME? to detect whether two series are the same one:

>> a: ""
>> b: a
>> same? a b
== true

>> b: copy a
>> same? a b
== false
PatrickP61
10-Mar-2010
[2961]
>> loop 10 [x: "a" append x "b"]

And yet, if I repeat the exact same comand 10 times, I do NOT get 
the same result

x: "a" append x "b"
=="ab"
x: "a" append x "b"
=="ab"
Henrik
10-Mar-2010
[2962x2]
no, because every time you make a new line, you are making a new 
string. in the loop, you are reusing the same string.
If you type:

>> ""
== ""


you are making a string, but you are also immediately losing it again. 
you can't use it again.

By doing this:

>> ""
== ""
>> ""
== ""

You are therefore creating two separate strings.
PatrickP61
10-Mar-2010
[2964]
Ok, so the same as the loop would be:

x: "a" append x "b"
x: "ab" append x "b"
x: "abb" append x "b"  etc    right?
Henrik
10-Mar-2010
[2965x2]
This means also that doing this:

>> x: "a"
== "a"
>> x: "a"
== "a"


you are creating two separate strings, both assigned to 'x and the 
last assignment overwrites the first one.
nope. :-)

The same would be:

x: "a"
append x "b"
append x "b"
append x "b"...
PatrickP61
10-Mar-2010
[2967]
ahhh
Henrik
10-Mar-2010
[2968]
REBOL is very aggressively reusing strings, even inside code structures.
Ladislav
10-Mar-2010
[2969x2]
or: 

    b: [x: "a" append x "b"]
    do b
    do b
    do b
you can even examine what is going on this way
PatrickP61
10-Mar-2010
[2971]
Ok, I think I got that now -- thank you all
Davide
12-Mar-2010
[2972]
I need a small function "my-compose" that takes a blocks, deep search 
tag! values and change it with the value of the word into the tag.
For example, if I have a test function like this:

x: 1
test: func [y /local z] [
	z: 3
	my-compose [ 
		print [ <x> + (<y> + <z>)]
	]
]

Calling:

test 2 

should return:

>> [print 1 + (2 + 3)]

My problem is to do the right bind in the parse:

my-compose: function [code [block!]] [elem rule pos] [
	rule: [any [

  pos: set elem tag! (change/only pos **magical-bind-here** to word! 
  to string! elem ) |		    	
		pos: any-block! :pos into rule |
		skip
	]]
	parse code rule
]
Ladislav
12-Mar-2010
[2973x2]
for inspiration, check http://www.fm.tul.cz/~ladislav/rebol/build.r
(you can even use it directly, if you don't insist to use just tags)
Davide
12-Mar-2010
[2975x2]
but in "build" you have to use the /with refinement ,  passing the 
block with the couples word-value isn't it ?
Now I'm using this function: 


my-compose: function [code [block!] params [block!]] [elem rule pos 
temp] [
	rule: [any [

  pos: set elem tag! (temp: select params to word! to string! elem 
  if not none? temp [change/only pos temp]) |		    	
		pos: any-block! :pos into rule |
		skip
	]]
	parse code rule    
]

And I call it using:

my-compose [print <x> + (<y> + <z>)] reduce ['x x 'y y 'z z]


It works but the second block is redundant and I would eliminate 
it.
Steeve
12-Mar-2010
[2977x2]
you need to pass the values to map because the formula block only 
contains tag! which basically are strings (tags have no context, 
nor values).
if instead you use get-words as tags, you don't need to.

my-compose: func [code [block!] /local pos][
	parse code rule: [
		any [
			  to get-word! pos: (pos/1: get pos/1) skip 		    	
			| to any-block! into rule
		]
	]
	code
]

>>x: 1
>>y: 2
>>z: 3
>>my-compose [print :x + (:y + :z)]
==[print 1 + (2 + 3)]
Correction:

my-compose: func [code [block!] /local pos][
	parse code rule: [
		any [
			  pos: get-word! (pos/1: get pos/1)
			| into rule
			| skip
		]
	]
	code
]
Davide
12-Mar-2010
[2979]
Steeve thanks, now it is much more clear.

I'll use the get-word type as you suggest. (I have to change a bit 
my dialect, but it's not a problem)
Sunanda
13-Mar-2010
[2980]
REBOL has no reserved words
 --I've seen suggestions like this several times.

There are exceptions....Some of the commonly used dialects have many 
reserved words. [Parse, View etc]. That is clearly stated in some 
of the documentation, eg:
    http://www.rebol.com/docs/view-guide.html#section-7


In addition, 'local acts as a reserved word as this code (R2 or R3) 
shows:
    >> use [local x][x: has [local][]]
    ** Script Error: Duplicate function value: local


So there is at least one real reserved word. Anyone know of any others?
Steeve
13-Mar-2010
[2981x2]
it fail not because it's reserverd, but because you declare local 
twice.
what you do is:
>>func [/local local][]
i give you a true one in R3.
>> context [self: 1]
** Script error: cannot set self - it is protected