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

frustrating pairs...

 [1/5] from: alain:goye:free at: 7-May-2004 19:13


; 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 tmp][ 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.

 [2/5] from: maximo:meteorstudios at: 7-May-2004 15:19


> -----Original Message----- > From: Alain Goyé [mailto:[alain--goye--free--fr]]
<<quoted lines omitted: 3>>
> ; pairs are not numbers, not series, not objects... too bad ! > ; If we set:
(that's why they are called pairs ;-) remember that you can use first second /1 and /2 a: 1x5 a/1 a/x first a are all equivalent... I know this does not answer your question... but since we are talking about pairs... its a good time for the reminder... -MAx

 [3/5] 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 won't work...
>> a: 9
== 9
>> square-root a
== 3
>> a
== 9 so we use this approach...
>> a: 9
== 9
>> a: square-root a
== 3
>> a
== 3 So a zero-x function would be used like this...
>> zero-x: func [arg][arg/x: 0 arg] >> xy: 1x2
== 1x2
>> xy: zero-x xy
== 0x2
>> xy
== 0x2 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:
<<quoted lines omitted: 58>>
> ] > ; Alain.
-- Carl Read

 [4/5] from: Christian:Ensel:GMX at: 8-May-2004 3:12


Dear Alain,
> either d = 'x [bx/pane/offset/x: - tmp * sf/data ] [ ; how to improve that
?
> bx/pane/offset/y: - tmp * sf/data ]
The function MIX-PAIRS as defined below takes two PAIR!s and a word of value X or Y. Depending on the latter MIX-PAIRS returns a new PAIR! which is composed by A's axis as supplied in the 'D argument and B's opposite axis. This lets you 'simulate' touching only one axis easily. Sounds very confusing, I know, but look at the following code snippet, you will get the idea: ;------------ cut'n'paste -- (beware of line-breaks) ------------------------- REBOL [] mix-pairs: func [a [pair!] b [pair!] 'd [word!] /local way] [ (a * way: pick [1x0 0x1] :d = 'x) + (b * reverse way) ] scroll: func [bx sf 'd] [ bx/pane/offset: mix-pairs bx/size - bx/pane/size * sf/data bx/pane/offset :d ;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ;-- bx/pane/offset is modified only for the direction supplied in :d ; by setting the opposite axis back to the value before the modification ; show bx ] d: 'x view layout [ across bx: box 200x100 white with [ pane: make-face/spec 'box [size: 40x40 color: red] ] sf: slider 20x100 [scroll bx sf :d] return tg: btn "toggle" [d: select [x y x] :d sf/data: bx/pane/offset/:d / (bx/size/:d - bx/pane/size/:d) show [tg sf] ] ] ;------------ cut'n'paste -- (beware of line-breaks) ------------------------- regards, Christian

 [5/5] from: alain:goye:free at: 8-May-2004 12:25


Thank you Maxim, Carl and Christian for all your answers. You're right Carl that pairs are better seen as numbers. And they are still are convenient even though they don't have all other types' features. I appreciate Christian's code with its nice use of reverse and select (d: select [x y x] :d). Regards, Alain.

Notes
  • Quoted lines have been omitted from some messages.
    View the message alone to see the lines that have been omitted