我想将一个块从 转换block: [ a: 1 b: 2 ]
为[a 1 b 2]
. 还有比这更简单的方法吗?
map-each word block [ either set-word? word [ to-word word ] [ word ] ]
保持简单:
>> block: [a: 1 b: 2]
== [a: 1 b: 2]
>> forskip block 2 [block/1: to word! block/1]
== b
>> block
== [a 1 b 2]
我有同样的问题,所以我写了这个函数。也许有一些我不知道的更简单的解决方案。
flat-body-of: function [
"Change all set-words to words"
object [object! map!]
][
parse body: body-of object [
any [
change [set key set-word! (key: to word! key)] key
| skip
]
]
body
]
这些会创建新的块,但相当简洁。对于已知set-word/value
对:
collect [foreach [word val] block [keep to word! word keep val]]
否则,您可以在您的情况下使用 ' :
collect [foreach val block [keep either set-word? val [to word! val][val]]]
我建议你map-each
的本身也相当简洁。
我喜欢 DocKimbel 的回答,但为了另一种选择......
for i 1 length? block 2 [poke block i to word! pick block i]
Graham Chiu 的回答:
在 R2 中,您可以这样做:
>> to block! form [ a: 1 b: 2 c: 3]
== [a 1 b 2 c 3]
或使用解析:
block: [ a: 1 b: 2 ]
parse block [some [m: set-word! (change m to-word first m) any-type!]]