Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

what to do with bind ?

 [1/8] from: pat665:ifrance at: 24-Nov-2001 17:29


Hi, I am wondering what is the purpose of bind. I have read the doc and also interesting code where bind is used (for example Ladislav s-c? function). My question is more why is there a bind function than what does bind do. Or put in another way what would be impossible (or very difficult) to do without bind ? Patrick

 [2/8] from: al:bri:xtra at: 25-Nov-2001 8:20


Patrick wrote:
> Or put in another way what would be impossible (or very difficult) to do
without bind ? If I didn't have 'bind, it would be impossible for me to write directly interpreted Rebol dialects. For example, check out my HTML dialect at: http://valley.150m.com/Rebol/Values/ML.r and: http://valley.150m.com/Rebol/Values/HTML.r 'bind is really great for getting Rebol script that's written outside a object or context, and getting that script so it runs inside that object/context. Andrew Martin ICQ: 26227169 http://valley.150m.com/

 [3/8] from: lmecir:mbox:vol:cz at: 24-Nov-2001 22:54


Hi Patrick, 1) e.g. I used Bind in http://www.sweb.cz/LMecir/highfun.r (twice), although it could have been done without Bind too. 2) if you can do something with Bind and you know the context of the Known-word argument, then there usually is a (slower and more complicated) way how to write things without Bind (this is the case of most dialects, e.g. Andrew could have written his dialect even without knowing the Bind function). If you explore unknown contexts or inaccessible contexts such as e.g. function contexts, you may not be able to write your code without Bind. For example the In-object? function or the S-c? function couldn't have been written without Bind (IMO). Cheers Ladislav Hi, I am wondering what is the purpose of bind. I have read the doc and also interesting code where bind is used (for example Ladislav s-c? function). My question is more why is there a bind function than what does bind do. Or put in another way what would be impossible (or very difficult) to do without bind ? Patrick

 [4/8] from: rebol665:ifrance at: 25-Nov-2001 18:06


Hi, ladislav The first time I found 'bind was in some of your code. But I wonder if 'bind is only useful for those who explore the guts of Rebol. I'm looking for an example that enlighten the use of bind in a simple way. I thank Andrew Martin for his help, but ML.r is far too complex for me. Comme on dit chez nous "ce qui se conçoit bien s'énonce clairement". Searching for the light, I found Ingo Hohmann's TUI Dialect article on Rebol-Force site. I thought it was it, because he was using 'bind in a way that was logical to me (to avoid an error). I have experimented his code and to my surprise and disbelief I have found that the program works fine with or without 'bind . So I'm more puzzled than ever. This is the code I extract from Ingo article. It should give an error because screen-size is not in the same context but it did not. What is wrong with it ? Rebol [ title: "test" purpose: "what to do with bind?"] cursor2: func [ {Cursor positioning dialect (iho)} commands [block!] /local rules string arg ][ ; Don't forget to add 'screen-size to our functions locals screen-size: ( c: open/binary/no-wait [scheme: 'console] prin "^(1B)[7n" arg: next next to-string copy c close c arg: parse/all arg ";R" forall arg [change arg to-integer first arg] arg: to-pair head arg ) string: copy "" commands: compose commands ; NEW LINE HERE rules: [ any [ 'clear (append string "^(1B)[J") | 'at set arg pair! (append string rejoin ["^(1B)[" arg/x ";" arg/y "H"]) | set cnt integer! set arg string! ( append string head insert/dup copy "" arg cnt ) | set arg string! (append string arg) ] ] parse commands rules string ] print cursor2 [clear at 1x10 (screen-size/y / 2) "x"] print cursor2 [at 2x10 "no bind but no error too"] halt

 [5/8] from: lmecir:mbox:vol:cz at: 25-Nov-2001 19:23


Hi Patrick, I will try to explain it ... 1) Let's have a look at the: { ; Don't forget to add 'screen-size to our functions locals} sentence. That means, that Ingo wanted to have the word 'screen-size local in the function. Why? - He knew, that the word was defined only for the function to work as he wanted it to. - Forgetting to define a local word as local may become a source of some hard to find bugs (interactions of some functions that are supposed to be independent). 2) That is why Ingo defined the word as local: cursor2: func [ {Cursor positioning dialect (iho)} [catch] commands [block!] /local rules string arg screen-size cnt ][ screen-size: ( c: open/binary/no-wait [scheme: 'console] prin "^(1B)[7n" arg: next next to-string copy c close c arg: parse/all arg ";R" forall arg [change arg to-integer first arg] arg: to-pair head arg ) string: copy "" commands: compose commands ; NEW LINE HERE rules: [ any [ 'clear (append string "^(1B)[J") | 'at set arg pair! (append string rejoin ["^(1B)[" arg/x ";" arg/y "H"]) | set cnt integer! set arg string! ( append string head insert/dup copy "" arg cnt ) | set arg string! (append string arg) ] ] parse commands rules string ] (see the screen-size word above). 3) After the above definition, the situation changes:
>> print cursor2 [clear at 1x10 (screen-size/y / 2) "x"]
** Script Error: screen-size has no value ** Where: cursor2 ** Near: screen-size/y / 2 <<Patrick>> Hi, ladislav The first time I found 'bind was in some of your code. But I wonder if 'bind is only useful for those who explore the guts of Rebol. I'm looking for an example that enlighten the use of bind in a simple way. I thank Andrew Martin for his help, but ML.r is far too complex for me. Comme on dit chez nous "ce qui se conçoit bien s'énonce clairement". Searching for the light, I found Ingo Hohmann's TUI Dialect article on Rebol-Force site. I thought it was it, because he was using 'bind in a way that was logical to me (to avoid an error). I have experimented his code and to my surprise and disbelief I have found that the program works fine with or without 'bind . So I'm more puzzled than ever. This is the code I extract from Ingo article. It should give an error because screen-size is not in the same context but it did not. What is wrong with it ? Rebol [ title: "test" purpose: "what to do with bind?"] cursor2: func [ {Cursor positioning dialect (iho)} commands [block!] /local rules string arg ][ ; Don't forget to add 'screen-size to our functions locals screen-size: ( c: open/binary/no-wait [scheme: 'console] prin "^(1B)[7n" arg: next next to-string copy c close c arg: parse/all arg ";R" forall arg [change arg to-integer first arg] arg: to-pair head arg ) string: copy "" commands: compose commands ; NEW LINE HERE rules: [ any [ 'clear (append string "^(1B)[J") | 'at set arg pair! (append string rejoin ["^(1B)[" arg/x ";" arg/y "H"]) | set cnt integer! set arg string! ( append string head insert/dup copy "" arg cnt ) | set arg string! (append string arg) ] ] parse commands rules string ] print cursor2 [clear at 1x10 (screen-size/y / 2) "x"] print cursor2 [at 2x10 "no bind but no error too"] halt

 [6/8] from: rebol665:ifrance at: 25-Nov-2001 19:47


Hi Ladislav You are right. I did not understand the meaning of { ; Don't forget to add 'screen-size to our functions locals }. It's cristal clear now thanks to you. I will have a better night ! Patrick ----- Original Message ----- From: "Ladislav Mecir" <[lmecir--mbox--vol--cz]> To: <[rebol-list--rebol--com]> Sent: Sunday, November 25, 2001 7:23 PM Subject: [REBOL] Re: what to do with bind ? Hi Patrick, I will try to explain it ... 1) Let's have a look at the: { ; Don't forget to add 'screen-size to our functions locals} sentence. That means, that Ingo wanted to have the word 'screen-size local in the function. Why? - He knew, that the word was defined only for the function to work as he wanted it to. - Forgetting to define a local word as local may become a source of some hard to find bugs (interactions of some functions that are supposed to be independent). 2) That is why Ingo defined the word as local: cursor2: func [ {Cursor positioning dialect (iho)} [catch] commands [block!] /local rules string arg screen-size cnt ][ screen-size: ( c: open/binary/no-wait [scheme: 'console] prin "^(1B)[7n" arg: next next to-string copy c close c arg: parse/all arg ";R" forall arg [change arg to-integer first arg] arg: to-pair head arg ) string: copy "" commands: compose commands ; NEW LINE HERE rules: [ any [ 'clear (append string "^(1B)[J") | 'at set arg pair! (append string rejoin ["^(1B)[" arg/x ";" arg/y "H"]) | set cnt integer! set arg string! ( append string head insert/dup copy "" arg cnt ) | set arg string! (append string arg) ] ] parse commands rules string ] (see the screen-size word above). 3) After the above definition, the situation changes:
>> print cursor2 [clear at 1x10 (screen-size/y / 2) "x"]
** Script Error: screen-size has no value ** Where: cursor2 ** Near: screen-size/y / 2 <<Patrick>> Hi, ladislav The first time I found 'bind was in some of your code. But I wonder if 'bind is only useful for those who explore the guts of Rebol. I'm looking for an example that enlighten the use of bind in a simple way. I thank Andrew Martin for his help, but ML.r is far too complex for me. Comme on dit chez nous "ce qui se conçoit bien s'énonce clairement". Searching for the light, I found Ingo Hohmann's TUI Dialect article on Rebol-Force site. I thought it was it, because he was using 'bind in a way that was logical to me (to avoid an error). I have experimented his code and to my surprise and disbelief I have found that the program works fine with or without 'bind . So I'm more puzzled than ever. This is the code I extract from Ingo article. It should give an error because screen-size is not in the same context but it did not. What is wrong with it ? Rebol [ title: "test" purpose: "what to do with bind?"] cursor2: func [ {Cursor positioning dialect (iho)} commands [block!] /local rules string arg ][ ; Don't forget to add 'screen-size to our functions locals screen-size: ( c: open/binary/no-wait [scheme: 'console] prin "^(1B)[7n" arg: next next to-string copy c close c arg: parse/all arg ";R" forall arg [change arg to-integer first arg] arg: to-pair head arg ) string: copy "" commands: compose commands ; NEW LINE HERE rules: [ any [ 'clear (append string "^(1B)[J") | 'at set arg pair! (append string rejoin ["^(1B)[" arg/x ";" arg/y "H"]) | set cnt integer! set arg string! ( append string head insert/dup copy "" arg cnt ) | set arg string! (append string arg) ] ] parse commands rules string ] print cursor2 [clear at 1x10 (screen-size/y / 2) "x"] print cursor2 [at 2x10 "no bind but no error too"] halt

 [7/8] from: greggirwin:mindspring at: 25-Nov-2001 15:04


Hi Patrick, << But I wonder if 'bind is only useful for those who explore the guts of Rebol. I'm looking for an example that enlighten the use of bind in a simple way. >> I hope I won't confuse matters, being a relative beginner myself, but here's how I recently used bind. I created a basic AWK dialect and implemented it as an object. AWK provides certain built-in variables that allow you to reference the current line, fields in that line, the number of records read, etc. and they are designed to have a very concise syntax (e.g. $1 = field 1 in the current line, $0 is the current line in its entirety). Since the goal is to write AWK "programs" outside the context of my object, I needed a way to reference the built-in variables and I wanted to avoid the requirement of fully qualifying every reference with the context name. That's what 'bind allowed me to do. Here's the basic idea: rawk: context [ _: none ; current parsed line/record exec: func [ {Execute the RAWK 'program' contained in the prog block for each file contained in the files block.} program [block!] files [block!] ][ bind program 'self ; EXECUTE PROGRAM HERE ] ] ; If bind isn't used, we have to do this: rawk-prog: [ [rawk/_/1 = "lng"] [print ["found line with _/1 = lng:" rawk/_]] ] ; If bind is used, we can do this: rawk-prog: [ [_/1 = "lng"] [print ["found line with _/1 = lng:" _]] ] HTH! --Gregg

 [8/8] from: rebol665:ifrance at: 26-Nov-2001 13:20


Hi, Gregg That helps. I received a lot of useful explanation, and I am going to be soon a 'bind expert ;-) Patrick