給定一個子列表是否有一種方法來獲取對其父/包含列表/塊的引用?例如:獲取對Rebol中封閉列表/塊的引用
fs: [
usr [
local [
bin []
]
share []
]
bin []
]
l: fs/usr/local ;grab a sublist
p: none ;now grab l's parent (which is fs/usr) without hardcoding the path
感謝您的幫助!
給定一個子列表是否有一種方法來獲取對其父/包含列表/塊的引用?例如:獲取對Rebol中封閉列表/塊的引用
fs: [
usr [
local [
bin []
]
share []
]
bin []
]
l: fs/usr/local ;grab a sublist
p: none ;now grab l's parent (which is fs/usr) without hardcoding the path
感謝您的幫助!
塊沒有「父」引用!系列,因爲它可以從多個其他塊引用!系列,所以在這種情況下,「父母」的概念是沒有意義的。然而,當你需要時,你可以在你的塊中手動添加一個反向引用(只要在PROBE-ing或模具化「子」塊時注意,它就會導致引用指向的塊比你想要的更詳細的結果)。
提示:我有時會使用「隱藏」標題添加對塊值的反向引用(只是移動塊偏移量以傳遞要隱藏的值)。它是PROBE抗性和方便的,但不能被序列化(例如在磁盤上)而無需編寫自己的特定序列化例程來適當地處理這些後向引用(通常意味着用非系列值替換它們:整數,字,問題,標籤...任何可以讓你重新建立鏈接,一旦加載回來)。
例如:
>> x: [1 2 3]
== [1 2 3]
>> y: [a b c]
== [a b c]
>> insert/only x y ; insert a reference to y in x
== [1 2 3]
>> x
== [[a b c] 1 2 3] ; y reference is at first position
>> x: next x ; let's hide it now
== [1 2 3]
>> x
== [1 2 3]
>> x/-1 ; here is how to retrieve our "hidden" header
== [a b c]
請注意,/ -1的快捷方式目前沒有R3工作作爲R2。
因此,對於上面自己的代碼示例,您需要創建一個函數,該函數將潛入您的初始塊結構並插入這些隱藏的後向引用,以便您可以在需要時輕鬆訪問它們。下面是處理您的結構的示例代碼:
insert-back-ref: func [blk [block!] parent [block! none!]][
if parent [ ;-- no processing if root call
insert/only blk next head parent ;-- insert the back-reference in child
parent/1: blk: next blk ;-- reset the parent reference to child to
] ;-- point after the back-ref (and hide it)
forall blk [
if block? blk/1 [ ;-- if a child block is found
insert-back-ref blk/1 blk ;-- process its content recursively
]
]
]
insert-back-ref fs none ;-- pass none as root parent
?? fs
l: fs/usr/local
?? l
p: l/-1
?? p
p: p/-1
?? p
這將輸出:
fs: [
usr [
local [
bin []
]
share []
]
bin []
]
l: [
bin []
]
p: [
local [
bin []
]
share []
]
p: [[
local [
bin []
]
share []
]
bin []
]
事實上,我們使用嵌套塊參照已經創建了一個圖形結構,可以非常簡單地使用來導航內置系列行動和路徑。
我讀了你的第一個編輯並開始編碼,但我必須承認你的代碼比我的更冷!非常感謝解釋和代碼Doc! – rebnoob 2013-03-12 22:37:50
很高興我能幫到你。 :)一旦你習慣了系列參考和系列偏移,你就可以自己快速想出這樣的代碼,只要不斷練習。 – DocKimbel 2013-03-13 10:43:16
我當然打算!再次感謝! – rebnoob 2013-03-14 01:58:36