4

How to convert a block of strings into a block?

To change this:

keep rejoin['circle " " coord " " 5 " "]]]

["circle 10x10 5 " "circle 20x20 5 " "circle 30x30 5 "]

to this:

[circle 10x10 5 circle 20x20 5 circle 30x30 5]

I want to change it so it can be used with VID.

view [
      size 800x600
      base 780x580
      draw drawblock
     ]

Thanks!

Mufferaw
  • 41
  • 3

2 Answers2

2

To convert string! to Red code, you need to LOAD it: red>> load "circle 10x10 5 " == [circle 10x10 5]

So for block of string!s, just load them in loop: collect [ foreach arg ["circle 10x10 5 " "circle 20x20 5 " "circle 30x30 5 "] [ keep load arg ] ] == [circle 10x10 5 circle 20x20 5 circle 30x30 5]

However, for code generation, it is better to use Red types directly and not start with strings.

rebolek
  • 1,281
  • 7
  • 16
2

You can also use load rejoin to convert ["set " "of " "spaced " "strings"] to

red>> load rejoin ["circle 10x10 5 " "circle 20x20 5 " "circle 30x30 5 "]
== [circle 10x10 5 circle 20x20 5 circle 30x30 5]

Best thing to do is not have a block of strings in the first place and try to use literals and code as much as possible.

Edit:

For your particular use case this would work:

drawblock: collect [
    foreach arg [10 20 30] [
        keep compose [circle (as-pair arg arg) 5]
    ]
]

p.s. if you are playing around with view this gist could help

Geeky I
  • 751
  • 6
  • 22