[REBOL] Re: frustrating pairs...
From: carl:cybercraft at: 8-May-2004 1:16
I think it's best to just treat them as you would a number. For
instance, when wanting to convert a number to its square-root, this
>> a: 9
>> square-root a
so we use this approach...
>> a: 9
>> a: square-root a
So a zero-x function would be used like this...
>> zero-x: func [arg][arg/x: 0 arg]
>> xy: 1x2
>> xy: zero-x xy
Not sure if that helps with your scroll function though. My head's a
bit wolly at the moment... :-)
On 08-May-04, Alain Goyé wrote:
> ; pairs are not numbers, not series, not objects... too bad !
> ; If we set:
> toto: 5x5
> ; all this (and more...) is wrong!:
> set toto/x 3 ; toto/x is not a word
> set 'toto/x 3 ; 'toto/x is not a word
> in toto 'x ; toto is not an object
> change toto 3 ; toto is not a series...
> ; So what ?
> ; Well, I am frustrated while trying to touch only x or y of a pair
> in a function. This makes no problem:
> zero-pair: func ['arg] [set arg 0x0]
> zero-pair toto
> toto ; OK
> ; But now if I want a function to set the pair's x value to 0, all
> this is wrong again:
> zero-x: func [arg] [arg/x: 0] ; argument goes by reference zero-x:
> func ['arg] [arg/x: 0] ; Cannot use path on word! value zero-x: func
> ['arg] [set arg/x 0] ; toto/x is not a word ; etc.
> ; Of course this for example works:
> zero-x: func ['arg] [
> p: get arg
> set arg as-pair 0 p/y
> ; or in that special case:
> zero-x: func ['arg] [set arg 0x1 * get arg]
> ; Is there a simpler way ?
> ; And what if I would like to pass an argument to specify whether to
> affect x or y in the function ? ; this is short nice:
> test-print: func [p [pair!] 'd [word!]] [print p/:d]
> test-print toto x
> ; but this isn't that an ugly "usine à gaz" ("gas factory" in
> French) ? :
> test-change: func [
> 'arg [word!] "the pair to affect"
> 'd [word!] "the direction to change: x or y"
> val [integer!] "the value to set"
> ] [
> p: get arg
> either d = 'x [
> set arg as-pair val p/y
> ] [
> set arg as-pair p/x val
> test-change toto x 10
> ; ...while one would like so much just to write something like :
> arg/:d: val ; wrong of course...
> ; did I miss something please ? ; of course the above examples are
> useless, but here is the real one: ; a function to scroll a box's
> pane using a horizontal OR vertical slider:
> scroll: func [bx "box" sf "slider" 'd "direction: x or y" /local
> if none? bx/pane [exit]
> if 0 < tmp: bx/pane/size/:d - bx/size/:d [
> either d = 'x [ bx/pane/offset/x: - tmp * sf/data ] [ ; how
> to improve that ?
> bx/pane/offset/y: - tmp * sf/data ]
> show bx
> ; Alain.