Accessing nested Blocks + search in blocks
[1/10] from: jthomas:apollo:evo:it at: 4-Nov-2000 19:19
Hello Andrew
On 03-Nov-00, Andrew Martin wrote:
>AM Joachim Thomas wrote:
>AM> having this
<<quoted lines omitted: 24>>
>AM == [[one [1 2 "I'm a new value!"]] [two [4 5 6]] [three [7 8 9]]]
>AM
yes, I know this... :-)
my question was if I can assign myblock/1/2/3 to a variable in some way
and use this variable to access and/or modify the element in myblock...
By the way, is there a method to search (deep/recutsively) into block, not only at the
first level? And accessing into blocks, like myblock/1/2/3 using
indexes/variables and not direct references?
Thanx..
Regards
--
Joachim Thomas - [jthomas--mail--evo--it] - ICQ 18088459
Amiga1260+Amiga4060PPC604
Member of ATO (italian Division) http://bilbo.di.unipi.it/~ato-it & Phoenix Consortium
Concerti - Gruppi Musicali & Locali http://concertitalia.supereva.it
Virtual PHOTO Gallery http://www.terzomercato.com/free/jt_home
Your mouse has moved.
Windows NT must be restarted for the change to take effect.
Reboot now?
-- Chris Holt
[2/10] from: joel:neely:fedex at: 4-Nov-2000 20:36
Hello, Joachim,
[rebol-bounce--rebol--com] wrote:
> On 03-Nov-00, Andrew Martin wrote:
> >AM Joachim Thomas wrote:
<<quoted lines omitted: 9>>
> >AM> global to myblock, so is there a way to assign the
> >AM> content of myblock, like a pointer, to the variable...
I'll be delighted to have someone prove me wrong, but AFAIK
there is no way to create "shortcuts" to arbitrary "places"
in a REBOL data structure. You *can* do this for reference
types (e.g., blocks) so that a lame answer to your original
question would be
shortcut: myblock/1/2
print shortcut/3
shortcut/3: 33
I don't know what your perceived need to do this is, but if
you're trying to tweak for speed, the use of a reference to
the lowest-level block in the data structure will eliminate
some of the traversal.
> By the way, is there a method to search (deep/recursively)
> into block, not only at the first level?
One way to write such a thing...
deepfind: func [b [block!] v [any-type!] /local c d] [
either found? d: find b v [
d
][
c: b
forall c [
if all [block? d: first c d: deepfind d v] [
return d
]
]
none
]
]
which, given your
myblock: [[one [1 2 3]][two [4 5 6]][three [7 8 9]]]
does the following
deepfind myblock 5 == [5 6]
deepfind myblock 'three == [three [7 8 9]]
deepfind myblock 1 == [1 2 3]
> And accessing into blocks, like myblock/1/2/3 using
> indexes/variables and not direct references?
>> i: 1 == 1
>> j: 2 == 2
>> k: 3 == 3
>> myblock/:i/:j/:k == 3
Hope this helps!
-jn-
[3/10] from: lmecir:geocities at: 5-Nov-2000 7:06
Hi,
I hope, that somebody can find some use for the following :
Rebol []
rtp: 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:
rtp [now 'time 'second]
a: [1 2 3]
rtp [a 1 + 1]
only: false
head rtp ['insert if only ['only] / tail a [4]]
only: true
head rtp ['insert if only ['only] / tail a [4]]
}
[4/10] from: brett:codeconscious at: 5-Nov-2000 20:25
Hi Ladislav,
This looks really useful.
A more general use of the behaviour than that of my posts.
But two things.
The name. Why not something more accessible like invoke-path, do-path or
whatever?
Can you explain why the emphasis on referential transparency? Or put another
way, what is a non-referentially transparent way to invoke paths and how
does that differ from your functions?
Brett.
[5/10] from: g:santilli:tiscalinet:it at: 4-Nov-2000 20:55
Hello Joachim!
On 04-Nov-00, you wrote:
JT> By the way, is there a method to search (deep/recutsively)
JT> into block, not only at the first level? And accessing into
It's probably slow, but:
find-deep: func [block [any-block!] value /local res] [
forall block [
any [
if any-block? block/1 [
if res: find-deep block/1 :value [break/return res]
]
if block/1 = :value [break/return :block]
]
]
]
>> find-deep myblock 'one
== [one [1 2 3]]
>> find-deep myblock 3
== [3]
>> find-deep myblock 7
== [7 8 9]
Depending on your application, this might be speeded up using the
native FIND.
JT> blocks, like myblock/1/2/3 using indexes/variables and not
JT> direct references?
Did you mean something like this?
>> i: 1 j: 2 k: 3
== 3
>> myblock/:i/:j/:k
== 3
>> pick pick pick myblock i j k
== 3
Regards,
Gabriele.
--
Gabriele Santilli <[giesse--writeme--com]> - Amigan - REBOL programmer
Amiga Group Italia sez. L'Aquila -- http://www.amyresource.it/AGI/
[6/10] 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]
}
[7/10] from: jthomas::apollo::evo::it at: 5-Nov-2000 14:01
Hello Joel
On 05-Nov-00, Joel Neely wrote:
>JN Hello, Joachim,
>JN
>
<snip>
thanks for all your clarifications...
>JN
>JN> And accessing into blocks, like myblock/1/2/3 using
<<quoted lines omitted: 7>>
>JN Hope this helps!
>JN
this was it... I wonder why I didn't think at doing so... :-/
>JN -jn-
Regards
--
Joachim Thomas - [jthomas--mail--evo--it] - ICQ 18088459
Amiga1260+Amiga4060PPC604
Member of ATO (italian Division) http://bilbo.di.unipi.it/~ato-it & Phoenix Consortium
Concerti - Gruppi Musicali & Locali http://concertitalia.supereva.it
Virtual PHOTO Gallery http://www.terzomercato.com/free/jt_home
I haven't lost my mind; it's backed up on a disk in here somewhere...
-- John A. Whiting.
[8/10] from: jthomas:apollo:evo:it at: 5-Nov-2000 14:04
Hello Ladislav
On 05-Nov-00, Ladislav Mecir wrote:
>LM Hi,
>LM
<<quoted lines omitted: 4>>
>LM rtp: function [
>LM {A referentially transparent way to invoke paths}
interesting....
I'll do some tests with it....
Thanks!
Regards
--
Joachim Thomas - [jthomas--mail--evo--it] - ICQ 18088459
Amiga1260+Amiga4060PPC604
Member of ATO (italian Division) http://bilbo.di.unipi.it/~ato-it & Phoenix Consortium
Concerti - Gruppi Musicali & Locali http://concertitalia.supereva.it
Virtual PHOTO Gallery http://www.terzomercato.com/free/jt_home
WinErr 024: Uptime exceeded 2 hours. Microsoft do not allow Windows to run
longer than this. This is why your computer will now crash.
-- Lorens Johansson
[9/10] from: jthomas:apollo:evo:it at: 5-Nov-2000 13:56
Hello Gabriele
On 04-Nov-00, Gabriele Santilli wrote:
>GS Hello Joachim!
>GS
<<quoted lines omitted: 12>>
>GS == 3
>GS
exactly.... I tried it with some paren but I didn't think at this
solution...
>GS Regards,
>GS Gabriele.
Regards
--
Joachim Thomas - [jthomas--mail--evo--it] - ICQ 18088459
Amiga1260+Amiga4060PPC604
Member of ATO (italian Division) http://bilbo.di.unipi.it/~ato-it & Phoenix Consortium
Concerti - Gruppi Musicali & Locali http://concertitalia.supereva.it
Virtual PHOTO Gallery http://www.terzomercato.com/free/jt_home
Software suppliers are trying to make their software packages more
'user-friendly'.... Their best approach, so far, has been to take all
the old brochures, and stamp the words, 'user-friendly' on the cover.
-- Bill Gates
[10/10] from: jthomas:apollo:evo:it at: 5-Nov-2000 19:59
Hello Ladislav
On 05-Nov-00, Ladislav Mecir wrote:
>LM Hi Brett,
>LM
<<quoted lines omitted: 4>>
>LM a/(1 + 1)
>LM
This was my problem, initially....
>LM
Regards
--
Joachim Thomas - [jthomas--mail--evo--it] - ICQ 18088459
Amiga1260+Amiga4060PPC604
Member of ATO (italian Division) http://bilbo.di.unipi.it/~ato-it & Phoenix Consortium
Concerti - Gruppi Musicali & Locali http://concertitalia.supereva.it
Virtual PHOTO Gallery http://www.terzomercato.com/free/jt_home
PS: I dont know how, but I think they must be putting an addictive
substance in the language somewhere.
-- Ryan Cole (about REBOL)
Notes
- Quoted lines have been omitted from some messages.
View the message alone to see the lines that have been omitted