[REBOL] Re: Accessing nested Blocks + search in blocks
From: lmecir:geocities at: 5-Nov-2000 12:50
Hi Brett,
Do-path really sounds good, so I decided to accept it, thanks. To your
second question. The Rebol paths are not referentially-transparent in Rebol,
e.g.
a/(1 + 1)
isn't a path in Rebol,
now/time/second
doesn't work as some of us would like it to, etc. It means, that Do-path is
referentially transparent as opposed to a direct Rebol path use.
do-path: function [
{A referentially transparent way to invoke paths}
[catch]
block [block!]
] [head path stop] [
path: either stop: find block first [/] [
copy/part block stop
] [
stop: [none]
block
]
if not empty? path [
path: reduce path
if not word? set/any 'head first path [change/only path 'head]
if error? path: try [to-path! path] [
throw make error! {Invalid path representation}
]
]
do compose [(:path) (next stop)]
]
to-path!: function [
[catch]
path-block [block!]
] [path] [
path: make path! length? path-block
foreach element path-block [
throw-on-error [
if :element [insert/only tail :path :element]
]
]
:path
]
{
Example:
do-path [now 'time 'second]
a: [1 2 3]
do-path [a 1 + 1]
only: false
head do-path ['insert if only ['only] / tail a [4]]
only: true
head do-path ['insert if only ['only] / tail a [4]]
do-path [make object! [a: 14] 'a]
}