Other useful things


I gather here a few Red commands (or actions, or functions) that are important to understand, but don't really fit in category worth creating a chapter for. It is mostly about how Red access and evaluates words or groups of words. Understanding it can spare you a lot of confusion in the future as it is not straightforward as in some other languages.

get

Every word in Red, the native ones and the ones you create, go into a dictionary. If the word is associated with an expression, the dictionary keeps the whole expression that may or may not be evaluated depending on the type of call that fetch the word

If you want to know what is the dictionary description of a word, you use get . Notice that when you refer to a word in Red (the word itself, not the value) you precede it with a quote ( ' ). get gives you the "meaning" even of Red's native words, but returns an error if used on a value, e.g. integer! pair! tuple! :

>> get 'print
== make native! [[
    "Output...
>> get 'get
== make native! [[
    "Return...
>> a: 7
== 7
>> get 'a
== 7
>> a: [7 + 2]
== [7 + 2]
>> get 'a
== [7 + 2]
>> get 8
*** Script Error: get does not allow integer! for its word argument

mold

mold turns a datatype! (i.e. a block!, an integer! a series! etc.) into a string and returns it:

>> type? 8
== integer!
>> type? mold 8
== string!

>> print [4 + 2]
6
>> print mold [4 + 2]
[4 + 2]

Refinements
/only - Exclude outer brackets if value is a block!
/all - Return value in loadable format
/flat - Exclude all indentation
/part - Limit the length of the result, where limit is an integer!

form

form also turns a datatype! into a string, but depending on the type, the resulting text might not contain extra type information (such as [ ] { } and "") as would be produced by mold. Useful for String and text manipulation.

"My house^/    is a very^/^-funny house"
My house
    is a very
    funny house
[3 5 7]
3 5 7
>>

Allows the refinement /part to limit the number of characters.

Main uses for mold and form:

mold is basically used to turn a series into code that can be saved and interpreted later

form is basically used to generate regular text from a series

>> a: [b: drop-down data[ "one" "two" "three"][print a/text]]
== [b: drop-down data ["one" "two" "three"] [print a/text]]
>> mold a
== {[b: drop-down data ["one" "two" "three"] [print a/text]]}
>> form a
== "b drop-down data one two three print a/text"

probe

probe prints its argument without evaluation and then returns it. It prints and returns the argument "as it is", so to speak.
It is used mostly for debugging as a way of showing code (by printing) without changing it.

>> print [3 + 2]
5
>> probe [3 + 2]
[3 + 2]
== [3 + 2]

do

Evaluates the code in its arguments. In other words: executes de code. This argument can be a block, a file, a function or any other value.

Check the Files and I/O chapter before you proceed here.

do, load and save are better understood if you think of Red's console as the screen of some old computer from the 80's running some variation of basic language. You can load your program, save it, or do (execute) it.

For example, if you save a Red program as myprogram.txt and type in the following code (note that in this example the Red interpreter and the text file are in the same folder, otherwise you must set your paths right):

>> a: load %myprogram.txt

And then:

>> do a

...your program will run normally.

You can also load functions saved as text if you use:

>> do load %myfunction.txt

Notice that you can do all this from inside a Red program! So it's a powerful command.

reduce

Evaluates expressions inside a block and returns a new block with the evaluated values. Take a look at the chapter about evaluation.

>> a: [3 + 5   2 - 8   9 > 3]
== [3 + 5 2 - 8 9 > 3]
>> reduce a
== [8 -6 true]


>> b:[3 + 5 2 + 9 7 > 2 [6 + 6 3 > 9]]
== [3 + 5 2 + 9 7 > 2 [6 + 6 3 > 9]]
>> reduce b
== [8 11 true [6 + 6 3 > 9]]                    ;it does not evaluate expressions of blocks inside blocks
>> b
== [3 + 5 2 + 9 7 > 2 [6 + 6 3 > 9]]            ;the original block remains unchanged.

to

Converts one datatype! to another, e.g. an integer! to a string! , a float! to an integer! and even a string! to a number!.

>> to integer! 3.4
== 3

>> to float! 23
== 23.0

>> to string! 23.2
== "23.2"

>> to integer! "34"
== 34

wait

Stops the execution for the number of seconds given as argument.

  • Note: as of November 2017, the GUI Console does not work well with wait.

now

Returns date and time:

>> now
== 12-Dec-2017/19:24:41-02:00

Refinements

/year - Returns year only. integer!

/month - Returns month only. integer!

/day - Returns day of the month only. integer!

/time - Returns time only. time!

/zone - Returns time zone offset from UCT (GMT) only. time!

/date - Returns date only. date!

/weekday - Returns day of the week as integer! (Monday is day 1).

/yearday - Returns day of the year (Julian). integer!

/precise - High precision time. date!

/utc - Universal time (no zone). date!

results matching ""

    No results matching ""