6

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 ] ]

johnk
  • 1,102
  • 7
  • 12

6 Answers6

4

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]
DocKimbel
  • 3,127
  • 16
  • 27
3

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 
]
rebolek
  • 1,281
  • 7
  • 16
  • You should use ANY instead of SOME in there, also, you should use the SKIP keyword instead of ANY-TYPE! – Ladislav May 10 '13 at 09:21
2

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.

rgchris
  • 3,698
  • 19
  • 19
1

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]
Brett
  • 428
  • 3
  • 11
0

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]
johnk
  • 1,102
  • 7
  • 12
  • A clever but... hackish solution. To string and back? :-/ I feel like there's a sort of [essential complexity](http://en.wikipedia.org/wiki/Essential_complexity) to the problem as you stated it, and the solution from your question kind of corresponds to that in the DO dialect. It's just an issue of in place vs. out of place, etc. Can you give any more context on what you're doing? – HostileFork says dont trust SE May 10 '13 at 06:59
  • The context is trying to convert rgchris's Twitter client to R3 - http://reb4.me/r/twitter - in the `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`) :-/ – johnk May 10 '13 at 09:22
0

Or using PARSE:

block: [ a: 1 b: 2 ]
parse block [some [m: set-word! (change m to-word first m) any-type!]]
endo64
  • 2,269
  • 2
  • 27
  • 34