I want to convert a block from block: [ a: 1 b: 2 ]
to [a 1 b 2]
.
Is there an easier way of than this?
map-each word block [ either set-word? word [ to-word word ] [ word ] ]
I want to convert a block from block: [ a: 1 b: 2 ]
to [a 1 b 2]
.
Is there an easier way of than this?
map-each word block [ either set-word? word [ to-word word ] [ word ] ]
Keeping it simple:
>> block: [a: 1 b: 2]
== [a: 1 b: 2]
>> forskip block 2 [block/1: to word! block/1]
== b
>> block
== [a 1 b 2]
I had same problem so I wrote this function. Maybe there's some simpler solution I do not know of.
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
]
These'd create new blocks, but are fairly concise. For known set-word/value
pairs:
collect [foreach [word val] block [keep to word! word keep val]]
Otherwise, you can use 'either as in your case:
collect [foreach val block [keep either set-word? val [to word! val][val]]]
I'd suggest that your map-each
is itself fairly concise also.
collect keep
approaches are very elegant –
Veator I like DocKimbel's answer, but for the sake of another alternative...
for i 1 length? block 2 [poke block i to word! pick block i]
Answer from Graham Chiu:
In R2 you can do this:
>> to block! form [ a: 1 b: 2 c: 3]
== [a 1 b 2 c 3]
sign
function there is a difference in behaviour here between r2 and r3 params: make oauth any [params []] params: sort/skip third params 2
(even after I have replaced the third
with body-of
) :-/ –
Veator Or using PARSE:
block: [ a: 1 b: 2 ]
parse block [some [m: set-word! (change m to-word first m) any-type!]]
© 2022 - 2024 — McMap. All rights reserved.