我想将一个块从block: [ a: 1 b: 2 ]
转换为[a 1 b 2]
。
有比这更简单的方法吗?
map-each word block [ either set-word? word [ to-word word ] [ word ] ]
答案 0 :(得分:4)
保持简单:
>> block: [a: 1 b: 2]
== [a: 1 b: 2]
>> forskip block 2 [block/1: to word! block/1]
== b
>> block
== [a 1 b 2]
答案 1 :(得分:3)
我有同样的问题所以我写了这个函数。也许有一些我不知道的简单解决方案。
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
]
答案 2 :(得分:2)
这些会创建新的块,但相当简洁。对于已知的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
本身也很简洁。
答案 3 :(得分:1)
我喜欢DocKimbel的答案,但为了另一种选择...
for i 1 length? block 2 [poke block i to word! pick block i]
答案 4 :(得分:0)
来自Graham Chiu的回答:
在R2中你可以这样做:
>> to block! form [ a: 1 b: 2 c: 3]
== [a 1 b 2 c 3]
答案 5 :(得分:0)
或使用PARSE:
block: [ a: 1 b: 2 ]
parse block [some [m: set-word! (change m to-word first m) any-type!]]