Setting variable in complex paths
[1/3] from: mike::yaunish::home::com at: 27-Apr-2001 12:53
I have been working with complex paths in REBOL because I just love how
easy they are
to work with - however I have had some problems setting variables within a
complex
path. I think I have a fairly good solution, but know if I expose it to the
light of other REBOLers
it can be even better. I have called this function "complex-set".
complex-set: func ["Sets a complex path (from a block of literal words) to
a specified value"
complex-path [block!] "Block of literal and normal
words that describe the complex path"
value [any-type!] "Value to be set"
][
either lit-word? last complex-path [
prefix-path: copy/part complex-path ((length? complex-path) - 1 )
the-path: to-path reduce prefix-path
the-field: to-lit-word last complex-path
either ( found? find the-path the-field ) [
do bind reduce [to-set-path reduce complex-path value] 'do
][ ; field doesn't already exist so create it
append/only the-path the-field
append/only the-path value
]
][
do bind reduce [to-set-path reduce complex-path value] 'do
]
]
address-book: [ [name [first "Joe" last "Blow" ] address [city "Goosebay"
province "Labrador"]]
[name [first "Tim" last "Taylor" middle "Lee"] address [city
Calgary
province "Alberta"]]
]
>>index: 2
>>new-name: "Tiny"
>>complex-set [ 'address-book index 'name 'middle ] new-name
complex-set
also accomodates the creation of fields within a path.
For example;
>>complex-set [ 'address-book 1 'name 'middle ] "Lincoln"
>>complex-set [ 'address-book 2 'address 'zip ] 90210
>> print mold address-book
[[name [first "Joe" last "Blow" middle "Lincoln"] address [city "Goosebay"
province "Labrador"]]
[name [first "Tiny" last "Taylor" middle "Tiny"] address [city
Calgary
province "Alberta" zip 90210]]
]
There are some minor limitations - but it works great for REBOL named fields.
Any comments would be appreciated.
Mike Yaunish
[mike--yaunish--home--com]
[2/3] from: petr:krenzelok:trz:cz at: 27-Apr-2001 21:46
Hi,
I remember the very ooold days of Rebol, it was some 1997 and Rebol existed
just in some form of Alpha prerelease featuring very little of its current
set of datatypes :-) I implemented function called 'extract, doing something
similar ....
... it is interesting to see someone else sees need for some kind of
functions, allowing us to work with nested structures. Everything Rebol
interpreted is just ... well ... interpreted ...
I suggested some kind of find/deep functionality, returning path to the
element. I mentioned it to Carl on Express Conference, and he expressed the
opinion Rebol's 'find function is overly compex. So I suggested calling it
'locate, or something like that ...
->> locate address-book 'last
->> 1/2/2 ; or [1 2 2]
I am not sure if it could be appliable upon some more complex cases, e.g.
distinguishing literals lookup, words look-up, string and other datatypes
lookup, what about several occurances of the same value we are looking for?
just theoretically, it could:
1) treat even complex block nested structured as "flat" ones, 1 dimmensional
ones ...
2) let's say we want to perform some kind of lookup that way thru
main-face/pane containing other faces (and their panes etc, simply nested
displays .... there is danger of cross-reference though ...
... as for cross-references, rebol halts probing VID stuff too much. ...
just as an example try:
view layout [btn: button "OK" [unview]]
probe btn
... and you will see not only btn face got probed ...
Sorry for being off-topic, but ... it just came to my mind :-)
-pekr-
[3/3] from: ryanc:iesco-dms at: 27-Apr-2001 14:52
Interisting piece of code, but win a spot in my tool chest. I like where your
going with it though. This sort functionality could be achieved in a more
standard way with the Pekr's 'locate vision. Example:
Rank: [ A-group [ Bobby 3 Mark 4 Jim 2 ] ] B-group [ Sam 1 Arnold 5 ] ]
change locate Rank 'Bobby 'Bob ; Change Bobby's name to Bob
; Exchange Sam's and Jim's ranks
change next locate Rank 'Sam 1
change next locate Rank 'Jim 2
Unfortuneately no one has written 'locate yet. Hmmm... I got some time this
weekend...
--Ryan
Mike Yaunish wrote:
> I have been working with complex paths in REBOL because I just love how
> easy they are
<<quoted lines omitted: 49>>
> [rebol-request--rebol--com] with "unsubscribe" in the
> subject, without the quotes.
--
Ryan Cole
Programmer Analyst
www.iesco-dms.com
707-468-5400
I am enough of an artist to draw freely upon my imagination.
Imagination is more important than knowledge. Knowledge is
limited. Imagination encircles the world.
-Einstein
Notes
- Quoted lines have been omitted from some messages.
View the message alone to see the lines that have been omitted