How to convert given words in a block into words

I want to convert a block from block: [ a: 1 b: 2 ] to [a 1 b 2] . Is there an easier way than this?

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

+6
source share
6 answers

Saving simplicity:

 >> block: [a: 1 b: 2] == [a: 1 b: 2] >> forskip block 2 [block/1: to word! block/1] == b >> block == [a 1 b 2] 
+4
source

I had the same problem, so I wrote this function. Maybe there is a simpler solution that I don't know about.

 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 ] 
+3
source

New blocks have been created, but they are brief enough. For famous set-word/value pairs:

 collect [foreach [word val] block [keep to word! word keep val]] 

Otherwise, you can use "as in your case:

 collect [foreach val block [keep either set-word? val [to word! val][val]]] 

I would suggest that your map-each in itself is pretty concise.

+2
source

I like the DocKimbel answer, but for the sake of another alternative ...

 for i 1 length? block 2 [poke block i to word! pick block i] 
+1
source

From Graham Chiu:

In R2, you can do this:

 >> to block! form [ a: 1 b: 2 c: 3] == [a 1 b 2 c 3] 
0
source

Or using PARSE:

 block: [ a: 1 b: 2 ] parse block [some [m: set-word! (change m to-word first m) any-type!]] 
0
source

Source: https://habr.com/ru/post/944685/


All Articles