如何使用 Rebol 在块内创建子块
How to create subblock inside block with Rebol
我想得到
[["1" "a"] ["2" "b"]]
来自
["1" "" "a" "2" "" "b"]
什么是正确且最优雅的方式?我试过了,但没有得到我想要的结果:
block: ["1" "" "a" "2" "" "b"]
new-block: []
sub-block: []
foreach [first second third] block [
sub-block: []
append sub-block first
append sub-block third
append new-block sub-block
]
newblock: copy [] forskip block 3 [repend/only newblock [block/1 block/3 ]]
== [["1" "a"] ["2" "b"]]
foreach [a b c] block [repend/only [] [a c]]
另一种方法是使用 COLLECT/KEEP
:
collect [
foreach [first second third] block [
keep/only collect [
keep first
keep third
]
]
]
另一种不太优雅的方法是:
>> parse ["1" "" "a" "2" "" "b"] [
2 [set a string! skip set b string!
(append x: [] reduce [a b])
]
]
>> x
== ["1" "a" "2" "b"]
为了更多样化,这里有一个使用 MAP-EACH
的解决方案
map-each [first second third] block [reduce [first third]]
注意。以上在 Rebol (2 & 3) 中有效。 MAP-EACH
目前没有红色版本。
我想得到
[["1" "a"] ["2" "b"]]
来自
["1" "" "a" "2" "" "b"]
什么是正确且最优雅的方式?我试过了,但没有得到我想要的结果:
block: ["1" "" "a" "2" "" "b"]
new-block: []
sub-block: []
foreach [first second third] block [
sub-block: []
append sub-block first
append sub-block third
append new-block sub-block
]
newblock: copy [] forskip block 3 [repend/only newblock [block/1 block/3 ]]
== [["1" "a"] ["2" "b"]]
foreach [a b c] block [repend/only [] [a c]]
另一种方法是使用 COLLECT/KEEP
:
collect [
foreach [first second third] block [
keep/only collect [
keep first
keep third
]
]
]
另一种不太优雅的方法是:
>> parse ["1" "" "a" "2" "" "b"] [
2 [set a string! skip set b string!
(append x: [] reduce [a b])
]
]
>> x
== ["1" "a" "2" "b"]
为了更多样化,这里有一个使用 MAP-EACH
map-each [first second third] block [reduce [first third]]
注意。以上在 Rebol (2 & 3) 中有效。 MAP-EACH
目前没有红色版本。