6

我想将一个块从 转换block: [ a: 1 b: 2 ][a 1 b 2]. 还有比这更简单的方法吗?

map-each word block [ either set-word? word [ to-word word ] [ word ] ]

4

6 回答 6

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]
于 2013-05-10T11:40:44.637 回答
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 
]
于 2013-05-10T07:08:52.413 回答
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的本身也相当简洁。

于 2013-05-10T07:22:05.313 回答
1

我喜欢 DocKimbel 的回答,但为了另一种选择......

for i 1 length? block 2 [poke block i to word! pick block i]
于 2013-05-10T12:06:29.223 回答
0

Graham Chiu 的回答:

在 R2 中,您可以这样做:

>> to block! form [ a: 1 b: 2 c: 3]
== [a 1 b 2 c 3]
于 2013-05-10T05:34:23.587 回答
0

或使用解析:

block: [ a: 1 b: 2 ]
parse block [some [m: set-word! (change m to-word first m) any-type!]]
于 2013-05-10T14:10:46.760 回答