This is now a community project, you can collaborate on GitHub.
index parse vid series draw help about links contact
across all-over aqua areatop alphanumeric-index category-index
at backdrop base beige
below black blue bold
bottom box brick brown
button camera caret-to-offset center
center-face check coal coffee
crimson cyan data default!
disabled distance? do do-events
draw draw drop-down drop-list
dump-face event? extra facet
field find-flag? focus font
font-color font-name font-size foreach-face
forest get-scroller glass gold
gray green group-box h1
h2 h3 h4 h5
handle! handle? hidden hint
image image! image? insert-event-func
italic ivory khaki layout
leaf left linen link-sub-to-parent
link-tabs-to-parent loose magenta make-face
maroon metrics? middle mint
navy no-wrap offset-to-caret offset-to-char
offset? oldrab olive on-alt-down
on-alt-up on-aux-down on-aux-up on-change
on-click on-close on-create on-dbl-click
on-detect on-down on-drag on-drag-start
on-drop on-enter on-face-deep-change* on-focus
on-key on-key-down on-key-up on-menu
on-mid-down on-mid-up on-move on-moving
on-over on-pan on-parse-event on-press-tap
on-resize on-resizing on-rotate on-select
on-time on-two-tap on-unfocus on-up
on-zoom orange origin overlap?
pad panel papaya para
pewter pink progress purple
radio rate react reblue
rebolor red remove-event-func remove-event-func
remove-reactor request-dir request-file request-font
return rich-text right rtd-layout
screen select set-flag set-focus
show sienna silver size-text
sky slider snow space
style tab-panel tanned teal
text text-list title top
transparent underline unview view
violet water wheat white
window with wrap yello
yellow
copy skiptop alphanumeric-index category-index
box fill-pen line pentop alphanumeric-index category-index
% * ** +top alphanumeric-index category-index
- / // <
<< <= <> =
== =? > >=
>> >>> ? ??
a-an about absolute acos
action! action? add alert
all all-word! all-word? also
alter and and~ any
any-block! any-block? any-function! any-function?
any-list! any-list? any-object! any-object?
any-path! any-path? any-string! any-string?
any-type! any-word! any-word? append
arccosine arcsine arctangent arctangent2
as as-color as-ipv4 as-pair
as-rgba asin ask at
atan atan2 attempt average
back binary! binary? bind
bitset! bitset? block! block?
body-of break browse call
case catch cause-error cd
change change-dir char! char?
charset checksum clean-path clear
clear-reactions collect comma comment
complement complement? compose construct
context context? continue copy
cos cosine CR create-dir
datatype! datatype? date! date?
dbl-quote debase decompress dehex
delete difference dir dir?
dirize divide do do-safe
do-thru does dot either
ellipsize-at email! email? empty?
enbase equal? error! error?
escape eval-set-path even? event!
event? exclude exists-thru? exists?
exit exp expand expand-directives
extend external! extract extract-boot-args
face? false fetch-help fifth
file! file? find find-flag?
first flip-exe-flag float! float?
forall foreach forever form
fourth func function function!
function? get get-current-dir get-env
get-path! get-path? get-word! get-word?
greater-or-equal? greater? halt has
hash! hash? head head?
help help-string hex-to-rgb if
immediate! immediate? in index?
input insert integer! integer?
internal! intersect issue! issue?
keys-of last last-lf? length?
lesser-or-equal? lesser? lf list-dir
list-env lit-path! lit-path? lit-word!
lit-word? ll load load-thru
log-10 log-2 log-e logic!
logic? loop lowercase ls
make make-dir map! map?
math max min mod
modify modulo mold move
multiply NaN? native! native?
needs negate negative? new-line
new-line? newline next no
none none! none? normalize-dir
not not-equal? now null
number! number? object object!
object? odd? off offset?
on on-face-deep-change* on-parse-event op!
op? or or~ os-info
overlap? pad pair! pair?
paren! paren? parse parse-trace
path! path-thru path? percent!
percent? pi pick poke
positive? power prin print
probe put pwd q
query quit quit-return quote
random react react? read
read-clipboard read-thru rebol recycle
red-complete-input reduce refinement! refinement?
reflect rejoin remainder remove
remove-each repeat repend replace
return reverse round routine
routine! routine? same? save
scalar! scalar? second select
series! series? set set-current-dir
set-env set-path! set-path? set-quiet
set-word! set-word? shift shift-left
shift-logical shift-right sign? sin
sine size? skip slash
sort source sp space
spec-of split split-path sqrt
square-root stats stop-reactor strict-equal?
string! string? subtract suffix?
sum swap switch tab
tag! tag? tail tail?
take tan tangent third
throw time! time? to
to-binary to-bitset to-block to-char
to-date to-email to-file to-float
to-get-path to-get-word to-hash to-hex
to-image to-integer to-issue to-lit-path
to-lit-word to-local-date to-local-file to-logic
to-map to-none to-pair to-paren
to-path to-percent to-red-file to-refinement
to-set-path to-set-word to-string to-tag
to-time to-tuple to-typeset to-unset
to-url to-UTC-date to-word trim
true try tuple! tuple?
type? typeset! typeset? union
unique unless unset unset!
unset? until uppercase url!
url? value? values-of vector!
vector? wait what what-dir
while within? word! word?
words-of write write-clipboard write-stdout
xor xor~ yes zero?
Bases of Numbers Bit manipulation Booleantop alphanumeric-index category-index
Casting Types Comparison Conditional branching
Console Constants Create
Conversion Colors Compression
Directories Documentation Datatypes
Draw Dialect Date/Time Error
Evaluation Exiting Events
Files Formatting Functions
GUI: VID, View Help & Debug Input
Iteration Logic Maps
Math Macros Network
Objects Output Parse
Parse dialect Reflection Reactivity
Series Sets Special Purpose
String manipulation System Related Type Checking
Unknown URL/Internet Word Manipulation
debase enbase to-binary to-hextop alphanumeric-index category-index
<< >> >>> andtop alphanumeric-index category-index
and~ complement not or
or~ shift shift-left shift-logical
shift-right to-bitset xor xor~
all and and~ anytop alphanumeric-index category-index
false no none not
off on or or~
to-logic true yes
as-color as-ipv4 as-pair as-rgbatop alphanumeric-index category-index
to
< <= <> =top alphanumeric-index category-index
== =? > >=
all and and~ any
equal? greater-or-equal? greater? lesser-or-equal?
lesser? negative? not not-equal?
or or~ positive? same?
strict-equal? zero?
case catch either iftop alphanumeric-index category-index
switch throw unless
prin print probetop alphanumeric-index category-index
comma CR dbl-quote dottop alphanumeric-index category-index
escape false lf newline
no none null on
pi slash sp space
tab true yes
maketop alphanumeric-index category-index
as as-color as-ipv4 as-pairtop alphanumeric-index category-index
as-rgba debase dehex dirize
enbase form hex-to-rgb modify
to to-binary to-bitset to-block
to-char to-date to-email to-file
to-float to-get-path to-get-word to-hash
to-hex to-image to-integer to-issue
to-lit-path to-lit-word to-local-date to-local-file
to-logic to-map to-none to-pair
to-paren to-path to-percent to-red-file
to-refinement to-set-path to-set-word to-string
to-tag to-time to-tuple to-typeset
to-unset to-url to-UTC-date to-word
aqua beige black bluetop alphanumeric-index category-index
brick brown coal coffee
crimson cyan forest glass
gold gray green ivory
khaki leaf linen magenta
maroon mint navy oldrab
olive orange papaya pewter
pink purple reblue rebolor
red sienna silver sky
snow tanned teal transparent
violet water wheat white
yello yellow as-color as-rgba
hex-to-rgb tuple!
decompresstop alphanumeric-index category-index
request-dir change-dir clean-path deletetop alphanumeric-index category-index
dir dir? dirize exists?
get-current-dir list-dir ll normalize-dir
pwd read split-path suffix?
write
? ?? comment fetch-helptop alphanumeric-index category-index
help help-string source what
default! image! action! all-word!top alphanumeric-index category-index
any-block! any-function! any-list! any-object!
any-path! any-string! any-type! any-word!
binary! bitset! block! char!
datatype! email! error! event!
external! file! float! function!
get-path! get-word! hash! integer!
internal! issue! lit-path! lit-word!
logic! make map! native!
none! number! object! op!
pair! paren! path! percent!
refinement! routine! scalar! series!
set-path! set-word! string! tag!
time! to-typeset tuple! typeset!
unset! url! vector! word!
box fill-pen line pentop alphanumeric-index category-index
date! date? now to-datetop alphanumeric-index category-index
to-local-date to-time to-UTC-date
attempt catch cause-error error?top alphanumeric-index category-index
throw try
also do do-safe do-thrutop alphanumeric-index category-index
eval-set-path reduce
exit halt q quittop alphanumeric-index category-index
quit-return return
on-alt-down on-alt-up on-aux-down on-aux-uptop alphanumeric-index category-index
on-change on-click on-close on-create
on-dbl-click on-detect on-down on-drag
on-drag-start on-drop on-enter on-face-deep-change*
on-focus on-key on-key-down on-key-up
on-menu on-mid-down on-mid-up on-move
on-moving on-over on-pan on-parse-event
on-press-tap on-resize on-resizing on-rotate
on-select on-time on-two-tap on-unfocus
on-up on-zoom
request-file cd change-dir clean-pathtop alphanumeric-index category-index
create-dir delete dir dir?
dirize exists-thru? exists? file!
file? get-current-dir list-dir ll
load load-thru ls make-dir
needs normalize-dir path-thru path?
query read read-thru save
set-current-dir size? split-path suffix?
to-file to-local-file to-red-file what-dir
write
form mold padtop alphanumeric-index category-index
body-of does exit functop alphanumeric-index category-index
function function! has keys-of
native? routine routine! routine?
across all-over aqua areatop alphanumeric-index category-index
at backdrop base beige
below black blue bold
bottom box brick brown
button camera caret-to-offset center
center-face check coal coffee
crimson cyan data default!
disabled distance? do do-events
draw draw drop-down drop-list
dump-face event? extra facet
field find-flag? focus font
font-color font-name font-size foreach-face
forest get-scroller glass gold
gray green group-box h1
h2 h3 h4 h5
handle! handle? hidden hint
image image! insert-event-func italic
ivory khaki layout leaf
left linen link-sub-to-parent link-tabs-to-parent
loose magenta make-face maroon
metrics? middle mint navy
no-wrap offset-to-caret offset-to-char offset?
oldrab olive on-alt-down on-alt-up
on-aux-down on-aux-up on-change on-click
on-close on-create on-dbl-click on-detect
on-down on-drag on-drag-start on-drop
on-enter on-face-deep-change* on-focus on-key
on-key-down on-key-up on-menu on-mid-down
on-mid-up on-move on-moving on-over
on-pan on-parse-event on-press-tap on-resize
on-resizing on-rotate on-select on-time
on-two-tap on-unfocus on-up on-zoom
orange origin overlap? pad
panel papaya para pewter
pink progress purple radio
rate react reblue rebolor
red remove-event-func remove-event-func remove-reactor
request-dir request-file request-font return
rich-text right rtd-layout screen
select set-flag set-focus show
sienna silver size-text sky
slider snow space style
tab-panel tanned teal text
text-list title top transparent
underline unview view violet
water wheat white window
with wrap yello yellow
face? needs on-face-deep-change* overlap?
set-quiet to-image
? ?? about fetch-helptop alphanumeric-index category-index
help help-string source what
alert ask input last-lf?top alphanumeric-index category-index
load load-thru needs path-thru
read read-thru red-complete-input
foreach-face break continue foralltop alphanumeric-index category-index
foreach forever loop repeat
until while
all and and~ anytop alphanumeric-index category-index
logic! not or or~
extend puttop alphanumeric-index category-index
% * ** +top alphanumeric-index category-index
- / // absolute
acos add arccosine arcsine
arctangent arctangent2 asin atan
atan2 average cos cosine
divide even? exp float!
integer! log-10 log-2 log-e
math max min mod
modulo multiply NaN? negate
number! odd? pi power
random remainder round sign?
sin sine sqrt square-root
subtract sum tan tangent
to-float to-integer within?
expand expand-directivestop alphanumeric-index category-index
checksum to-emailtop alphanumeric-index category-index
bind construct context extendtop alphanumeric-index category-index
in object path! path?
last-lf? new-line? prin printtop alphanumeric-index category-index
write write-clipboard write-stdout
on-parse-event parse parse-tracetop alphanumeric-index category-index
copy skiptop alphanumeric-index category-index
keys-of reflect spec-of values-oftop alphanumeric-index category-index
words-of
react clear-reactions react react?top alphanumeric-index category-index
stop-reactor
alter append at backtop alphanumeric-index category-index
change clear collect compose
copy empty? exclude extract
fifth find first fourth
head head? index? insert
last length? move new-line
next offset? path! path?
pick poke rejoin remove
remove-each repend replace reverse
second select skip sort
swap tail tail? take
third to-hash to-map union
unique
charset difference intersect uniontop alphanumeric-index category-index
unique
alsotop alphanumeric-index category-index
a-an alter append attop alphanumeric-index category-index
back change clear collect
compose copy ellipsize-at empty?
exclude extract fifth find
first fourth head head?
index? insert last length?
lowercase move next pad
pick poke rejoin remove
replace reverse second select
skip sort split swap
tail tail? third to-string
trim uppercase
call extract-boot-args flip-exe-flag get-envtop alphanumeric-index category-index
halt list-env os-info quit
quit-return read-clipboard rebol recycle
routine set-env stats wait
write-clipboard write-stdout
event? handle! image? action?top alphanumeric-index category-index
all-word? any-block? any-function? any-list?
any-object? any-path? any-string? any-word?
binary? bitset? block? char?
complement? context? datatype? date?
dir? email? empty? equal?
error? event? face? file?
find-flag? float? function? get-path?
get-word? hash? immediate! immediate?
integer? issue? lit-path? lit-word?
logic? map? native? new-line?
none? not-equal? number? object?
op? pair? paren? path?
percent? refinement? routine? same?
scalar? series? set-path? set-word?
string? tag? time? tuple?
type? typeset? url? value?
vector? word?
as-ipv4 browse dehex do-thrutop alphanumeric-index category-index
email! exists-thru? load-thru path-thru
read read-thru to-email to-url
url! url?
get quote set to-get-wordtop alphanumeric-index category-index
to-lit-word to-set-word to-word unset
unset! unset?
The infix word % returns what is left over when argument 1 is dividedExamples
by argument 2.
Arguments can have those types:
number! char! pair! tuple! vector! time!
red>> 3 % 2
== 1
red>> 3.5 % 2
== 1.5
red>> 3.6 % 2.2
== 1.4
red>> 11x19 % 3
== 2x1
red>> 11x19 % 3x4
== 2x3
top alphanumeric-index category-index
The infix word * multiplies its two operands.Examples
It is the equivalent of the multiply function.
It operates on these types:
number! char! pair! tuple! vector!
In Red, infix operators are evaluated from left to right, with no precedence.
You may use parentheses ( ) to change the order of evaluation.
The word * is a Red word and thus needs to be separated by whitespace.
In general, the type of the result is the same type as the first operand,
where this is sensible. Errors will result when the types are incompatible.
Note the result when vectors of different lengths are multiplied.
When we multiply two vector!s, the result is a freshly allocated vector!.
When we multiply a vector! with a number!, the original vector!
will be modified.
red>> 2 * 3.7
== 7.4
red>> 3 * 1.4.8
== 1.12.8
red>> 4 * 10x20
== 40x80
red>> v1: make vector![2 3 4 5]
== make vector! [2 3 4 5]
red>> v2: make vector! [22 55]
== make vector! [22 55]
red>> v1 * v2
== make vector! [44 165]
red>> v1
== make vector! [2 3 4 5]
; v1 unchanged
red>> v1 * 9
== make vector! [18 27 36 45]
red>> v1
== make vector! [18 27 36 45]
; v1 changed
red>> 3 * v1
*** Script error: vector type is not allowed here
*** Where: *
top alphanumeric-index category-index
The infix word ** raises argument 1 to the power argument 2.Examples
red>> 2 ** 2
== 4
red>> 2 ** 10
== 1024
red>> 2 ** -1
== 0.5
red>> 2 ** -10
== 0.0009765625
red>> -2 ** 4
== 16
red>> -2 ** 3
== -8
red>> 2 ** 0.5
== 1.414213562373095
red>> 2 ** -0.5
== 0.7071067811865476
top alphanumeric-index category-index
The infix word + adds its two operands.Examples
It is the equivalent of the add function.
It operates on these types:
number! char! pair! tuple! vector! time! date!
In Red, infix operators are evaluated from left to right, with no precedence.
You may use parentheses ( ) to change the order of evaluation.
In general, the type of the result is the same type as the first operand,
where this is sensible. Errors will result when the types are incompatible.
See examples.
Note the result when vectors of different lengths are added.
The operator + is a Red word and thus needs to be separated by whitespace.
When we add two vector!s, the result is a freshly allocated vector!.
When we add a number! to a vector!, the original vector! will be modified.
red>> 1 + 3.7
== 4.7
red>> 3 + 100x200
== 103x203
red>> v1: make Vector![3 2 1 4]
== make vector! [3 2 1 4]
red>> v2: make vector![100 200]
== make vector! [100 200]
red>> v1 + v2
== make vector! [103 202]
red>> v1
== make vector! [3 2 1 4]
; v1 unchanged
red>> v2 + v1
== make vector! [103 202]
red>> v1 - 22
== make vector! [-19 -20 -21 -18]
; v1 altered
red>> v1
== make vector! [-19 -20 -21 -18]
red>> 3 + v1
*** Script error: vector type is not allowed here
*** Where: +
top alphanumeric-index category-index
The infix word - subtracts its second operand from its first operand.Examples
It is the equivalent of the subtract function.
It operates on these types:
number! char! pair! tuple! vector! time! date!
In Red, infix operators are evaluated from left to right, with no precedence.
You may use parentheses ( ) to change the order of evaluation.
The operator - is a Red word and thus needs to be separated by whitespace.
In general, the type of the result is the same type as the first operand,
where this is sensible. Errors will result when the types are incompatible.
Note the result when vectors of different lengths are subtracted.
When we subtract 2 vectors, the result is a freshly allocated vector.
When we subtract a number from a vector, the original vector will be modified.
; -------------------------------------------------------------------------
; Subtract float! from integer!
; -------------------------------------------------------------------------
red>> 3 - 1.4
1.6
; -------------------------------------------------------------------------
; Subtract pair! from integer! not possible
; -------------------------------------------------------------------------
red>> 3 - 100x100
*** Script error: - does not allow pair for its value2 argument
*** Where: -
; -------------------------------------------------------------------------
; Subtract integer! from pair! is possible
; -------------------------------------------------------------------------
red>> 100x200 - 3
== 97x197
; -------------------------------------------------------------------------
; Subtract vector!s
; -------------------------------------------------------------------------
red>> v1: make vector! [3 2 1 4]
== make vector! [3 2 1 4]
red>> v2: make vector! [100 200]
== make vector! [100 200]
red>> v1 - v2
== make vector! [-97 -198]
red>> v2 - v1
== make vector! [97 198]
red>> v2
== make vector! [100 200]
; v2 is unchanged
red>> v1 - 22
== make vector! [-19 -20 -21 -18]
red>> v1
== make vector! [-19 -20 -21 -18]
; v1 is altered!
red>> 22 - v1
*** Script error: vector type is not allowed here
*** Where: -
top alphanumeric-index category-index
The infix word / divides its operands giving a quotient.Examples
It is the equivalent of the divide function.
It operates on these types:
number! char! pair! tuple! vector!
In Red, infix operators are evaluated from left to right, with no precedence.
You may use parentheses ( ) to change the order of evaluation.
/ is a Red word and thus needs to be separated by whitespace.
When one of the operands is a float!, the result is a float!.
When both operands are integer!, the result is integer!,
truncated towards zero.
Otherwise, the type of the result is the same type as the first operand,
where this is sensible. Errors will result when the types are incompatible.
Note the result when vector!s of different lengths are divided.
When we divide two vector!s, the result is a freshly allocated vector!.
When we divide a vector! by a number!, the original vector! will be modified.
red>> 9 / 3
== 3
red>> 9.0 / 3
== 3.0
red>> 10 / 11
== 0
red>> -11 / 4
== -2
red>> 100x30 / 5
== 20x6
red>> 100x40 / 2x4
== 50x10
red>> 3.9.19 / 3
== 1.3.6
red>> v1: make vector![100 200 300 400]
== make vector! [100 200 300 400]
red>> v2: make vector![5 5 10]
== make vector! [5 5 10]
red>> v1 / v2
== make vector! [20 40 30]
red>> v1
== make vector! [100 200 300 400]
; v1 unchanged
red>> v1 / 5
== make vector! [20 40 60 80]
red>> v1
== make vector! [20 40 60 80]
; v1 changed
red>> 5 / v1
*** Script error: vector type is not allowed here
*** Where: /
top alphanumeric-index category-index
The infix word // is the nonnegative remainder oparator,Examples
that returns what is left over when the first argument is divided
by the second.
Has two arguments of type:
number! char! pair! tuple! vector!
There is also a functional version, named remainder.
If the first argument is positive, then the remainder is positive.
If the first argument is negative, then the remainder is also positive,
as in -5 // 4, which results in 3.
If the first argument is zero, then the remainder is also zero.
If the second argument is zero, a run-time error occurs.
red>> 5 // 4
== 1
red>> 5 // 5
== 0
red>> 5 // 6
== 5
red>> -5 // 4
== 3
red>> 10 // 3.3
== 0.1000000000000005
red>> 5x10 // 4
*** Internal error: reserved for future use (or not yet implemented)
*** Where: >
red>> remainder 5x10 4
== 1x2
red>> make vector![5 5 10] // 4
*** Script error: % does not allow block for its value1 argument
*** Where: %
top alphanumeric-index category-index
The VID dialect word across is used to position GUI elements following thatExamples
word at the right hand side of the previous GUI element.
Red [
needs: 'view
]
view [
across
text "Line 1"
text "Line 2"
]
top alphanumeric-index category-index
Look in the official VID documents:
https://doc.red-lang.org/en/vid.html
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
The VID dialect word aqua is a color that can be used for GUI elements.Examples
It is a tuple! of value 40.100.130
view [
text 100x50 aqua " "
]
top alphanumeric-index category-index
Provides a multi-line rectangle where text can be entered, selected, edited,
copied, pasted. We can choose the font-size, color, font, etc. Rich text
is not supported.
The default event-handler is on-change.
The text in the area can be accessed via the /text refinement.
Examples:
;-- an area with a size, specified font, and initial text.
a1: area white font [name: "Consolas" size: 12 color: black]
{area with
some text
}
120x70 [print "a1 changed"] ;-- on-change
There is a complete VID example in:
http://www.red-by-example.org/vid-text-red.html
There is an introduction to VID at:
http://www.red-by-example.org/vid.html
Its official View documentation is at:
https://doc.red-lang.org/en/view.html#_area
top alphanumeric-index category-index
Look in the official VID documents:
https://doc.red-lang.org/en/vid.html
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
Sets the background color of the container face.Examples
Argument
backdrop- name or value of a color (word! tuple! issue!).
view [
;-- set color, size, and title for window
backdrop green ;-- green window
size 300x200
title "Demo Window"
space 100x70 ;-- bigger horizontal spacing
b1: button "Hello"
b2: button "Goodbye"
return
]
top alphanumeric-index category-index
The most basic face type, but also the most versatile one. By default, it
will only display a background of color! 128.128.128. (Grey). It
provides transparency, and can be used for drawing.
The default event is on-down
VID Example:
base 140x140 %country.png ;-- supply image file name
There is a complete VID example in:
http://www.red-by-example.org/vid-misc-faces-red.html
There is an introduction to VID at:
http://www.red-by-example.org/vid.html
Its official View documentation is at:
https://doc.red-lang.org/en/view.html#_base
top alphanumeric-index category-index
The VID dialect word beige is a color that can be used for GUI elements.Examples
It is a tuple! of value 255.228.196
view [
text 100x50 beige " "
]
top alphanumeric-index category-index
The function below is used to position GUI elements following thatExamples
word below the previous GUI element.
Red [
needs: 'view
]
view [
below
text "Line 1"
text "Line 2"
]
top alphanumeric-index category-index
The VID dialect word black is a color that can be used for GUI elements.Examples
It is a tuple! of value 0.0.0
view [
text 100x50 black " "
]
top alphanumeric-index category-index
The VID dialect word blue is a color that can be used for GUI elements.Examples
It is a tuple! of value 0.0.255
view [
text 100x50 blue " "
]
top alphanumeric-index category-index
Look in the official VID documents:
https://doc.red-lang.org/en/vid.html
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
Look in the official VID documents:
https://doc.red-lang.org/en/vid.html
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
This is a VID style. It is a base type, but with a default
transparent color.
The default event is on-down
There is a complete VID example in:
http://www.red-by-example.org/vid-misc-faces-red.html
There is an introduction to VID at:
http://www.red-by-example.org/vid.html
top alphanumeric-index category-index
The VID dialect word brick is a color that can be used for GUI elements.Examples
It is a tuple! of value 178.34.34
view [
text 100x50 brick " "
]
top alphanumeric-index category-index
The VID dialect word brown is a color that can be used for GUI elements.Examples
It is a tuple! of value 139.69.19
view [
text 100x50 brown " "
]
top alphanumeric-index category-index
A button is used to confirm something, or to simply invoke some
action.
Note that on-click and on-down do the same thing for a
button.
The default event is on-down.
In VID, here are 2 buttons.
button "Click me" [print "Clicked"] ;-- respond to down
button2: button "Try Me 2" ;-- named button
on-down [button2-down] ;-- respond to down - call a function
on-over [button2-over] ;-- respond to over (hover) - call a
function
There is a complete VID example in:
http://www.red-by-example.org/vid-misc-faces-red.html
There is an introduction to VID at:
http://www.red-by-example.org/vid.html
Its official View documentation is at:
https://doc.red-lang.org/en/view.html#_button
top alphanumeric-index category-index
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
USAGE:
CARET-TO-OFFSET face pos
DESCRIPTION:
Given a text position, returns the corresponding coordinate relative
to the top-left of the layout box.
CARET-TO-OFFSET is a function! value.
ARGUMENTS:
face [object!]
pos [integer!]
REFINEMENTS:
/lower => lower end offset of the caret.
RETURNS:
[pair!]
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
Look in the official VID documents:
https://doc.red-lang.org/en/vid.html
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
USAGE:
CENTER-FACE face
DESCRIPTION:
Center a face inside its parent.
CENTER-FACE is a function! value.
ARGUMENTS:
face [object!] "Face to center."
REFINEMENTS:
/x => Center horizontally only.
/y => Center vertically only.
/with => Provide a reference face for centering instead of parent face.
parent [object!] "Reference face."
RETURNS:
Returns the centered face.
[object!]
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
A check provides a check-box, with an optional label.
The default event is on-change
The boolean state exists in 'data'. Note that 'change' can mean true to
false, or false to true.
Here is a VID example which displays the current state when checked:
check1-changed: function [] [
print ["check1-has changed. Current state is: " check1/data]
]
view [
check1: check "Check me" [check1-changed]
]
There is a complete VID example in:
http://www.red-by-example.org/vid-choice-faces-red.html
There is an introduction to VID at:
http://www.red-by-example.org/vid.html
Its official View documentation is at:
https://doc.red-lang.org/en/view.html#_check
top alphanumeric-index category-index
The VID dialect word coal is a color that can be used for GUI elements.Examples
It is a tuple! of value 64.64.64
view [
text 100x50 coal " "
]
top alphanumeric-index category-index
The VID dialect word coffee is a color that can be used for GUI elements.Examples
It is a tuple! of value 76.26.0
view [
text 100x50 coffee " "
]
top alphanumeric-index category-index
The VID dialect word crimson is a color that can be used for GUI elements.Examples
It is a tuple! of value 220.20.60
view [
text 100x50 crimson " "
]
top alphanumeric-index category-index
The VID dialect word cyan is a color that can be used for GUI elements.Examples
It is a tuple! of value 0.255.255
view [
text 100x50 cyan " "
]
top alphanumeric-index category-index
Look in the official VID documents:
https://doc.red-lang.org/en/vid.html
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
DEFAULT! is a typeset! value: make typeset! [datatype! none! logic! block!
paren! string! file! url! char! integer! float! word! set-word! lit-word!
get-word! refinement! issue! native! action! op! function! path! lit-path!
set-path! get-path! routine! bitset! object! typeset! error! vector! hash!
pair! percent! tuple! map! binary! time! tag! email! handle! date! image!
event!]
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
Look in the official VID documents:
https://doc.red-lang.org/en/vid.html
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
USAGE:
DISTANCE? A B
DESCRIPTION:
Returns the distance between the center of two faces.
DISTANCE? is a function! value.
ARGUMENTS:
A [object!] "First face."
B [object!] "Second face."
RETURNS:
Distance between them.
[float!]
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
Look in the official VID documents:
https://doc.red-lang.org/en/vid.html
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
USAGE:
DO-EVENTS
DESCRIPTION:
Launch the event loop, blocks until all windows are closed.
DO-EVENTS is a function! value.
REFINEMENTS:
/no-wait => Process an event in the queue and returns at once.
RETURNS:
Returned value from last event.
[logic! word!]
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
USAGE:
DRAW image cmd
DESCRIPTION:
Draws scalable vector graphics to an image.
DRAW is a function! value.
ARGUMENTS:
image [image! pair!] "Image or size for an image."
cmd [block!] "Draw commands."
REFINEMENTS:
/transparent => Make a transparent image, if pair! spec is used.
RETURNS:
[image!]
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
USAGE:
DRAW image cmd
DESCRIPTION:
Draws scalable vector graphics to an image.
DRAW is a function! value.
ARGUMENTS:
image [image! pair!] "Image or size for an image."
cmd [block!] "Draw commands."
REFINEMENTS:
/transparent => Make a transparent image, if pair! spec is used.
RETURNS:
[image!]
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
A drop-down represents an edit field with a vertical list of text strings
displayed in a foldable frame. (i.e. a 'combo'). A vertical scrollbar
appears automatically if the content does not fit the frame.
The default event handler is on-enter.
The drop-down, drop-list, and text-list are related.
A none value for 'selection' is returned if no choice has been made.
The associative-array possibility exists, as in drop-list
It is possible to preset the edit field, which might be used as an initial
selection, or a prompt.
Example in VID:
drop-down1: drop-down "Choose Code" data ["A1" "A2" "B1" "B2"]
There is a complete VID example in:
http://www.red-by-example.org/vid-choice-faces-red.html
There is an introduction to VID at:
http://www.red-by-example.org/vid.html
Its official View documentation is at:
https://doc.red-lang.org/en/view.html#_drop_down
top alphanumeric-index category-index
This type represents a vertical list of text strings, displayed in a
foldable frame. A vertical scrollbar appears automatically if the content
does not fit the frame.
The default event handler is on-change
The drop-down, drop-list, and text-list are related.
A none value for 'selection' is returned if no choice has been made.
A drop-down is similar, but it provides an additional edit field (a
'combo').
The drop-list only displays strings, though the 'data' values can include
other types. This can give an associative-array effect. For example, to
find the postage cost of a letter, we can use this in VID:
postal-drop-list: drop-list data["Basic" 33 "2nd-class" 40 "Priority" 99]
Only the 3 strings are displayed.
Multiplying the selected position by 2 would take us to the integer cost
item.
There is a complete VID example in:
http://www.red-by-example.org/vid-choice-faces-red.html
There is an introduction to VID at:
http://www.red-by-example.org/vid.html
Its official View documentation is at:
https://doc.red-lang.org/en/view.html#_drop_list
top alphanumeric-index category-index
See the official View documents:
https://doc.red-lang.org/en/view.html
top alphanumeric-index category-index
Look in the official VID documents:
https://doc.red-lang.org/en/vid.html
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
Look in the official VID documents:
https://doc.red-lang.org/en/vid.html
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
Look in the official VID documents:
https://doc.red-lang.org/en/vid.html
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
A field provides single-line data entry. We can provide an initial text
value, and provide a different pixel width if the default width is
unsuitable.
The default event is on-enter.
We access the data entered with /text.
VID Examples:
f1: field ;-- default size, empty
f2: field "a field" ;-- default size, shows "a field"
f3: field "Enter name here" 200 ;-- wider
There is a complete VID example in:
http://www.red-by-example.org/vid-text-red.html
There is an introduction to VID at:
http://www.red-by-example.org/vid.html
Its official View documentation is at:
https://doc.red-lang.org/en/view.html#_field
top alphanumeric-index category-index
Look in the official VID documents:
https://doc.red-lang.org/en/vid.html
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
Look in the official VID documents:
https://doc.red-lang.org/en/vid.html
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
Look in the official VID documents:
https://doc.red-lang.org/en/vid.html
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
Look in the official VID documents:
https://doc.red-lang.org/en/vid.html
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
Look in the official VID documents:
https://doc.red-lang.org/en/vid.html
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
Look in the official VID documents:
https://doc.red-lang.org/en/vid.html
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
USAGE:
FOREACH-FACE face body
DESCRIPTION:
Evaluates body for each face in a face tree matching the condition.
FOREACH-FACE is a function! value.
ARGUMENTS:
face [object!] "Root face of the face tree."
body [block! function!] {Body block (`face` object) or
function `func [face [object!]]`.}
REFINEMENTS:
/with => Filter faces according to a condition.
spec [block! none!] "Condition applied to face object."
/post => Evaluates body for current face after processing its
children.
/sub =>
post? "Do not rebind body and spec, internal use only."
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
The VID dialect word forest is a color that can be used for GUI elements.Examples
It is a tuple! of value 0.48.0
view [
text 100x50 forest " "
]
top alphanumeric-index category-index
USAGE:
GET-SCROLLER face orientation
DESCRIPTION:
return a scroller object from a face.
GET-SCROLLER is a function! value.
ARGUMENTS:
face [object!]
orientation [word!]
RETURNS:
[object!]
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
The VID dialect provides named colors. Each color has a tuple! value.
For example, here we create a brown text element:
view [
text 100x50 brown " "
]
top alphanumeric-index category-index
The VID dialect word gold is a color that can be used for GUI elements.Examples
It is a tuple! of value 255.205.40
view [
text 100x50 gold " "
]
top alphanumeric-index category-index
The VID dialect word gray is a color that can be used for GUI elements.Examples
It is a tuple! of value 128.128.128
view [
text 100x50 gray " "
]
top alphanumeric-index category-index
The VID dialect word green is a color that can be used for GUI elements.Examples
It is a tuple! of value 0.255.0
view [
text 100x50 green " "
]
top alphanumeric-index category-index
(Awaiting additional documentation by red-by-example team.)
Possible phasing-out.
top alphanumeric-index category-index
The VID dialect has some defined styles (shortcuts). The styles h1 h2 h3
h4 h5 display text of a pre-defined size. The largest is h1. We have:
h1 - a text preset to font-size 32
h2 - a text preset to font-size 26
h3 - a text preset to font-size 22
h4 - a text preset to font-size 17
h5 - a text preset to font-size 13
Here is an example:
h1 "Biggest Text Heading"
message: h3 "Medium-sized yellow heading..." yellow
http://www.red-by-example.org/vid-text-faces-red.html
There is an introduction to VID at:
http://www.red-by-example.org/vid.html
top alphanumeric-index category-index
The VID dialect has some defined styles (shortcuts). The styles h1 h2 h3
h4 h5 display text of a pre-defined size. The largest is h1. We have:
h1 - a text preset to font-size 32
h2 - a text preset to font-size 26
h3 - a text preset to font-size 22
h4 - a text preset to font-size 17
h5 - a text preset to font-size 13
Here is an example:
h1 "Biggest Text Heading"
message: h3 "Medium-sized yellow heading..." yellow
http://www.red-by-example.org/vid-text-faces-red.html
There is an introduction to VID at:
http://www.red-by-example.org/vid.html
top alphanumeric-index category-index
The VID dialect has some defined styles (shortcuts). The styles h1 h2 h3
h4 h5 display text of a pre-defined size. The largest is h1. We have:
h1 - a text preset to font-size 32
h2 - a text preset to font-size 26
h3 - a text preset to font-size 22
h4 - a text preset to font-size 17
h5 - a text preset to font-size 13
Here is an example:
h1 "Biggest Text Heading"
message: h3 "Medium-sized yellow heading..." yellow
http://www.red-by-example.org/vid-text-faces-red.html
There is an introduction to VID at:
http://www.red-by-example.org/vid.html
top alphanumeric-index category-index
The VID dialect has some defined styles (shortcuts). The styles h1 h2 h3
h4 h5 display text of a pre-defined size. The largest is h1. We have:
h1 - a text preset to font-size 32
h2 - a text preset to font-size 26
h3 - a text preset to font-size 22
h4 - a text preset to font-size 17
h5 - a text preset to font-size 13
Here is an example:
h1 "Biggest Text Heading"
message: h3 "Medium-sized yellow heading..." yellow
http://www.red-by-example.org/vid-text-faces-red.html
There is an introduction to VID at:
http://www.red-by-example.org/vid.html
top alphanumeric-index category-index
The VID dialect has some defined styles (shortcuts). The styles h1 h2 h3
h4 h5 display text of a pre-defined size. The largest is h1. We have:
h1 - a text preset to font-size 32
h2 - a text preset to font-size 26
h3 - a text preset to font-size 22
h4 - a text preset to font-size 17
h5 - a text preset to font-size 13
Here is an example:
h1 "Biggest Text Heading"
message: h3 "Medium-sized yellow heading..." yellow
http://www.red-by-example.org/vid-text-faces-red.html
There is an introduction to VID at:
http://www.red-by-example.org/vid.html
top alphanumeric-index category-index
A handle! datatype will be used in the future for opaque OS handles for
graphical objects.
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
USAGE:
HANDLE? value
DESCRIPTION:
Returns true if the value is this type.
HANDLE? is a function! value.
ARGUMENTS:
value [any-type!]
A handle! datatype will be used in the future for opaque OS handles for
graphical objects.
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
Look in the official VID documents:
https://doc.red-lang.org/en/vid.html
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
Displays a hint message if a field is empty, which disappearsExamples
when an entry is made.
Argument
a string!
view [
title "Demo Window"
b1: button "Hello"
b2: button "Goodbye"
return
t1: field hint "Enter name" ;-- hint if nothing entered
]
top alphanumeric-index category-index
This is a VID style. It is a base type, but with a default size of
100x100. It expects an image option.
The default event is on-down
There is a complete VID example in:
http://www.red-by-example.org/vid-misc-faces-red.html
There is an introduction to VID at:
http://www.red-by-example.org/vid.html
top alphanumeric-index category-index
(Awaiting additional documentation by red-by-example team.)
Values of this datatype contain a series! of RGBA values, which represent
pixels in a 2D image. Consult the official Red documentation at:
https://doc.red-lang.org/en/datatypes/image.html
top alphanumeric-index category-index
USAGE:
IMAGE? value
DESCRIPTION:
Returns true if the value is this type.
IMAGE? is a function! value.
ARGUMENTS:
value [any-type!]
See image!
top alphanumeric-index category-index
See the official View documents:
https://doc.red-lang.org/en/view.html
top alphanumeric-index category-index
Look in the official VID documents:
https://doc.red-lang.org/en/vid.html
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
The VID dialect word ivory is a color that can be used for GUI elements.Examples
It is a tuple! of value 255.255.240
view [
text 100x50 ivory " "
]
top alphanumeric-index category-index
The VID dialect word khaki is a color that can be used for GUI elements.Examples
It is a tuple! of value 179.179.126
view [
text 100x50 khaki " "
]
top alphanumeric-index category-index
USAGE:
LAYOUT spec
DESCRIPTION:
Return a face with a pane built from a VID description.
LAYOUT is a function! value.
ARGUMENTS:
spec [block!] "Dialect block of styles, attributes, and layouts."
REFINEMENTS:
/tight => Zero offset and origin.
/options =>
user-opts [block!] "Optional features in [name: value] format."
/flags =>
flgs [block! word!] "One or more window flags."
/only => Returns only the pane block.
/parent =>
panel [object!]
divides [integer! none!]
/styles => Use an existing styles list.
css [block!] "Styles list.
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
The VID dialect word leaf is a color that can be used for GUI elements.Examples
It is a tuple! of value 0.128.0
view [
text 100x50 leaf " "
]
top alphanumeric-index category-index
Look in the official VID documents:
https://doc.red-lang.org/en/vid.html
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
The VID dialect word linen is a color that can be used for GUI elements.Examples
It is a tuple! of value 250.240.230
view [
text 100x50 linen " "
]
top alphanumeric-index category-index
USAGE:
LINK-SUB-TO-PARENT face type old new
DESCRIPTION:
Internal Use Only.
LINK-SUB-TO-PARENT is a function! value.
ARGUMENTS:
face [object!]
type [word!]
old
new
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
USAGE:
LINK-TABS-TO-PARENT face
DESCRIPTION:
Internal Use Only.
LINK-TABS-TO-PARENT is a function! value.
ARGUMENTS:
face [object!]
REFINEMENTS:
/init => Force /show of first tab.
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
Look in the official VID documents:
https://doc.red-lang.org/en/vid.html
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
The VID dialect word magenta is a color that can be used for GUI elements.Examples
It is a tuple! of value 255.0.255
view [
text 100x50 magenta " "
]
top alphanumeric-index category-index
USAGE:
MAKE-FACE style
DESCRIPTION:
Make a face from a given style name or example face.
MAKE-FACE is a function! value.
ARGUMENTS:
style [word!] "A face type."
REFINEMENTS:
/spec =>
blk [block!] "Spec block of face options expressed in VID."
/offset =>
xy [pair!] "Offset of the face."
/size =>
wh [pair!] "Size of the face."
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
The VID dialect word maroon is a color that can be used for GUI elements.Examples
It is a tuple! of value 128.0.0
view [
text 100x50 maroon " "
]
top alphanumeric-index category-index
USAGE:
METRICS? face type
DESCRIPTION:
Returns a pair! value in the type metrics for the argument face.
METRICS? is a function! value.
ARGUMENTS:
face [object!] "Face object to query."
type [word!] "Metrics type: 'paddings or 'margins."
REFINEMENTS:
/total => Return the addition of metrics along an axis.
axis [word!] "Axis to use for addition: 'x or 'y."
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
Look in the official VID documents:
https://doc.red-lang.org/en/vid.html
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
The VID dialect word mint is a color that can be used for GUI elements.Examples
It is a tuple! of value 100.136.116
view [
text 100x50 mint " "
]
top alphanumeric-index category-index
The VID dialect word navy is a color that can be used for GUI elements.Examples
It is a tuple! of value 0.0.128
view [
text 100x50 navy " "
]
top alphanumeric-index category-index
To do by red-by-example team ...
USAGE:
OFFSET-TO-CARET face pt
DESCRIPTION:
Given a coordinate, returns the corresponding caret position.
OFFSET-TO-CARET is a function! value.
ARGUMENTS:
face [object!]
pt [pair!]
RETURNS:
[integer!]
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
USAGE:
OFFSET-TO-CHAR face pt
DESCRIPTION:
Given a coordinate, returns the corresponding character position.
OFFSET-TO-CHAR is a function! value.
ARGUMENTS:
face [object!]
pt [pair!]
RETURNS:
[integer!]
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
To do by red-by-example team ...
The VID dialect word oldrab is a color that can be used for GUI elements.Examples
It is a tuple! of value 72.72.16
view [
text 100x50 oldrab " "
]
top alphanumeric-index category-index
The VID dialect word olive is a color that can be used for GUI elements.Examples
It is a tuple! of value 128.128.0
view [
text 100x50 olive " "
]
top alphanumeric-index category-index
See 'Actors' in the official VID documents:
https://doc.red-lang.org/en/vid.html
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
See 'Actors' in the official VID documents:
https://doc.red-lang.org/en/vid.html
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
See 'Actors' in the official VID documents:
https://doc.red-lang.org/en/vid.html
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
See 'Actors' in the official VID documents:
https://doc.red-lang.org/en/vid.html
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
See 'Actors' in the official VID documents:
https://doc.red-lang.org/en/vid.html
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
See 'Actors' in the official VID documents:
https://doc.red-lang.org/en/vid.html
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
See 'Actors' in the official VID documents:
https://doc.red-lang.org/en/vid.html
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
See 'Actors' in the official VID documents:
https://doc.red-lang.org/en/vid.html
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
See 'Actors' in the official VID documents:
https://doc.red-lang.org/en/vid.html
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
See 'Actors' in the official VID documents:
https://doc.red-lang.org/en/vid.html
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
See 'Actors' in the official VID documents:
https://doc.red-lang.org/en/vid.html
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
See 'Actors' in the official VID documents:
https://doc.red-lang.org/en/vid.html
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
See 'Actors' in the official VID documents:
https://doc.red-lang.org/en/vid.html
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
See 'Actors' in the official VID documents:
https://doc.red-lang.org/en/vid.html
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
See 'Actors' in the official VID documents:
https://doc.red-lang.org/en/vid.html
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
See 'Actors' in the official VID documents:
https://doc.red-lang.org/en/vid.html
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
See 'Actors' in the official VID documents:
https://doc.red-lang.org/en/vid.html
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
See 'Actors' in the official VID documents:
https://doc.red-lang.org/en/vid.html
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
See 'Actors' in the official VID documents:
https://doc.red-lang.org/en/vid.html
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
See 'Actors' in the official VID documents:
https://doc.red-lang.org/en/vid.html
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
See 'Actors' in the official VID documents:
https://doc.red-lang.org/en/vid.html
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
See 'Actors' in the official VID documents:
https://doc.red-lang.org/en/vid.html
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
See 'Actors' in the official VID documents:
https://doc.red-lang.org/en/vid.html
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
See 'Actors' in the official VID documents:
https://doc.red-lang.org/en/vid.html
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
See 'Actors' in the official VID documents:
https://doc.red-lang.org/en/vid.html
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
See 'Actors' in the official VID documents:
https://doc.red-lang.org/en/vid.html
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
See 'Actors' in the official VID documents:
https://doc.red-lang.org/en/vid.html
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
To do by red-by-example team ...
See 'Actors' in the official VID documents:
https://doc.red-lang.org/en/vid.html
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
See 'Actors' in the official VID documents:
https://doc.red-lang.org/en/vid.html
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
See 'Actors' in the official VID documents:
https://doc.red-lang.org/en/vid.html
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
See 'Actors' in the official VID documents:
https://doc.red-lang.org/en/vid.html
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
See 'Actors' in the official VID documents:
https://doc.red-lang.org/en/vid.html
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
See 'Actors' in the official VID documents:
https://doc.red-lang.org/en/vid.html
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
See 'Actors' in the official VID documents:
https://doc.red-lang.org/en/vid.html
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
See 'Actors' in the official VID documents:
https://doc.red-lang.org/en/vid.html
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
See 'Actors' in the official VID documents:
https://doc.red-lang.org/en/vid.html
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
See 'Actors' in the official VID documents:
https://doc.red-lang.org/en/vid.html
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
The VID dialect word orange is a color that can be used for GUI elements.Examples
It is a tuple! of value 255.150.10
view [
text 100x50 orange " "
]
top alphanumeric-index category-index
Look in the official VID documents:
https://doc.red-lang.org/en/vid.html
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
Look in the official VID documents:
https://doc.red-lang.org/en/vid.html
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
Look in the official VID documents:
https://doc.red-lang.org/en/vid.html
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
A panel is a container for other faces. We supply a block of faces.
The default event is on-down
In VID, here is a black panel containing 2 buttons:
panel1: panel black [
panel1-b1: button "panel1-b1" [mytext/text: "panel1-b1 clicked" ]
panel1-b2: button "panel1-b2" [mytext/text: "panel1-b2 clicked" ]
]
There is a complete VID example in:
http://www.red-by-example.org/vid-choice-faces-red.html
There is an introduction to VID at:
http://www.red-by-example.org/vid.html
Its official View documentation is at:
https://doc.red-lang.org/en/view.html#_panel
top alphanumeric-index category-index
The VID dialect word papaya is a color that can be used for GUI elements.Examples
It is a tuple! of value 255.80.37
view [
text 100x50 papaya " "
]
top alphanumeric-index category-index
Look in the official VID documents:
https://doc.red-lang.org/en/vid.html
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
The VID dialect word pewter is a color that can be used for GUI elements.Examples
It is a tuple! of value 170.170.170
view [
text 100x50 pewter " "
]
top alphanumeric-index category-index
The VID dialect word pink is a color that can be used for GUI elements.Examples
It is a tuple! of value 255.164.200
view [
text 100x50 pink " "
]
top alphanumeric-index category-index
This type represents a horizontal or vertical progress bar.
The default orientation is horizontal, because the default size for faces
is wider than its height. For a vertical slider, specify a size that is
taller than its width.
The optional value for the initial position can be a percent, or a float
between 0.0 and 1.0.
The default event is on-change
Its current value is available in 'data'.
There is a complete VID example in:
http://www.red-by-example.org/vid-misc-faces-red.html
There is an introduction to VID at:
http://www.red-by-example.org/vid.html
Its official View documentation is at:
https://doc.red-lang.org/en/view.html#_progress
top alphanumeric-index category-index
The VID dialect word purple is a color that can be used for GUI elements.Examples
It is a tuple! of value 128.0.128
view [
text 100x50 purple " "
]
top alphanumeric-index category-index
A radio provides a radio-button, with an optional label. Only one radio
button per panel is allowed to be on.
The default event-handler is on-change, and the boolean state exists in
'data'.
Here is a VID example with 3 radios labelled "Big", "Small", "Medium".
Changing any of them results is the 3 states being printed.
panel2: panel orange [ ;-- orange panel, 3 radios
panel2-r1: radio "Big"
[print ["radios: " panel2-r1/data panel2-r2/data panel2-r3/data]]
panel2-r2: radio "Small"
[print ["radios: " panel2-r1/data panel2-r2/data panel2-r3/data]]
panel2-r3: radio "Medium"
[print ["radios: " panel2-r1/data panel2-r2/data panel2-r3/data]]
]
There is a complete VID example in:
http://www.red-by-example.org/vid-choice-faces-red.html
There is an introduction to VID at:
http://www.red-by-example.org/vid.html
Its official View documentation is at:
https://doc.red-lang.org/en/view.html#_radio
top alphanumeric-index category-index
Look in the official VID documents:
https://doc.red-lang.org/en/vid.html
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
See the official VID documents:
https://doc.red-lang.org/en/vid.html
top alphanumeric-index category-index
The VID dialect word reblue is a color that can be used for GUI elements.Examples
It is a tuple! of value 38.58.108
view [
text 100x50 reblue " "
]
top alphanumeric-index category-index
The VID dialect word rebolor is a color that can be used for GUI elements.Examples
It is a tuple! of value 142.128.110
view [
text 100x50 rebolor " "
]
top alphanumeric-index category-index
The VID dialect word red is a color that can be used for GUI elements.Examples
It is a tuple! of value 255.0.0
view [
text 100x50 red " "
]
top alphanumeric-index category-index
See the official View documents:
https://doc.red-lang.org/en/view.html
top alphanumeric-index category-index
See the official View documents:
https://doc.red-lang.org/en/view.html
top alphanumeric-index category-index
See the official View documents:
https://doc.red-lang.org/en/view.html
top alphanumeric-index category-index
Causes a dialog to appear asking the user to select a directory, and returnsExamples
the full directory path as a file! type, or a block of paths.
It returns none if no selection is made. The title bar shows:
'Browse For Folder'
It works in a Red GUI program, and also in a non-GUI console program.
There are no arguments.
Refinements
/title - to be displayed under the title bar. We provide a string!
/dir - Set starting directory. We provide a name, which can be a
string! or file!.
/filter - To Be Developed: Block of filters (filter-name filter):
a block!
/keep - Keep previous directory path
/multi - To Be Developed: Allows multiple file selection, returned as
a block.
; -------------------------------------------------------------------------
; Use request-dir from the Red console, on Windows
; -------------------------------------------------------------------------
red>> choice: request-dir
;user browsed to c:\mike\ALBUM\ Note the %, indicating a file!
red>> choice
== %/C/ALBUM/
; -------------------------------------------------------------------------
; /dir and /title refinements
; -------------------------------------------------------------------------
;add a title, and start with C:/mike selected
red>> print request-dir/title/dir "Choose Dir" "C:\mike"
== %/C/Mike/data/
top alphanumeric-index category-index
Causes a dialog to appear asking the user to select a file, andExamples
returns the full file path as a file! type, or block of paths.
It returns none if no selection is made. 'Open' is displayed
as the default title. It works in a Red GUI app, and also in a
non-GUI console app.
There are no arguments
Refinements
/title - window title. We supply a string!
/file - Default file name or directory. We supply a name as
a string! or file!.
/filter - supply a block of filters consisting of pairs of filter
names, and the actual filters.
/save - File save mode.
/multi - Allows multiple file selection, returned as a block.
; -------------------------------------------------------------------------
; request-file examples
; -------------------------------------------------------------------------
;display an 'open' dialog, on Windows
red>> choice: request-file
;note the file! type result
red>> choice
== %/C/red/try-demos.red
; -------------------------------------------------------------------------
; /filter refinement .jpg, .red
; -------------------------------------------------------------------------
red>> print request-file/filter ["jpegs" "*.jpg" "Red files" "*.red"]
top alphanumeric-index category-index
Causes a dialog to appear asking the user to select a font, and returnsExamples
a font! object!, or none if no selection is made.
It works in a Red GUI program, and also in a non-GUI console program.
There are no arguments
Refinements
/mono - Show monospaced font only.
; -------------------------------------------------------------------------
; request-font examples
; -------------------------------------------------------------------------
;display the dialog. The Dayton font is selected by the user here
red>> choice: request-font
;note the result
red>> print choice
name: "Dayton"
size: 11
style: none
angle: 0
color: none
anti-alias?: false
shadow: none
state: none
parent: none
;and it is an object
red>> type? choice
== object!
top alphanumeric-index category-index
The VID dialect word return is used to position the next GUIExamples
elements from the left side of the window.
Red [
needs: 'view
]
view [
across
text "Line 1"
text "Line 2"
return
text "Line 3"
]
top alphanumeric-index category-index
Rich-text is supported. See:Examples
https://github.com/red/red/wiki/Rich-Text-support
view
[ ;-- show a rich text face, with differing font size, colors
out-rich: rich-text 300x100 data [
i b "Hello" /b font 24 red " Red " /font blue "World!" /i
]
]
top alphanumeric-index category-index
Look in the official VID documents:
https://doc.red-lang.org/en/vid.html
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
USAGE:
RTD-LAYOUT spec
DESCRIPTION:
Returns a rich-text face from a RTD source code.
RTD-LAYOUT is a function! value.
ARGUMENTS:
spec [block!] "RTD source code."
REFINEMENTS:
/only => Returns only [text data] facets.
/with => Populate an existing face object.
face [object!] "Face object to populate."
RETURNS:
[object! block!]
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
Represents a graphic display unit connected to the computer (usually a
monitor).
Its official View documentation is at:
https://doc.red-lang.org/en/view.html#_screen
top alphanumeric-index category-index
Look in the official VID documents:
https://doc.red-lang.org/en/vid.html
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
USAGE:
SET-FLAG face facet value
DESCRIPTION:
Sets a flag in a face object.
SET-FLAG is a function! value.
ARGUMENTS:
face [object!]
facet [word!]
value [any-type!]
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
USAGE:
SET-FOCUS face
DESCRIPTION:
Sets the focus on the argument face.
SET-FOCUS is a function! value.
ARGUMENTS:
face [object!]
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
See the official View documents:
https://doc.red-lang.org/en/view.html
top alphanumeric-index category-index
The VID dialect provides named colors. Each color has a tuple! value.
For example, here we create a brown text element:
view [
text 100x50 brown " "
]
top alphanumeric-index category-index
The VID dialect provides named colors. Each color has a tuple! value.
For example, here we create a brown text element:
view [
text 100x50 brown " "
]
top alphanumeric-index category-index
In system/words, sometimes used in rich-text programming.
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
The VID dialect provides named colors. Each color has a tuple! value.
For example, here we create a brown text element:
view [
text 100x50 brown " "
]
top alphanumeric-index category-index
This type represents a cursor which can be moved along a horizontal or
vertical axis. The optional value for the initial position can be a
percent, or a float between 0.0 and 1.0.
Its default event is: on-change
Its current value is available in 'data'.
The default orientation is horizontal, because the default size for
faces is wider than its height. For a vertical slider, specify a
size that is taller than its width.
Here is VID example where a slider's value is displayed in a text
and used to control a progress bar. Note the use of 'face' to
represent the current face. We could also have written 'slider1'.
slider1: slider 20x100 50% [ ;--vertical, initially at middle
mytext/text: face/data
progress1/data: face/data
]
There is a complete VID example in:
http://www.red-by-example.org/vid-misc-faces-red.html
There is an introduction to VID at:
http://www.red-by-example.org/vid.html
Its official View documentation is at:
https://doc.red-lang.org/en/view.html#_slider
top alphanumeric-index category-index
The VID dialect provides named colors. Each color has a tuple! value.
For example, here we create a brown text element:
view [
text 100x50 brown " "
]
top alphanumeric-index category-index
Changes the default spacing between faces.Examples
Argument
a pair!
view [
size 300x200
title "Demo Window"
space 100x70 ;-- bigger horizontal/vertical spacing
b1: button "Hello"
b2: button "Goodbye"
return
]
top alphanumeric-index category-index
Look in the official VID documents:
https://doc.red-lang.org/en/vid.html
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
A collection of panels, only one is visible at a time. We supply a block
of strings for the tabs.
The default event is: on-select.
Here is a VID example, with 3 tabs:
tab-panel 250x120 [
"Tab A" [h4 "In a tab!" button "Click in A" [print "A"] ]
"Tab B" [button "Click in B" [print "B"] ]
"Tab C" [button "Click in C" [print "C"] ]
]
There is a complete VID example in:
http://www.red-by-example.org/vid-misc-faces-red.html
There is an introduction to VID at:
http://www.red-by-example.org/vid.html
Its official View documentation is at:
https://doc.red-lang.org/en/view.html#_tab_panel
top alphanumeric-index category-index
The VID dialect provides named colors. Each color has a tuple! value.
For example, here we create a brown text element:
view [
text 100x50 brown " "
]
top alphanumeric-index category-index
The VID dialect provides named colors. Each color has a tuple! value.
For example, here we create a brown text element:
view [
text 100x50 brown " "
]
top alphanumeric-index category-index
This face is used to display text data on-screen. The data can be
changed later, e.g. by:
text-my-name/text: "Jimi"
There are also h1 h2 h3 h4 h5, which provide a
slightly shorter way of creating text with common font-sizes, as in:
h1: "Very big heading"
h5: "Small heading"
We cannot key-in data to a text - use the field for this.
Here is a VID example with a yellow text and a button. When we
click it, the text displays "Goodbye!":
view [
message: text yellow "Hello!" ;-- yellow, with initial string
button "Press Me" [message/text: "Goodbye!"] ;-- change it
]
There is a complete VID example in:
http://www.red-by-example.org/vid-text-faces-red.html
There is an introduction to VID at:
http://www.red-by-example.org/vid.html
Its official View documentation is at:
https://doc.red-lang.org/en/view.html#_text
top alphanumeric-index category-index
This type represents a vertical list of text strings, displayed in a
fixed frame. A vertical scrollbar appears automatically if the content
does not fit the frame.
The drop-down, drop-list, and text-list are related.
The default event-handler is on-change
'selected' provides the position of the selection. This is -1 if no
selection exists.
In VID:
days-text-list1: text-list pink 90x110 data [
"Monday" "Tuesday" "Wednesday" "Thursday"
]
We can choose to preset an initial selection like this:
days-text-list: text-list pink 90x110 select 2 ;-- 2 refers to
"Tuesday"
data [
"Monday" "Tuesday" "Wednesday" "Thursday"
]
There is a complete VID example in:
http://www.red-by-example.org/vid-choice-faces-red.html
There is an introduction to VID at:
http://www.red-by-example.org/vid.html
Its official View documentation is at:
https://doc.red-lang.org/en/view.html#_text_list
top alphanumeric-index category-index
Sets the container title.Examples
Argument
A string!
view [
;-- set title for window
backdrop green
size 300x200
title "Demo Window" ;-- title of window
b1: button "Hello"
b2: button "Goodbye"
return
]
top alphanumeric-index category-index
Look in the official VID documents:
https://doc.red-lang.org/en/vid.html
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
The VID dialect provides named colors. Each color has a tuple! value.
For example, here we create a brown text element:
view [
text 100x50 brown " "
]
top alphanumeric-index category-index
Look in the official VID documents:
https://doc.red-lang.org/en/vid.html
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
USAGE:
UNVIEW
DESCRIPTION:
Close last opened window view.
UNVIEW is a function! value.
REFINEMENTS:
/all => Close all views.
/only => Close a given view.
face [object!] "Window view to close."
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
The function view is used to:Examples
- declare a GUI layout using VID
- invoke that GUI layout
Red [
needs: 'view
]
view [
below
text "Line 1"
text "Line 2"
]
top alphanumeric-index category-index
The VID dialect provides named colors. Each color has a tuple! value.
For example, here we create a brown text element:
view [
text 100x50 brown " "
]
top alphanumeric-index category-index
The VID dialect provides named colors. Each color has a tuple! value.
For example, here we create a brown text element:
view [
text 100x50 brown " "
]
top alphanumeric-index category-index
The VID dialect provides named colors. Each color has a tuple! value.
For example, here we create a brown text element:
view [
text 100x50 brown " "
]
top alphanumeric-index category-index
The VID dialect provides named colors. Each color has a tuple! value.
For example, here we create a brown text element:
view [
text 100x50 brown " "
]
top alphanumeric-index category-index
Represents a window displayed on the OS desktop.
In VID the window is made large enough to hold all the faces, but we
can also choose our own fixed size.
The View Engine provides more facilities - such as handling a
window-resize event, and adding a menu bar.
In VID we can define a window like this:
view [
size 350x450 ;-- window size
title "Text faces" ;-- window title
t1: text "Hello" ;-- rest of VID code
;-- etc
]
Its official View documentation is at:
https://doc.red-lang.org/en/view.html#_window
top alphanumeric-index category-index
Evaluates a block of Red code bound to the currently defined face. Allows
directly setting the face fields, overriding other VID options.
Note that the with is executed as the face is created. In the following,
if we reversed the order of b1 and b2, creating b2 first, an error would
result, because b1 does not exist (yet).
view [
b1: button "Hello" ;-- actually shows "HI!"
b2: button "Goodbye" with [b1/text: "HI!"]
]
top alphanumeric-index category-index
Look in the official VID documents:
https://doc.red-lang.org/en/vid.html
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
The VID dialect provides named colors. Each color has a tuple! value.
For example, here we create a brown text element:
view [
text 100x50 brown " "
]
top alphanumeric-index category-index
The VID dialect provides named colors. Each color has a tuple! value.
For example, here we create a brown text element:
view [
text 100x50 brown " "
]
top alphanumeric-index category-index
Consult the Parse dialect page for information.
top alphanumeric-index category-index
Consult the Parse dialect page for information.
top alphanumeric-index category-index
Consult the Draw pages for information.
top alphanumeric-index category-index
Consult the Draw pages for information.
top alphanumeric-index category-index
Consult the Draw pages for information.
top alphanumeric-index category-index
Consult the Draw pages for information.
top alphanumeric-index category-index
The infix word < loosely compares operands on its left and right side andExamples
returns true if the operand on the left is smaller than the operand on the right.
Otherwise returns false.
Has 2 operands of any datatype!.
Loose comparison means that:
- the case of strings is ignored when comparing
- same numeric values for different datatypes are considered equal
2 < 3
== true
2 < 2.0
== false
22-03-2000 < 14-04-2012
== true
"ABC" < "abc"
== false
"abc" < "ABC"
== false
"abc" = "ABC"
== true
top alphanumeric-index category-index
The word << shifts its first argument (an integer!) to the leftExamples
by the number of bits in its second argument (also an integer!).
The sign is taken into account.
red>> to-hex 6
== #00000006
red>> to-hex 6 << 1
== #0000000C
red>> to-hex 6 << 2
== #00000018
red>> to-hex 6 << 3
== #00000030
red>> to-hex 6 << 4
== #00000060
red>> to-hex -6
== #FFFFFFFA
red>> to-hex -6 << 1
== #FFFFFFF4
red>> to-hex -6 << 2
== #FFFFFFE8
red>> to-hex -6 << 3
== #FFFFFFD0
red>> to-hex -6 << 4
== #FFFFFFA0
top alphanumeric-index category-index
The infix word <= loosely compares operands on its left and right side andExamples
returns true if the operand on the left is smaller than or equal to the operand
on the right. Otherwise returns false.
Has 2 operands of any datatype!.
Loose comparison means that:
- the case of strings is ignored when comparing
- same numeric values for different datatypes are considered equal
2 <= 3
== true
2 <= 2.0
== true
22-03-2000 <= 14-04-2012
== true
"ABC" <= "abc"
== true
"abc" <= "ABC"
== true
top alphanumeric-index category-index
The infix word <> compares 2 values, and returns true if they areExamples
NOT equal, otherwise false.
The 2 values need not be of the same datatype!.
It is equivalent to the not-equal? function.
Note that string! values which only differ in case are considered equal.
Red also has a strict-equal? function, where the types must be the same,
and string! case is checked.
red>> a: 33
== 33
red>> a <> 35
== true
red>> b: [1 2 3]
== [1 2 3]
red>> b <> [1 2 3]
== false
red>> 12 <> 12.0
== false
red>> "abc" <> "abc"
== false
red>> "abC" <> "ABC"
== false
; Note uppercase considered equal to lowercase!
top alphanumeric-index category-index
The infix word = loosely compares operands on its left and right side andExamples
returns true if the operand on the left is loosely equal to the operand on the
right. Otherwise returns false.
Has 2 operands of any datatype!.
Loose comparison means that:
- the case of strings is ignored when comparing
- same numeric values for different datatypes are considered equal
2 = 3
== false
2 = 2.0
== true
22-03-2000 = 14-04-2012
== false
"ABC" = "abc"
== true
top alphanumeric-index category-index
The infix word == strictly compares operands on its left and right side andExamples
returns true if the operand on the left is strictly equal to the operand on the
right. Otherwise returns false.
Has 2 operands of any datatype!.
The comparison is "strict", which means that:
- numeric operands are not equal if they have a different datatype
- string operands are not equal if their case differs
2 == 3
== false
2 == 2.0
== false
22-03-2000 == 14-04-2012
== false
"ABC" == "abc"
== false
top alphanumeric-index category-index
The infix word =? returns true if two values have the same identity.Examples
As regards series! (including string!s) the references (pointers) to
the series! are compared.
red>> a: 22
== 22
red>> b: 22
== 22
red>> a =? b
== true
red>> c: [1 2 3]
== [1 2 3]
red>> d: c
== [1 2 3]
red>> c =? d
== true
; Pointers are compared - they indeed reference the same series
red>> c: [3 2 1]
== [3 2 1]
red>> d: [3 2 1]
== [3 2 1]
red>> c = d
== true
; Values are compared - are equal
red>> c =? d
== false
; Pointers are compared - not referencing the same series
top alphanumeric-index category-index
The infix word > loosely compares operands on its left and right sideExamples
returns true if the operand on the left is greater than the operand on
the right. Otherwise returns false.
Has 2 operands of any datatype
Loose comparison means that:
- the case of strings is ignored when comparing
- same numeric values for different datatypes are considered equal
Examples
2 > 3
== false
2 > 2.0
== false
22-03-2000 > 14-04-2012
== false
"ABC" > "abc"
== false
"abc" > "ABC"
== false
"abc" = "ABC"
== true
top alphanumeric-index category-index
The infix word >= loosely compares operands on its left and right side andExamples
returns true if the operand on the left is greater than or equal to the operand
on the right. Otherwise returns false.
Has 2 operands of any datatype!.
Loose comparison means that:
- the case of strings is ignored when comparing
- same numeric values for different datatypes are considered equal
2 >= 3
== false
2 >= 2.0
== true
22-03-2000 >= 14-04-2012
== false
"ABC" >= "abc"
== true
"abc" >= "ABC"
== true
top alphanumeric-index category-index
The word >> shifts its first argument (an integer!) to the rightExamples
by the number of bits in its second argument (also an integer!).
The sign is taken into account.
red>> to-hex 6
== #00000006
red>> to-hex 6 >> 1
== #00000003
red>> to-hex 6 >> 2
== #00000001
red>> to-hex 6 >> 3
== #00000000
red>> to-hex 6 >> 4
== #00000000
red>> to-hex -6
== #FFFFFFFA
red>> to-hex -6 >> 1
== #FFFFFFFD
red>> to-hex -6 >> 2
== #FFFFFFFE
red>> to-hex -6 >> 3
== #FFFFFFFF
red>> to-hex -6 >> 4
== #FFFFFFFF
top alphanumeric-index category-index
The word >>> shifts its first argument (an integer!) to the rightExamples
by the number of bits in its second argument (also an integer!).
This is a shift which ignores the sign.
red>> to-hex 6
== #00000006
red>> to-hex 6 >>> 1
== #00000003
red>> to-hex 6 >>> 2
== #00000001
red>> to-hex 6 >>> 3
== #00000000
red>> to-hex 6 >>> 4
== #00000000
red>> to-hex -6
== #FFFFFFFA
red>> to-hex -6 >>> 1
== #7FFFFFFD
red>> to-hex -6 >>> 2
== #3FFFFFFE
red>> to-hex -6 >>> 3
== #1FFFFFFF
red>> to-hex -6 >>> 4
== #0FFFFFFF
top alphanumeric-index category-index
This word is a synonym for helptop alphanumeric-index category-index
The word ?? prints a Red word! (given as its argument)Examples
and the value it refers to, in a molded format. This means that
the output includes type information (e.g. [ ], " ").
It is very useful for debugging.
Warning: when used with a function! name, it displays the
function! specification, rather than evaluating
that function!.
red>> age: 35
== 35
red>> ?? age
age: 35
== 35
red>> older: 1 + ?? age
age: 35
== 36
red>> ?? 44
*** Script error: ?? does not allow integer for its value argument
*** Where: ??
red>> ?? sin 0.4
sin: routine ["Returns the trigonometric sine" angle [float!]
"Angle in radians"][natives/sine* no 1]
== 0.4
; Effectively the 0.4 here is a stand-alone value;
; ?? does not do anything with it!
top alphanumeric-index category-index
USAGE:
A-AN str
DESCRIPTION:
Returns the appropriate variant of a or an (simple, vs 100% grammatically correct).
A-AN is a function! value.
ARGUMENTS:
str [string!]
REFINEMENTS:
/pre => Prepend to str.
; -------------------------------------------------------------------------
; a-an
; -------------------------------------------------------------------------
>> a-an "apple"
== "an"
>> a-an "chair"
== "a"
top alphanumeric-index category-index
Displays the version of Red currently in useExamples
Has no parameters
about
Red 0.5.4 - 1-Dec-2015/11:46:16+1:00
top alphanumeric-index category-index
The absolute word evaluates an expression or a valueExamples
and returns the absolute value if the datatype is appropriate.
Has 1 parameter, an expression or a value
absolute 1 - 3
== 2
; 1 - 3 == -2
; Returns the absolute value of -2, which is 2
absolute -2 / 5
== 0.4
; -2 / 5 == -0.4
; Returns the absolute value of -0.4, which is 0.4
absolute 6 / 3
== 2
; 6 / 3 == 2
; Returns the absolute value of 2, which is 2
absolute -2x5
== 2x5
top alphanumeric-index category-index
This word is a synonym for arccosinetop alphanumeric-index category-index
The datatype! action! encompassess a specific set of native!Examples
functions.
; -------------------------------------------------------------------------
; List all functionss that are an action
; -------------------------------------------------------------------------
red>> ? action!
absolute => Returns the non-negative value
add => Returns the sum of the two values
and~ => Returns the first value ANDed with the second
append => Inserts value(s) at series tail; returns series head
at => Returns a series at a given index
back => Returns a series at the previous index
change => Changes a value in a series and returns the series after
the change.
clear => Removes series values from current index to tail; returns
new tail
complement => Returns the opposite (complementing) value of the input `
value
copy => Returns a copy of a non-scalar value
divide => Returns the quotient of two values
even? => Returns true if the number is evenly divisible by 2
find => Returns the series where a value is found, or NONE
form => Returns a user-friendly string representation of a value
head => Returns a series at its first index
head? => Returns true if a series is at its first index
index? => Returns the current index of series relative to the head,
or of word in a context
insert => Inserts value(s) at series index; returns series past the
insertion
length? => Returns the number of values in the series, from the current
index to the tail
make => Returns a new value made from a spec for that value's type
modify => Change mode for target aggregate value
mold => Returns a source format string representation of a value
move => Moves one or more elements from one series to another
position or series
multiply => Returns the product of two values
negate => Returns the opposite (additive inverse) value
next => Returns a series at the next index
odd? => Returns true if the number has a remainder of 1 when divided
by 2
or~ => Returns the first value ORed with the second
pick => Returns the series value at a given index
poke => Replaces the series value at a given index, and returns
the new value
power => Returns a number raised to a given power (exponent)
put => Replaces the value following a key, and returns the new value
random => Returns a random value of the same datatype; or shuffles series
read => Read from a file, URL, or other port
reflect => Returns internal details about a value via reflection
remainder => Returns what is left over when one value is divided by another
remove => Returns the series at the same index after removing a value
reverse => Reverses the order of elements; returns at same position
round => Returns the nearest integer. Halves round up (away from zero)
by default
select => Find a value in a series and return the next value, or NONE
skip => Returns the series relative to the current index
sort => Sorts a series (modified); default sort order is ascending
subtract => Returns the difference between two values
swap => Swaps elements between two series or the same series
tail => Returns a series at the index after its last value
tail? => Returns true if a series is past its last value
take => Removes and returns one or more elements
to => Converts to a specified datatype
trim => Removes space from a string or NONE from a block or object
write => Writes to a file, URL, or other port
xor~ => Returns the first value exclusive ORed with the second
; -------------------------------------------------------------------------
; Check if a word is an action!
; -------------------------------------------------------------------------
red>> action? :take ; Colon is mandatory to get to function definition
== true
top alphanumeric-index category-index
The word action? returns true if its argument is an action!Examples
type, or false otherwise.
red>> action? :take ; Colon is mandatory to get to function definition
== true
top alphanumeric-index category-index
The word add adds 2 values together.Examples
This is equivalent to the infix operator +.
Has 2 operands, which can be expressions or values
; -------------------------------------------------------------------------
; Add some number!s
; -------------------------------------------------------------------------
add -1 3
== 2
add 4 / 2 5
== 7
; 4 / 2 == 2
; 2 + 5 == 7
; Returns 7
add 2.3 7.77 - 2
== 8.07
; 7.77 - 2 == 5,77
; 2.3 + 5.77 == 8.07
; Returns 8.07
; -------------------------------------------------------------------------
; Add a number! to a pair! and vv
; -------------------------------------------------------------------------
red>> add 2x4 5
== 7x9
red>> add 5 2x4
== 7x9
top alphanumeric-index category-index
docstartExamples
USAGE:
ALERT msg
DESCRIPTION:
ALERT is a function! value.
ARGUMENTS:
msg [string! block!]
.docend
Displays a pop-up notification box, with a message and an OK button.
The program waits, and continues when OK is clicked, or the 'enter' key is pressed.
alert "Hello"
alert ["Year is " 2022]
top alphanumeric-index category-index
The word all evaluates each expression in a block in turnExamples
and either returns the last resulting value (if that value is
not falsey) or returns none if it encounters a falsey
resulting value.
Has 1 parameter, a block.
The resulting value of all can be used in the if or either words, where all
functions like a short-circuit and.
all [1 + 1 2 + 2 3 = 3 2 + 6]
== 8
; 1 + 1 == 2 (not falsey, so evaluation continues)
; 2 + 2 == 4 (not falsey, so evaluation continues)
; 3 = 3 == true (not falsey, so evaluation continues)
; 2 + 6 == 8
; Returns the last resulting value (8) because no falsey values detected
all [1 + 1 3 = 4 2 + 2 "John"]
== none
; 1 + 1 == 2 (which is not falsey, so evaluation continues)
; 3 = 4 == false
; Returns none immediately, because a falsey value (false) is detected
top alphanumeric-index category-index
all-word! is a typeset! value:
make typeset! [word! set-word! lit-word! get-word! refinement! issue!]
top alphanumeric-index category-index
all-word? returns true if its argument is any all-word! type.
It has no refinements.
Examples
; -------------------------------------------------------------------------
; Try block, word, and /refinement
; -------------------------------------------------------------------------
>> all-word? [1 2]
== false
>> all-word? 'find
== true
>> all-word? first [find]
== true
>> all-word? /ref
== true
top alphanumeric-index category-index
The word also first evaluates its first argument and thenExamples
also evaluates its second argument.
red>> a: 4
== 4
red>> b: 5
== 5
red>> also c: a + b c: c * a
== 9 ; Returns result of first expression
red>> c
== 36 ; Contains result of last expression
top alphanumeric-index category-index
The word alter appends/removes a value to/from a series!.Examples
The value can be of any type.
If the value does NOT exist in the series! it is appended, and true is returned.
The original series! is modified.
If the value does exist, it is removed, and false is returned.
Note that find can be used to search a value in a series!.
There are no refinements.
red>> a: ["cat" "dog"]
== ["cat" "dog"]
red>> alter a "fish"
== true
red>> a
== ["cat" "dog" "fish"]
red>> alter a "fish"
== false
red>> a
== ["cat" "dog"]
red>> find a "dog"
== ["dog"]
red>> find a "fish"
== none
top alphanumeric-index category-index
The infix word and performs a logical "and" of two values.Examples
The values are restricted to these types:
logic! integer! char! bitset! typeset! pair! tuple! vector!
It is the infix version of the and~ word.
In the case of anding two integer!s (or types closely related to integer!s,
such as char!, pair!), a bitwise (bit-by-bit) process is performed.
In general, types can be mixed, but logic! types cannot be mixed with other types.
The type of the result is the same type as the first value for a bitwise and.
For a logic and true or false are returned.
There are no refinements.
; -------------------------------------------------------------------------
; And some number!s
; -------------------------------------------------------------------------
red>> (3 > 2) and true
== true
red>> 8 and 16
== 0
red>> 16 and false
*** Script error: logic type is not allowed here
*** Where: and
; -------------------------------------------------------------------------
; And pair!s
; -------------------------------------------------------------------------
red>> my-pair: 3x4
== 3x4
red>> my-pair and 1
== 1x0
red>> 1 and my-pair
*** Script error: and does not allow pair for its value2 argument
*** Where: and
; -------------------------------------------------------------------------
; And vector!s
; -------------------------------------------------------------------------
red>> v1: make vector! [1 2 3]
== make vector! [1 2 3]
red>> v2: make vector! [1 1 1]
== make vector! [1 1 1]
red>> v1 and v2
== make vector! [1 0 1]
top alphanumeric-index category-index
The and~ word performs a logical "and" of its 2 arguments.Examples
The arguments are restricted to these types:
logic! integer! char! bitset! typeset! pair! tuple! vector!
It is the functional version of the infix and word.
In the case of and~ing two integer!s (or types closely related to integer!s,
such as char!, pair!), a bitwise (bit-by-bit) process is performed.
In general, types can be mixed, but logic! types cannot be mixed with other types.
The type of the result is the same type as the first value for a bitwise and~.
For a logic and~ true or false are returned.
There are no refinements.
; -------------------------------------------------------------------------
; Using number!s
; -------------------------------------------------------------------------
red>> and~ (3 > 2 ) true
== true
red>> and~ 8 16
== 0
; -------------------------------------------------------------------------
; Using bad parameter
; -------------------------------------------------------------------------
red>> and~ 16 false
*** Script error: logic type is not allowed here
*** Where: and~
; -------------------------------------------------------------------------
; Using pair!
; -------------------------------------------------------------------------
red>> my-pair: 3x4
== 3x4
red>> and~ my-pair 1
== 1x0
red>> and~ 1 my-pair
*** Script error: and~ does not allow pair for its value2 argument
*** Where: and~
; -------------------------------------------------------------------------
; Using vector!
; -------------------------------------------------------------------------
red>> v1: make vector![1 2 3]
== make vector! [1 2 3]
red>> v2: make vector![1 1 1]
== make vector! [1 1 1]
red>> and~ v1 v2
== make vector! [1 0 1]
top alphanumeric-index category-index
The word any evaluates each expression in a block! in turn and returnsExamples
the first resulting value that is not falsey.
If all resulting values are falsey it returns none.
Has 1 parameter, a block!
The resulting value of any can be used in the if or either words,
where any functions like a short-circuit or.
any [none false 1 false]
== 1
; none -> none (is falsey, so evaluation continues)
; false -> false (is falsey, so evaluation continues)
; 1 == 1
; Returns 1 immediately because a non falsey value (1) is detected
any [2 = 3 4 = 5 2 + 2 = 4 5 - 3 = 1]
== true
; 2 = 3 == false (is falsey, so evaluation continues)
; 4 = 5 == false (is falsey, so evaluation continues)
; 2 + 2 = 4 == true
; Returns true immediately because a non falsey value (true) is detected
top alphanumeric-index category-index
ANY-BLOCK! is a typeset! value: make typeset! [block! paren! path! lit-path!
set-path! get-path! hash!]
top alphanumeric-index category-index
USAGE:
ANY-BLOCK? value
DESCRIPTION:
Returns true if the value is any type of any-block.
ANY-BLOCK? is a function! value.
ARGUMENTS:
value [any-type!]
; -------------------------------------------------------------------------
; any-block? Examples
; -------------------------------------------------------------------------
>> any-block? [1 2]
== true
>> any-block? "234"
== false
>> any-block? first [ (1 2 3)]
== true
top alphanumeric-index category-index
ANY-FUNCTION! is a typeset! value: make typeset! [native! action! op! function! routine!]
top alphanumeric-index category-index
The word any-function? returns true if its argument is a any-function!.Examples
It has no refinements.
red>> any-function? a
== false
red>> any-function? :find
== true
; The colon (:) gets the definition, not the value of a function
top alphanumeric-index category-index
The word any-list! is a typeset! of value: [block! paren! hash!].Examples
We can test if any item is an any-list! with the any-list? function
red>> any-list!
== make typeset! [block! paren! hash!]
red>> a: [1 2 3]
== [1 2 3]
red>> any-list? a
== true
red>> any-list? 3
== false
top alphanumeric-index category-index
The word any-list? returns true if its argument is an any-list! type,Examples
otherwise false.
An any-list! is a typeset! of:
block! paren! hash!
Its argument can be of any type.
; -------------------------------------------------------------------------
; try some types with any-list?
; -------------------------------------------------------------------------
red>> any-list? "text"
false
red>> any-list? 127.33.63
== false
red>> any-list? [22 33]
== true
red>> any-list? (2 3)
== false
red>> any-list? (2 3) ; invalid - Red tries to evaluate the argument
== false
red>> first [(2 3)] ; valid - [ ] prevents evaluation
== (2 3)
red>> any-list? first [(2 3)] ; now it works!
== true
top alphanumeric-index category-index
A typeset!
make typeset! [object! error!]
See any-object?
top alphanumeric-index category-index
The word any-object? returns true if its argument is an object!.Examples
It has no refinements.
red>> my-obj: make object! [ x: 10 y: 10 ]
== make object! [
x: 10
y: 10
]
red>> any-object? my-obj
== true
red>> a: 22
red>> any-object? a
== false
top alphanumeric-index category-index
Is a typeset!
make typeset! [path! lit-path! set-path! get-path!]
See any-path?
top alphanumeric-index category-index
The word any-path? returns true if its argument is a valid path! variant.Examples
Otherwise false.
In Red, there are several types of path! like:
path! lit-path! set-path! get-path!
For more details on the ways of interpreting a word (involving ' and :),
the reader should look at the lit-word!, get-word! and set-word! docs.
; -------------------------------------------------------------------------
; Standard paths. Note: we use a [block] to prevent evaluation
; -------------------------------------------------------------------------
red>> any-path? first [a/b/c]
== true
red>> path? first [a/b/c]
== true
red>> type? first [a/b/c]
== path!
red>> any-path? first [a / b] ; Surprise - it is a division!
== false
; -------------------------------------------------------------------------
; Set paths.
; -------------------------------------------------------------------------
red>> any-path? first[a/b:]
== true
red>> set-path? first[a/b:]
== true
red>> type? first[a/b:]
== set-path!
red>> type? first[a:/b/c] ; Surprise - because of colon in 2nd position!
== url!
red>> any-path? first [a:/b/c]
== false
; -------------------------------------------------------------------------
; Get paths.
; -------------------------------------------------------------------------
red>> any-path? first [:a/b/c]
== true
red>> get-path? first [:a/b/c]
== true
red>> type? first [:a/b/c]
== get-path!
; -------------------------------------------------------------------------
; Lit paths.
; -------------------------------------------------------------------------
red>> any-path? first ['a/b/c]
== true
red>> lit-path? first ['a/b/c]
== true
red>> type? first ['a/b/c]
== lit-path!
top alphanumeric-index category-index
Is a typeset!
make typeset! [string! file! url! tag! email!]
See any-string?
top alphanumeric-index category-index
The word any-string? returns true if its argument is any type of string!,Examples
including the types string!, file! and url!.
It has no refinements.
red>> any-string? http://www.aaa.com
== true
red>> any-string? "fred"
== true
red>> any-string? 123
== false
red>> any-string? %notes/data.txt
== true
top alphanumeric-index category-index
top alphanumeric-index category-index
any-word! is a typeset! value:
make typeset! [word! set-word! lit-word! get-word!]
top alphanumeric-index category-index
The word any-word? returns true if its argument can be any-word!.Examples
It has no refinements.
red>> any-word? 123
== false
; 123 is a number, not a symbol
red>> any-word? find
*** Script error: find is missing its series argument
*** Where: find
; Red tries to invoke the function find but fails on missing arguments
red>> any-word? 'find
== true
; Red takes quoted symbols literally
red>> any-word? :find
== false
; A function definition is not a word
red>> a: 'find
== find
red>> any-word? a
== true
top alphanumeric-index category-index
The word append inserts value(s) at the end of a series!Examples
and returns the head of the series. The original series is changed.
Arguments:
1. the series to be extended can be any series a bitmap! or a map!
2. the value to be appended can be any-type!
Refinements
/part - limit the number of values inserted. (a number! or a series index)
/only - insert block types as single values (overrides /part).
/dup - duplicate the inserted values. Provide a number!
; -------------------------------------------------------------------------
; Append to a block!
; -------------------------------------------------------------------------
red>> a-block: [1 2 3 "four" 5]
== [1 2 3 "four" 5]
red>> append a-block 6
== [1 2 3 "four" 5 6]
red>> append a-block [7 8 "nine" [10 11]]
== [1 2 3 "four" 5 6 7 8 "nine" [10 11]]
; -------------------------------------------------------------------------
; Append to a string!
; -------------------------------------------------------------------------
red>> a-string: "ABCD"
== "ABCD"
red>> append a-string "123"
== "ABCD123"
red>> append a-string [6 5 4]
== "ABCD123654" ; see what happens here?
; -------------------------------------------------------------------------
; /part refinement
; -------------------------------------------------------------------------
red>> series-1: [1 2 3 4 5 6]
== [1 2 3 4 5 6]
red>> series-2: [100 200 300 400 500]
== [100 200 300 400 500]
red>> append/part series-1 series-2 3
== [1 2 3 4 5 6 100 200 300] ; only 3 items appended
; -------------------------------------------------------------------------
; /only refinement
; -------------------------------------------------------------------------
red>> series-1: [1 2 3 4 5 6]
== [1 2 3 4 5 6]
red>> series-2: [100 200 300 400 500]
== [100 200 300 400 500]
red>> append/only series-1 series-2
== [1 2 3 4 5 6 [100 200 300 400 500]] ; appended as a comnplete block
; -------------------------------------------------------------------------
; /dup refinement - note duplicated values at end
; -------------------------------------------------------------------------
red>> series-1: [1 2 3 4 5 6]
== [1 2 3 4 5 6]
red>> series-2: [100 200]
== [100 200]
red>> append/dup series-1 series-2 3
== [1 2 3 4 5 6 100 200 100 200 100 200]
top alphanumeric-index category-index
The word arccosine returns the trigonometric arccosine (in degrees by default).Examples
Has 1 parameter, a number!.
Refinements:
/radians : returns the angle in radians;
without refinement returns the angle in degrees.
The resulting numeric angle value of arccosine can be used in an expression.
arccosine 0.5
== 60.0
arccosine/radians 1
== 0.0
top alphanumeric-index category-index
The word arcsine returns the trigonometric arcsine (in degrees by default).Examples
Has 1 parameter, a number!.
Refinements:
/radians : returns the angle in radians;
without refinement returns the angle in degrees.
arcsine 0.5
== 30.0
arcsine/radians 0
== 0.0
top alphanumeric-index category-index
The word arctangent returns the trigonometric arctangent (in degrees by default).Examples
Has 1 parameter, a number!.
Refinements:
/radians : returns the angle in radians;
without refinement returns the angle in degrees.
arctangent 0
== 0.0
arctangent/radians 0
== 0.0
top alphanumeric-index category-index
USAGE:
ARCTANGENT2 y x
DESCRIPTION:
Returns the smallest angle between the X axis and the point (x,y) (-pi to pi).
ARCTANGENT2 is a native! value.
ARGUMENTS:
y [number!]
x [number!]
RETURNS:
[float!]
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
USAGE:
AS type spec
DESCRIPTION:
Coerce a series into a compatible datatype without copying it.
AS is a native! value.
ARGUMENTS:
type [datatype! block! paren! any-path! any-string!] "The datatype
or example value."
spec [block! paren! any-path! any-string!] "The series to coerce."
; -------------------------------------------------------------------------
; as examples
; -------------------------------------------------------------------------
>> b: [1 2 3]
== [1 2 3]
>> p: as paren! b
== (1 2 3)
>> b
== [1 2 3] ;-- nb unchanged
top alphanumeric-index category-index
The word as-color generates a tuple! with R, G and B valuesExamples
from its 3 integer! arguments.
red>> as-color 255 0 0
== 255.0.0
; -------------------------------------------------------------------------
; Predefined colors (e.g. red green etc.) are available also
; -------------------------------------------------------------------------
red>> red
== 255.0.0
red>> green
== 0.255.0
red>> yellow
== 255.255.0
top alphanumeric-index category-index
The word as-ipv4 returns an IP version 4 address created fromExamples
its 4 integer! arguments.
If an argument is not within the range 0 .. 255, Red tries to
"guess" an appropriate value using the modulo of its (absolute) value.
red>> as-ipv4 1 3 12 253
== 1.3.12.253
red>> as-ipv4 1 3 12 0
== 1.3.12.0
red>> as-ipv4 1 3 12 255
== 1.3.12.255
red>> as-ipv4 1 3 12 256
== 1.3.12.0
red>> as-ipv4 1 3 12 257
== 1.3.12.1
red>> as-ipv4 1 3 12 258
== 1.3.12.2
red>> as-ipv4 0 3 12 255
== 0.3.12.255
red>> as-ipv4 -1 3 12 255
== 255.255.255.255
red>> as-ipv4 -2 3 12 255
== 254.255.255.255
red>> as-ipv4 -3 3 12 255
== 253.255.255.255
top alphanumeric-index category-index
The word as-pair combines 2 integer! or float! values into a pair!.Examples
; -------------------------------------------------------------------------
; Demonstrate mixed integers and floats, also truncation
; -------------------------------------------------------------------------
red>> as-pair 22 33
22x33
red>> as-pair 22 33.9
22x33
red>> as-pair 44.3 44.99
44x44
top alphanumeric-index category-index
USAGE:
AS-RGBA a b c d
DESCRIPTION:
Combine a, b, c and d values into a tuple.
AS-RGBA is a routine! value.
ARGUMENTS:
a [integer!]
b [integer!]
c [integer!]
d [integer!]
; -------------------------------------------------------------------------
; as-rgba
; -------------------------------------------------------------------------
>> as-rgba 255 0 255 1
== 255.0.255.1
top alphanumeric-index category-index
This word is a synonym for arcsinetop alphanumeric-index category-index
Displays a prompt and gets the value that the user types at STDIN.Examples
Has 1 parameter, a string!
The resulting string value of ask can be used in an expression
answer: ask "What is your name?"
What is your name?Carl
== "Carl"
answer
== "Carl"
top alphanumeric-index category-index
The at word returns a series! at a given integer index.Examples
Note that it does not extract one item. Rather, it returns a reference (pointer)
into the original series!.
The original series! is unchanged.
Note that series! (which includes string!s) are indexed from 1.
If the index is off the 'left-hand side' of the series!, the whole series!
is returned.
If the series! is beyond the right-hand side, an empty series! is returned.
There are no refinements.
red>> at [1 2 3 4 5] 3
== [3 4 5]
red>> at "abcde" 3
== "cde"
red>> at "abcd" 6
== ""
red>> at [1 2 3 4] -2
== [1 2 3 4]
red>> at [1 2 3 4] 0
== [1 2 3 4]
top alphanumeric-index category-index
This word is a synonym for arctangenttop alphanumeric-index category-index
This word is a synonym for arctangent2top alphanumeric-index category-index
The word attempt tries to evaluate a block!Examples
and returns the result or none if an error occurs.
The effect of attempt is similar to that of error? try[block].
If you need more details about specific errors, look at try,
cause-error error?, and the error! type.
red>> s: "abcd"
== "abcd"
red>> calc: 3 * s
*** Script error: * does not allow string for its value2 argument
*** Where: *
red>> calc: attempt [3 * s]
== none
red>> calc
== none
red>> calc: attempt [3 * 2.2]
== 6.6
red>> calc
== 6.6
top alphanumeric-index category-index
USAGE:
AVERAGE block
DESCRIPTION:
Returns the average of all values in a block.
AVERAGE is a function! value.
ARGUMENTS:
block [block! vector! paren! hash!]
; -------------------------------------------------------------------------
; avergag
; -------------------------------------------------------------------------
>> average [7 8]
== 7.5
>> type? average [6 8] ;-- note type of result
== float!
top alphanumeric-index category-index
The word back moves the index of a series! 1 position towardsExamples
the head (= the start) of the series!.
When a series! index is already at head position, back won't
change the index; it stays at head.
The opposite of back is next.
red>> ser: [1 2 3]
== [1 2 3]
red>> head? ser
== true
red>> index? ser
== 1
red>> back ser
== [1 2 3]
red>> ser: back ser
== [1 2 3]
red>> head? ser
== true
red>> index? ser
== 1
red>> ser: tail ser
== []
red>> index? ser
== 4
red>> ser: back ser
== [3]
red>> index? ser
== 3
red>> ser: back ser
== [2 3]
red>> index? ser
== 2
red>> ser: back ser
== [1 2 3]
red>> index? ser
== 1
red>> head? ser
== true
top alphanumeric-index category-index
The binary! datatype! is a series!. Each element is a 0-255Examples
integer value. Literal binary values can be expressed with bases
2, 16, 64. Base 16 is the default.
Because of the byte-based representation, we must supply sufficient digits
to produce 8-bit units. Thus for base 16, we must provide 2, 4, 6 etc
digits, and for base 64, we must supply 4, 8, 12 etc digits. (4 base-64
digits provide 24 bits, which splits into 3 bytes exactly).
The arithmetic functions do not allow binary! arguments.
Literals can be written with white space for readability, and upper or
lower-case letters can be used.
Readers of Rebol documentation should note that Red is closer to Rebol3 in
this area. There are major differences from Rebol2.
; -------------------------------------------------------------------------
; Some literals
; -------------------------------------------------------------------------
;base 16 - hex
red>> #{0f}
== #{0F}
;base 2, with spaces
>> 2#{11110000 11110000}
== #{F0F0}
;base 64
>> 64#{ffff}
== #{7DF7DF}
;incorrect number of digits here
red>> #{fff}
*** Syntax Error: invalid binary! at "#{fff}"
*** Where: do
; -------------------------------------------------------------------------
; Use to binary!
; -------------------------------------------------------------------------
;convert a string
red>> to binary! "1111ffff"
== #{3131313166666666}
; -------------------------------------------------------------------------
; Use pick to get an integer element (FF)
; -------------------------------------------------------------------------
red>> b: #{00
ff
22
}
== #{00FF22}
red>> pick b 2
== 255
top alphanumeric-index category-index
The word binary? returns true if its argument is of binary! type.Examples
A literal binary series! is surrounded by #{...}
red>> binary? #{22}
== true
red>> binary? #{ 22 FF AA }
== true
red>> binary? 16
== false
top alphanumeric-index category-index
USAGE:
BIND word context
DESCRIPTION:
Bind words to a context; returns rebound words.
BIND is a native! value.
ARGUMENTS:
word [block! any-word!]
context [any-word! any-object! function!]
REFINEMENTS:
/copy => Deep copy blocks before binding.
RETURNS:
[block! any-word!]
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
The bitset! datatype provides an array of bits that is used to storeExamples
boolean values. It is often used with Parse, letting us represent
arbitrary sets of characters across the whole Unicode range, that can be
matched against an input character in a single operation.
In order to create a bitset, you need to provide one or several characters
as a base specification. They can be provided in different forms: codepoint
integer! values, char! values, string! values, a range, or a
group of previous elements.
The bitset! indexing starts at zero. It is not a series! type.
Bitsets expand at the right as needed, to provide enough storage for additions.
A charset! shortcut function is provided for convenience, and is shown
below.
We create a new bitset! with make, and its argument must be one of:
char!, integer!, string! or block!
; -------------------------------------------------------------------------
; Create some bitsets
; -------------------------------------------------------------------------
; an empty bitset with places at least for 16 bits
;note the 4 hex digits displayed
red>> b-demo: make bitset! 16
== make bitset! #{0000}
; a bitset - space for at least 17 bits. Size is rounded to upper byte bound.
red>> b-demo2: make bitset! 17
== make bitset! #{000000}
; create a bitset with bit 65 set
red>> b-demo: make bitset! #"A" (16 x 0000 bits, then a 0100)
== make bitset! #{000000000000000040}
; create a bitset with bits 104 and 105 set
red>> a-set: make bitset! "hi"
== make bitset! #{00000000000000000000000000C0}
; create and set bits using different values, representations
red>> make bitset! [120 "hello" #"A"]
== make bitset! #{00000000000000004000000004890080}
; create a bitset using ranges of values
red>> bits-09-az: make bitset! [#"0" - #"9" #"a" - #"z"]
== make bitset! #{000000000000FFC0000000007FFFFFE0}
; Ranges are defined using two values (char! or integer! allowed)
; we them separate by a dash word.
; -------------------------------------------------------------------------
; A shortcut charset function is also provided for practical usage,
; -------------------------------------------------------------------------
; so you can write:
red>> hex-set: charset "ABCDEF"
== make bitset! #{00000000000000007E}
red>> set-demo: charset [120 "hello" #"A"]
== make bitset! #{00000000000000004000000004890080}
red>> hyphen-demo: charset [120 "hello" #"A"]
== make bitset! #{00000000000000004000000004890080}
; -------------------------------------------------------------------------
; Auto-sizing. We start with "A", and add a "Z" at the right
; -------------------------------------------------------------------------
red>> b-set: make bitset! "A"
== make bitset! #{000000000000000040}
red>> append b-set "Z"
== make bitset! #{000000000000000040000020}
; now we append a "-". Note that it goes in its proper place,
; not at the rightmost end
red>> append b-set "-"
== make bitset! #{000000000004000040000020}
; note that append modifies the original bitset
; -------------------------------------------------------------------------
; Reading and writing single bits. The path notation can be used
; -------------------------------------------------------------------------
; make a bitset
red>> a-set: make bitset! "ABCDEFG"
== make bitset! #{00000000000000007F}
;do some work on the "A" entry
red>> position: to integer! #"A"
== 65
;look for "A" - true, it is there? Yes.
red>> a-set/:position
== true
;remove it
red>> a-set/:position: false
== false
; look at the set, to prove it
red>> a-set
== make bitset! #{00000000000000003F}
;remove it (no difference, it was not there anyway)
red>> remove/part a-set position
== make bitset! #{00000000000000003F}
; now remove "B", leaving us with CDEFG - 5 bits, in the 1F below
red>> remove/part a-set #"B"
== make bitset! #{00000000000000001F}
; -------------------------------------------------------------------------
; The set functions can also be used:
; -------------------------------------------------------------------------
; union, difference, unique, intersect
; make 2 bitsets to work with
red>> chars: make bitset! "ABCDEFGH"
== make bitset! #{00000000000000007F80}
red>> more-chars: make bitset! "123ABCDEFGHxyz"
== make bitset! #{00000000000070007F800000000000E0}
; find the difference between them - note that 7F8 is missing in result
red>> difference chars more-chars
== make bitset! #{000000000000700000000000000000E0}
; -------------------------------------------------------------------------
; The complement function inverts every bit
; -------------------------------------------------------------------------
red>> a-set: make bitset! "ABCD"
== make bitset! #{000000000000000078}
red>> opposite: complement a-set
== make bitset! [not #{000000000000000078}]
; The original set is unaltered
red>> a-set
== make bitset! #{000000000000000078}
top alphanumeric-index category-index
The word bitset? returns true if its argument is a bitset! type.Examples
red>> b: make bitset! [1 3 5]
== make bitset! #{54}
red>> n: [2 3 6]
== [2 3 6]
red>> bitset? b
== true
red>> bitset? n
== false
top alphanumeric-index category-index
The datatype! block! represents a series! type.Examples
They are enclosed in square brackets [ ] and can run over many lines.
The adagium code = data fits very well for a block!.
A block! is just a block of data, but blocks can also be used
as part of the code.
; -------------------------------------------------------------------------
; Create a block! with a string!, an integer! and a float! in it.
; -------------------------------------------------------------------------
red>> ["Mike" 55 1.95]
== ["Mike" 55 1.95]
; -------------------------------------------------------------------------
; Use a block! in program code.
; -------------------------------------------------------------------------
red>> if 3 > 2 [print "bigger"]
== bigger
; -------------------------------------------------------------------------
; Code = data
; -------------------------------------------------------------------------
red>> a: 22
== 22
red>> my-block: [4 + a]
== [4 + a] ; Just a data block!
red>> do my-block
== 26 ; Happens to contain valid code!
; -------------------------------------------------------------------------
; Beware of unset! values in a block!
; -------------------------------------------------------------------------
red>> my-data: [name "Li" age 42]
== [name "Li" age 42]
red>> print my-data
*** Script Error: name has no value
*** Where: print
top alphanumeric-index category-index
The word block? returns true if its parameter is aExamples
valid block! or false otherwise.
Has 1 parameter.
block? "test"
== false
block? [test 1 2 3]
== true
top alphanumeric-index category-index
The word body-of shows the body of a function! type.Examples
This word is tightly related with the source word.
red>> x: function [a b] [ a + b ]
== func [a b][a + b]
red>> source x
x: func [a b][a + b]
red>> body-of :x
== [a + b]
red>> source list-dir
list-dir: func [
{Displays a list of files and directories from given folder or current one}
dir [any-type!] "Folder to list"
/col "Forces the display in a given number of columns"
n [integer!] "Number of columns"
/local list max-sz name
][
unless value? 'dir [dir: %.]
unless find [file! word! path!] type?/word :dir [
cause-error 'script 'expect-arg ['list-dir type? :dir 'dir]
]
list: read normalize-dir dir
max-sz: either n [system/console/limit / n - n] [
n: max 1 system/console/limit / 22
22 - n
]
while [not tail? list] [
loop n [
if max-sz <= length? name: list/1 [
name: append copy/part name max-sz - 4 "..."
]
prin tab
prin pad form name max-sz
prin " "
if tail? list: next list [exit]
]
prin lf
]
]
red>> print mold body-of :list-dir
[
unless value? 'dir [dir: %.]
unless find [file! word! path!] type?/word :dir [
cause-error 'script 'expect-arg ['list-dir type? :dir 'dir]
]
list: read normalize-dir dir
max-sz: either n [system/console/limit / n - n] [
n: max 1 system/console/limit / 22
22 - n
]
while [not tail? list] [
loop n [
if max-sz <= length? name: list/1 [
name: append copy/part name max-sz - 4 "..."
]
prin tab
prin pad form name max-sz
prin " "
if tail? list: next list [exit]
]
prin lf
]
]
top alphanumeric-index category-index
The word break breaks out of a loop.Examples
Refinements:
/return : forces the loop function to return the value following.
; -------------------------------------------------------------------------
; Normal example
; -------------------------------------------------------------------------
foreach number [1 2 4 8 16] [
if number > 8 [break]
print number
]
print "loop 1 teminated"
1
2
4
8
loop 1 teminated
; -------------------------------------------------------------------------
; Contreived example
; -------------------------------------------------------------------------
print foreach number [1 2 4 8 16] [
if number > 8 [break/return -999]
print number
]
print "loop 2 teminated"
1
2
4
8
-999
loop 2 teminated
; The foreach loop returns the value -999,
; which in turn is printed by "print"
top alphanumeric-index category-index
USAGE:
BROWSE url
DESCRIPTION:
Open web browser to a URL or file mananger to a local file.
BROWSE is a native! value.
ARGUMENTS:
url [url! file!] The word browse opens a web browser
with the specified URL.
The single argument is a url!.
; -------------------------------------------------------------------------
; Open a web browser pointing to Google. Execution continues
; -------------------------------------------------------------------------
red>> browse http://www.google.com
red>> ; execution continues here
top alphanumeric-index category-index
Examples
The word call executes a shell command to run another process.
The argument is:
A command [string! file!] - A shell command or an executable file.
Refinements
/wait - Runs command and waits for exit.
/show - Force the display of system's shell window (Windows only).
/console - Runs command with I/O redirected to console (CLI console only at present).
/shell - Forces command to be run from shell.
/input - we provide a [string! file! binary!], which will be redirected to stdin.
/output - we provide a [string! file! binary!] which will
receive the redirected stdout from the command. Note that the
output is appended.
If you don't want this, clear the destination(or delete the file, for example).
/error - as /output, but redirects stderr.
(Awaiting additional documentation by red-by-example team.)
; -------------------------------------------------------------------------
; Please note:
; -------------------------------------------------------------------------
; These examples were run on Windows 7. Prints are included,
; to provide confirmation of a call starting and ending.
; File paths that do not begin with a / are relative paths.
; Enclose spaces in " ", or use hex codes
; ( '/' works on GNU/Linux, and Windows)
; -------------------------------------------------------------------------
; Call a non-gui program in current directory. % precedes a file name.
; -------------------------------------------------------------------------
print "The program only displays a message, and is not visible-on-screen"
call %hi-exe.exe
Print "Done 1"
; -------------------------------------------------------------------------
; Call notepad++ editor, with absolute path
; -------------------------------------------------------------------------
print "The notepad++ pops up, and this program continues"
call %"C:/Program Files/notepadPP/notepad++.exe"
print "Done 2"
; -------------------------------------------------------------------------
; Call notepad++ editor, with relative path
; -------------------------------------------------------------------------
call %"../Program Files/notepadPP/notepad++.exe"
print "Done 3"
; -------------------------------------------------------------------------
; Call notepad++, with a command-line argument for it.
; -------------------------------------------------------------------------
call %"C:/Program Files/notepadPP/notepad++.exe myData.txt"
print "Done 4"
; -------------------------------------------------------------------------
; /wait refinement. Call notepad++, wait till user closes it.
; -------------------------------------------------------------------------
print "The notepad++ pops up, and this program continues"
call/wait %"C:/Program Files/notepadPP/notepad++.exe"
print "Done 5"
; -------------------------------------------------------------------------
; /output refinement - redirect output of hi-exe.exe to file hi-out.txt
; -------------------------------------------------------------------------
call/output %hi-exe.exe %hi-out.txt
print "Done 6"
; -------------------------------------------------------------------------
; /output - from a .bat file containing Dos commands (A dir command here)
; -------------------------------------------------------------------------
call/output %bat-demo.bat %dir-out.txt
print "Done 7"
; -------------------------------------------------------------------------
; /output - with the output from a Dos command going to a string
; -------------------------------------------------------------------------
a-string: ""
call/output "dir *.txt" a-string
print ["Dir listing is: " a-string]
; -------------------------------------------------------------------------
; /show - run a command-file. Its window flashes up, then vanishes
; -------------------------------------------------------------------------
call/show %bat-demo.bat
print "Done 8"
top alphanumeric-index category-index
The word case provides a multi-way branch structure.Examples
The block! following the first true condition is evaluated.
The result of a case is the value of the last expression evaluated.
Conditional expressions are supported, rather than the simpler form
in the switch word.
Refinement:
/all : causes the blocks for every true condition to be evaluated.
; -------------------------------------------------------------------------
; case without /all refinement
; -------------------------------------------------------------------------
n: 555
case [
n < 10 [print "small"]
n < 100 [print "medium"]
n < 1000 [print "large"]
true [print "none of these"]
]
large
; -------------------------------------------------------------------------
; case with /all refinement (1)
; -------------------------------------------------------------------------
n: 555
case/all [
n < 10 [print "small"]
n < 100 [print "medium"]
n < 1000 [print "large"]
true [print "none of these"]
]
large
none of these
; -------------------------------------------------------------------------
; case with /all refinement (2)
; -------------------------------------------------------------------------
n: 555
case/all [
n > 10 [print "small"]
n > 100 [print "medium"]
n > 1000 [print "large"]
true [print "none of these"]
]
small
medium
none of these
top alphanumeric-index category-index
Catches a throw from a block and returns its value. Note that catchExamples
and throw are mainly intended for creating new control structures, and
are not the main exception-handling functions. For this, look at attempt,
try, cause-error error?, and error!.
Arguments
The single argument must be a block to evaluate.
Refinements
/name - Catches a named throw. We supply a word or block
of words, which are intended to match a named throw or one of
several named throws.
; -------------------------------------------------------------------------
; This complete example shows various catch/throw possibilities.
; -------------------------------------------------------------------------
;It is rather artificial - we could get the same effect with
;more 'either' instructions.
;The example has a flavour of exception-handling, but in Red
;we should really use 'try', 'error?' etc for proper exception-handling.
;throw and catch are intended for building control-structures
;a function for the examples.
grade-a-mark: function [
"Convert a % mark to a letter grade, with an un-named throw"
mark [integer!]] [
print ""
print ["In grade-a-mark, with " mark]
if mark > 100 [throw "special"] ;value of the throw is a string
print "about to grade the mark (assume always A for now)"
"A"
]
;Example with no catch - commented out ------------------------
; the code: print grade-a-mark 102
;would cause execution to halt, with this error:
; *** Throw Error: no catch for throw: "special"
print ""
print "Examples with catch and throw (un-named)-----------------"
; catches our throw, but NOT arithmetic errors (e.g. zero-divide)
print catch [grade-a-mark 103] ; prints: too big
;now, take some action after catching, using returnedresult
if (catch [grade-a-mark 104]) = "special" [
print "caught a throw: special"
]
print ""
print "Examples with catch and throw using /name --------------"
;a similar function, with 2 named throws
grade-a-mark-named: function [
"Convert a % mark to a letter grade - version 2 - 2 named throws"
mark [integer!]] [
print ""
print ["In grade-a-mark-named, with " mark]
;throws with a 'word
if mark > 100 [throw/name mark 'special-one] ;throw has a name as well as a value
if mark < 0 [throw/name mark 'too-small]
[print "grade-a-mark-named: about to grade the mark (assume always A for now)"
"A"
]
]
print "Catching any throw, displaying returned value"
print catch [grade-a-mark-named 105]
print "Now catching named throws via 'word argument for refinement"
if (catch/name [grade-a-mark-named 106] 'special-one) [
print "caught a throw named 'special-one"
]
if (catch/name [grade-a-mark -3] 'too-small) [
print "caught a throw named 'too-small"
]
print ""
print "Now catching a selection of named throws"
;because the names are in a block, we don't need the '
if (catch/name [grade-a-mark-named -4] [special-one too-small]) [
print "caught either 'special-one or 'too-small"
]
OUTPUT FROM THE ABOVE:
---------------------
Examples with catch and throw using /name --------------
Catching any throw, displaying returned value
In grade-a-mark-named, with 105
105
Now catching named throws via 'word argument for refinement
In grade-a-mark-named, with 106
caught a throw named 'special-one
In grade-a-mark, with -3
about to grade the mark (assume always A for now)
caught a throw named 'too-small
Now catching a selection of named throws
In grade-a-mark-named, with -4
caught either 'special-one or 'too-small
top alphanumeric-index category-index
Causes an immediate error, with the provided values. Note that this isExamples
not a throw, and cause-error does not co-operate with throw and
catch.
For other error/exception-handling words, look at the related entries for
attempt, try, error?, and the error! type.
Arguments
Here we show how to view the pre-defined Red errors, which can be used as
arguments. The 3 arguments are:
Argument 1: err-type - a word!
This is a general category of error. A list of these can be obtained
by typing the following at the Red console:
words-of system/catalog/errors
which currently displays:
[throw note syntax script math access user internal]
Argument 2: err-id - a word!
The specific error id words in a type can be obtained by typing e.g.:
? system/catalog/errors/math
which displays:
`system/catalog/errors/math` is an object! of value:
code integer! 400
type string! Math Error
zero-divide string! attempt to divide by zero
overflow string! math or number overflow
positive string! positive number required
Argument 3: args - a block!
Some errors take no arguments, and we can supply an empty block. The
zero-divide word above is such an error. However, when we look in the
script type, we see around 70 error words, including, for example:
no-value block! [:arg1 "has no value"]
which takes 1 argument. We can supply any required arguments in a block.
; -------------------------------------------------------------------------
; Cause some errors. (Abstract examples, no purpose to the errors)
; -------------------------------------------------------------------------
do-work: function [ ] [
sum: 0
;note the empty block for the 3rd argument:
cause-error 'math 'zero-divide []
]
print do-work
*** Math Error: attempt to divide by zero
*** Where: do
do-work: function [ ] [
sum: 0
;note the 3rd argument - often a word, could be e.g. a string.
cause-error 'script 'no-value ['sum]
]
print do-work
*** Script Error: sum has no value
*** Where: do
top alphanumeric-index category-index
This word is a synonym for change-dirtop alphanumeric-index category-index
The word change changes a value in a series! and returns the series beyondExamples
the change position. The original series is changed.
Arguments:
1. series! positioned at the point to change.
2. new value to insert - any type.
Refinements
/part - limits the amount to change to a given length (number) or position (index)
/only - changes a series as a series.
/dup - duplicates the change a specified number of times
; -------------------------------------------------------------------------
; Changing a string!
; -------------------------------------------------------------------------
red>> s: "abcdef"
== "abcdef"
red>> change s "NEW"
== "def"
red>> s
== "NEWdef"
; -------------------------------------------------------------------------
; Changing a block!
; -------------------------------------------------------------------------
red>> b: [22 33 44 55]
== [22 33 44 55]
red>> change b 11
== [33 44 55]
red>> b
== [11 33 44 55]
red>> b: next head b
== [33 44 55]
red>> change b 10
== [44 55]
red>> b
== [10 44 55]
red>> b: head b
== [11 10 44 55]
; -------------------------------------------------------------------------
; The /part refinement
; -------------------------------------------------------------------------
; -------------------------------------------------------------------------
; The /part refinement, with series as the range. The important thing to
; note here is that the series used for the range MUST refer to the same
; series that is being changed. If it doesn't, CHANGE will throw an error.
; -------------------------------------------------------------------------
red>> s: "abcdef" == "abcdef"
red>> change/part s "ABCD" at s 3 == "cdef"
; the series position limits the length of the part to be replaced
red>> s == "ABCDcdef"
; -------------------------------------------------------------------------
; The /only refinement
; -------------------------------------------------------------------------
red>> s: [11 22 33]
== [11 22 33]
red>> change/only s [1 2]
== [22 33]
red>> s
== [[1 2] 22 33]
; -------------------------------------------------------------------------
; The same example, but without /only
; -------------------------------------------------------------------------
red>> s: [11 22 33]
== [11 22 33]
red>> change s [1 2]
== [33]
red>> s
== [1 2 33]
; -------------------------------------------------------------------------
; The /dup refinement
; -------------------------------------------------------------------------
red>> s: "abcdef"
== "abcdef"
red>> change/dup s "<>" 2
== "ef"
red>> s
== "<><>ef"
top alphanumeric-index category-index
The word change-dir changes the current directory.Examples
The argument can be one of file! word! path!.
The directory to change to can be:
- an absolute path!
- a relative path!
; -------------------------------------------------------------------------
; Using an absolute path
; -------------------------------------------------------------------------
red>> change-dir %/e/src
== %/e/src/
red>> dir
8th/ go/ python/ red/ spiderbasic/
; -------------------------------------------------------------------------
; Using a relative path
; -------------------------------------------------------------------------
red>> change-dir %go
== %/e/src/go/
red>> dir
bin32/ bin64/ pkg/ src/
top alphanumeric-index category-index
The datatype! char! represents a scalar! type containingExamples
a single char!.
A literal looks like:
#"A"
Only a single character is valid. So e.g. #"AZ" is invalid!
A series! of char! is a string!.
Representation of the letter A as a:
char! -> #"A"
string! -> "A"
The caret ^ is the escape character for special codes:
- a numeric value is interpreted as HEX
- null, line, tab, page, esc, back, delete
To use such a code, specify:
#"^(code)"
There are some pre-defined characters:
null #"^@"
newline #"^/"
slash #"/"
dbl-quote #"^""
space #" "
lf #"^/"
tab #"^-"
CR #"^M"
dot #"."
escape #"^["
comma #","
sp #" "
; -------------------------------------------------------------------------
; Some examples
; -------------------------------------------------------------------------
red>> #"A"
== #"A"
red>> print #"A"
A
red>> print #"^(41)" ; Hex 41 is decimal 65 is ASCII A
A
red>> first "dog" ; A char! as element of a string! series!
== #"d"
red>> print #"^(line)" ; Output a blank line
red>> prin 3 prin #"^(tab)" print 6
3 6
red>> #"A" + 1
== #"B"
red>> #"B" > #"A"
== true
top alphanumeric-index category-index
The char? word returns true if the value is a char! type.Examples
red>> char? 33
== false
; 33 is a number and thus not a char!
red>> char? #"A"
== true
red>> char? "AB"
== false
; "AB" is a string, which is a series type
top alphanumeric-index category-index
The word charset is a helper function for the bitset! type.Examples
In Red, the main use of bitsets is with character sets (possibly
across the whole Unicode range).
The charset function provides simplified creation with ranges.
In cases when it is tedious to create massive bitsets (perhaps with huge
numbers of Unicode characters) we can create complemented sets, using not.
The single argument can be:
block! integer! char! string!
We can also use the hyphen "-" to specify ranges.
; -------------------------------------------------------------------------
; Create some bitsets with charset
; -------------------------------------------------------------------------
red>> vowels: charset ["AEIOU"]
== make bitset! #{0000000000000000444104}
red>> digits: charset ["0123456789"]
== make bitset! #{000000000000FFC0}
; -------------------------------------------------------------------------
; Use a range, with -
; -------------------------------------------------------------------------
red>> alpha: charset [#"A" - #"Z" #"a" - #"z"]
== make bitset! #{00000000000000007FFFFFE07FFFFFE0}
red>>
; -------------------------------------------------------------------------
; Mixed types
; -------------------------------------------------------------------------
red>> set-demo: charset [120 "hello" #"A"]
== make bitset! #{00000000000000004000000004890080}
; -------------------------------------------------------------------------
; Complementing -> all unicode characters except these 10
; -------------------------------------------------------------------------
red>> charset [not "0123456789"]
== make bitset! [not #{000000000000FFC0}]
top alphanumeric-index category-index
The word checksum computes a checksum, CRC, hash, or HMAC.Examples
Arguments
1. data = binary!, string! or file!
2. the checksum method - a word:
MD5 SHA1 SHA256 SHA384 SHA512 CRC32 TCP hash.
Refinements
/with - extra value for HMAC key or hash table size;
not compatible with TCP/CRC32 methods.
The value we supply can be:
any-string! binary! integer!
string or binary for MD5/SHA* HMAC key
integer! for hash table size.
; -------------------------------------------------------------------------
; Do a CRC and MD5 checksum
; -------------------------------------------------------------------------
red>> checksum "some text" 'CRC32
== 1337638330
red>> checksum "some text" 'MD5
== #{552E21CD4CD9918678E3C1A0DF491BC3}
top alphanumeric-index category-index
The word clean-path cleans-up '.' and '..' in a pathExamples
and returns the cleaned path.
The argument can be:
file! url! string!
Refinements
/only - do not prepend current directory.
/dir - add a trailing / if missing.
; -------------------------------------------------------------------------
; Remove some redundant parent (..) and current(.) uses.
; -------------------------------------------------------------------------
red>> clean-path %/folder1/folder2/folder3/../../file.txt
== %/folder1/file.txt
red>> clean-path %/folder1/folder2/././file.txt
== %/folder1/folder2/file.txt
; -------------------------------------------------------------------------
; The /dir refinement
; -------------------------------------------------------------------------
red>> clean-path/dir %/folder1/folder2
== %/folder1/folder2/
; -------------------------------------------------------------------------
; The /only refinement
; -------------------------------------------------------------------------
red>> clean-path/only %folder1/file.txt
== %file.txt
red>> clean-path %folder1/file.txt ; without /only
== %/C/red/folder1/file.txt
; -------------------------------------------------------------------------
; Clean up a URL
; -------------------------------------------------------------------------
red>> clean-path http://www/./google.com
== %/www/google.com
top alphanumeric-index category-index
The word clear deletes the contents of a series starting from the currentExamples
index.
; -------------------------------------------------------------------------
; Clear a string!
; -------------------------------------------------------------------------
red>> s: "abcdefghijklm"
== "abcdefghijklm"
red>> clear s
== ""
red>> s
== ""
; -------------------------------------------------------------------------
; Clear part of a series!
; -------------------------------------------------------------------------
red>> n: [1 2 3 4 5 6 7]
== [1 2 3 4 5 6 7]
red>> clear skip n 3
== []
red>> n
== [1 2 3]
; -------------------------------------------------------------------------
; Clear from a found position
; -------------------------------------------------------------------------
red>> s: "the quick brown fox jumps over the lazy dog"
== "the quick brown fox jumps over the lazy dog"
red>> clear find s "jumps"
== ""
red>> s
== "the quick brown fox "
top alphanumeric-index category-index
USAGE:
CLEAR-REACTIONS
DESCRIPTION:
Removes all reactive relations.
CLEAR-REACTIONS is a function! value.
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
Collect in a new block all the values passed to the 'keep' function fromExamples
the body block. We often use collect and keep in parse.
Argument
A body block! to evaluate.
Refinements
/into - Insert into a buffer instead (returns position after insert).
We supply a series!, which will be modified.
;create a block, with no keeps
red>> b: [4 3 * 3 (3 * 10) (5 * 100)]
;nothing to collect
red>> collect b
== []
;now use keep
red>> b: [keep 4 3 * 3 keep (3 * 10) (5 * 100)]
;note the 'kept' items
red>> collect b
== [4 30]
; try the /into refinement
; buf for /into
red>> buf: ["item 1"]
red>> b
== [keep 4 3 * 3 keep (3 * 10) (5 * 100)]
red>> collect/into b buf
== ["item 1" 4 30]
;note value of buf
red>> buf
== ["item 1" 4 30]
top alphanumeric-index category-index
A pre-defined char! value.
See char!
top alphanumeric-index category-index
The word comment introduces a comment in a Red program.Examples
It has the form:
comment {
... multi-line comment ...
}
or:
comment " ... single-line comment ... "
comment {
This is a multi-line comment,
consisting of more than one line.
}
comment "And this is a single-line comment!"
top alphanumeric-index category-index
The word complement returns the complement of the argument.Examples
Has one argument, of type:
logic! integer! bitset! typeset!
For logic!, it returns true or false.
For values with an accessible bit representation, it returns the
"ones complement".
; -------------------------------------------------------------------------
; With logic!
; -------------------------------------------------------------------------
red>> complement 3 > 2
== false
; -------------------------------------------------------------------------
; With integer!s
; -------------------------------------------------------------------------
red>> complement 2
== -3
red>> complement -1
== 0
red>> complement 63
== -64
; -------------------------------------------------------------------------
; With bitset!
; -------------------------------------------------------------------------
red>> b: make bitset![0 1]
== make bitset! #{C0}
red>> complement b
== make bitset! [not #{C0}]
top alphanumeric-index category-index
The word complement? returns true if the bitset isExamples
complemented (that is: inverted with not).
red>> vowels: charset ["AEIOU"]
== make bitset! #{0000000000000000444104}
red>> complement? vowels
== false
red>> non-digits: charset [ not "0123456789"]
== make bitset! [not #{000000000000FFC0}]
red>> complement? non-digits
== true
top alphanumeric-index category-index
Returns a copy of a block, evaluating only paren! items. Here is anExamples
example of a paren!, containing 3 elements:
(2 + 4)
Normally, it does not operate on nested blocks.
The single argument should be a block!
Refinements
/deep - compose nested blocks. All paren! expressions are evaluated,
however deeply they are nested inside [ ].
/only - compose a nested block as a single block containing its values,
rather than its series of values.
/into - put results into a target block, instead of creating a new
block. We supply the target block. This is intended for
very large blocks, and can result in lower memory usage.
; -------------------------------------------------------------------------
; compose some blocks
; -------------------------------------------------------------------------
red>> compose [unevaluated items 1 + 2 then (3 * 5)]
== [unevaluated items 1 + 2 then 15]
;nested blocks are not evaluated
red>> compose [(1 + 2) [(4 + 5)]]
== [3 [(4 + 5)]]
; -------------------------------------------------------------------------
; /deep refinement
; -------------------------------------------------------------------------
red>> b: [cat [dog (3 * 4) [fish (6 * 7)]]]
red>> compose/deep b
== [cat [dog 12 [fish 42]]]
;note the result without /deep
red>> compose b
== [cat [dog (3 * 4) [fish (6 * 7)]]]
; -------------------------------------------------------------------------
; /only refinement
; -------------------------------------------------------------------------
red>> powers: [1 2 4 8 16]
red>> compose/only [a b c (powers)]
;note [...] round powers values
;without /only, no [ ]
== [a b c [1 2 4 8 16]]
red>> compose [a b c (powers)]
== [a b c 1 2 4 8 16]
; -------------------------------------------------------------------------
; /into refinement a trivial example, insignificant memory use.
; -------------------------------------------------------------------------
red>> b: [aa bb (3 * 4)]
;create an empty block
red>> answer: copy []
== []
red>> compose/into b answer
== []
and here is the result
red>> answer
== [aa bb 12]
top alphanumeric-index category-index
USAGE:
CONSTRUCT block
DESCRIPTION:
Makes a new object from an unevaluated spec; standard logic words are
evaluated.
CONSTRUCT is a native! value.
ARGUMENTS:
block [block!]
REFINEMENTS:
/with => Use a prototype object.
object [object!] "Prototype object."
/only => Don't evaluate standard logic words.
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
The word context is a shorter form of make object!.Examples
Using context it is very easy to "hide" variables and functions from
the global naming scope.
; -------------------------------------------------------------------------
; Show that both forms are equal
; -------------------------------------------------------------------------
red>> x: context [a: 1 b: 2]
== make object! [
a: 1
b: 2
]
red>> y: make object! [a: 1 b: 2]
== make object! [
a: 1
b: 2
]
red>> x = y
== true
; -------------------------------------------------------------------------
; Reference fields in a context
; -------------------------------------------------------------------------
red>> x/a
== 1
red>> x/b
== 2
red>> y/a
== 1
red>> y/b
== 2
; -------------------------------------------------------------------------
; Use a function in a context
; -------------------------------------------------------------------------
red>> p: context [x: 5 y: 6 f: function [] [x + y]]
== make object! [
x: 5
y: 6
f: func [][x + y]
]
red>> p/f ; Uses the x and y in the scope of the context
== 11
red>> p/x: 20 ; Update a field
== 20
red>> p/f
== 26
top alphanumeric-index category-index
USAGE:
CONTEXT? word
DESCRIPTION:
Returns the context in which a word is bound.
CONTEXT? is a native! value.
ARGUMENTS:
word [any-word!] "Word to check."
RETURNS:
[object! function! none!]
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
The word continue transfers control back to the begin of a loop.Examples
repeat n 5 [
print ["Before" n]
if n < 3 [continue]
print ["After" n]
]
Before 1
Before 2
Before 3
After 3
Before 4
After 4
Before 5
After 5
top alphanumeric-index category-index
The word copy returns a copy of a series! or bitset!.Examples
The word copy does not work on single items such as:
integer! float! char! etc.
For these, we can simply use the colon (get word).
It has one argument of an appropriate type.
For (potentially large) series!, Red works with references
(pointers to series!) rather than directly with the contents.
Refinements
/part - limit the length of the result,
where length is a number! or series!
/deep - copy nested values also
/types - copy only specific types of non-scalar values.
red>> b: [1 2 3 4]
== [1 2 3 4]
; Create variable b which points to a new block
red>> b2: b
== [1 2 3 4]
; Assign variable b2 to point to the same block that b points to.
red>> b/2: -999
== -999
; Modify the block that b points to at position 2.
red>> b
== [1 -999 3 4]
; As expected b now points to the changed block
red>> b2
== [1 -999 3 4]
; Because b2 points to the same block you see the same change there also!
red>> b: [1 2 3 4]
== [1 2 3 4]
; Create variable b which points to a new block.
red>> b2: copy b
== [1 2 3 4]
; Create variable b2 which points to a copy of the block b points to.
red>> b/2: -999
== -999
; Show that the block that b points to has changed
red>> b2
== [1 2 3 4]
; Because of the copy the block b2 points to is not modified this time.
red>> s: "a string"
== "a string"
; Create variable s pointing to a string.
red>> s2: copy s
== "a string"
; Create variable s2 pointing to a copy of the string that s points to.
red>> a: [22 33 "name" [44 1.34]]
== [22 33 "name" [44 1.34]]
; Create variable a that points to a new block.
red>> b: copy a
== [22 33 "name" [44 1.34]]
; Create variable b and point it to a new copy to the block that a points to.
red>> b: copy/part a 2
== [22 33]
; Only the first 2 items of a are copied!
top alphanumeric-index category-index
This word is a synonym for cosinetop alphanumeric-index category-index
The word cosine returns the trigonometric cosine.Examples
Has 1 parameter, a number! (representing an angle).
Refinements:
/radians : expects the input angle in radians;
without refinement expects the input angle in degrees.
cosine 90
== 0.0
cosine/radians pi
== -1.0
top alphanumeric-index category-index
A pre-defined char! value.
See char!
top alphanumeric-index category-index
To do by red-by-example team ...
The word datatype! is the "mother" of all Red's datatypes.Examples
Note that a typeset! can be defined for grouping several datatypes!
Note that datatype! and typeset! are also a datatype!!
red>> ? datatype!
action! binary! bitset! block! char!
datatype! error! event! file! float!
function! get-path! get-word! hash! image!
integer! issue! lit-path! lit-word! logic!
map! native! none! object! op!
pair! paren! path! percent! point!
refinement! routine! set-path! set-word! string!
time! tuple! typeset! unset! url!
vector! word!
top alphanumeric-index category-index
The word datatype? returns true if its argument is aExamples
datatype! (e.g. integer!, char! etc).
Examples
red>> datatype? 123
== false
red>> datatype? integer!
== true
red>> datatype? block!
== true
top alphanumeric-index category-index
Values of the date! represent calendar dates, relying on the Gregorian
calendar. A date value can also contain optional time and timezone information.
Time in dates is expressed in 24-hour format (including optional time zone
information).
There are a number of date input formats (literals), and we can also construct
dates with make. Calculations can be performed with dates. The current
date and time can be obtained with now.
Path accessors (such as /day, /time ) are available to pick out parts of a date
There are lots of options, and the full documentation is at:
https://doc.red-lang.org/en/date.html
Examples
; -------------------------------------------------------------------------
; Some of the date literal formats, plus an incorrect one.
; -------------------------------------------------------------------------
>> d: 23-Oct-2042
== 23-Oct-2042
>> d: 3-July-2021
== 3-Jul-2021
>> d: 29-February-2017 ;-- not leap year
*** Script Error: cannot MAKE/TO date! from: [day month year]
*** Where: do
*** Stack: load
; -------------------------------------------------------------------------
; Some path accessors
; -------------------------------------------------------------------------
>> appointment: 3/May/2017/6:00:00 ;-- date with time
== 3-May-2017/6:00:00
>> appointment/month
== 5
>> appointment/hour
== 6
; -------------------------------------------------------------------------
; Make a date!
; -------------------------------------------------------------------------
>> any-day: 23
>> month: 2
>> d: make date![any-day month 2017]
== 23-Feb-2017
; -------------------------------------------------------------------------
; Date arithmetic
; -------------------------------------------------------------------------
>> d: now ;-- today's date
== 10-Sep-2017/16:20:04+01:00
>> d + 23:00 ;-- add 23 hours
== 11-Sep-2017/15:20:04+01:00
; -------------------------------------------------------------------------
; Date comparison, with e.g. < and >
; -------------------------------------------------------------------------
>> today: now
== 10-Sep-2017/11:25:24+01:00
>> later: today + 48:00
== 12-Sep-2017/11:25:24+01:00
>> later < today ;-- as expected
== false
>> later > today ;-- as expected
== true
top alphanumeric-index category-index
USAGE:
DATE? value
DESCRIPTION:
Returns true if the value is this type.
DATE? is a function! value.
ARGUMENTS:
value [any-type!]
; -------------------------------------------------------------------------
; date?
; -------------------------------------------------------------------------
>> d: 3-May-2019
>> date? d
== true
>> date? "30-May-2019"
== false
top alphanumeric-index category-index
A pre-defined char! value.
See char!
top alphanumeric-index category-index
Documentation can be found with the word enbase.
top alphanumeric-index category-index
USAGE:
DECOMPRESS data
DESCRIPTION:
Decompresses data. Data in GZIP format (RFC 1952) by default.
DECOMPRESS is a native! value.
ARGUMENTS:
data [binary!]
REFINEMENTS:
/zlib => Data in ZLIB format (RFC 1950).
size [integer!] "Uncompressed data size."
/deflate => Data in DEFLATE format (RFC 1951).
size [integer!] "Uncompressed data size."
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
The word dehex converts URL-style hex encoded (%xx) stringsExamples
into characters. The argument can be a string! or a file!.
red>> dehex "%41BC"
== "ABC"
; 41 is hex for A
red>> dehex "www.search.com/for%20this"
== "www.search.com/for this"
; Hex 20 is space
red>> dehex %my%20file.txt
== "my file.txt"
; file! type
top alphanumeric-index category-index
Deletes the specified file or empty folder. Returns false if the item
does not exist.
Arguments
A file!
Examples
; -------------------------------------------------------------------------
; delete: fffff.red does not exist, ttt.red does exist
; -------------------------------------------------------------------------
>> delete %fffff.red
== false
>> delete %ttt.red
== true
top alphanumeric-index category-index
The word difference returns the elements of two seriesExamples
that are not present in both.
Both series arguments must be of the same datatype, which
can be:
block! hash! string! bitset! typeset!.
difference in one of several set-style functions.
Refinements:
/case - use case-sensitive comparison.
/skip - treat the series! as fixed size records.
Specify the size as an integer!.
red>> difference [1 2 99 4] [1 2 3 4]
== [99 3]
red>> difference ["cat" "dog"] ["dog" "mouse" "fish"]
== ["cat" "mouse" "fish"]
red>> difference "abcd" "Ab"
== "cd"
red>> difference/case "abcd" "Ab"
== "acdA"
top alphanumeric-index category-index
The word dir lists the contents of a directory.Examples
It is tightly related to the word list-dir.
The directory to list to can be:
- an absolute path
- a relative path!
If no argument given, it lists the current directory.
; -------------------------------------------------------------------------
; Using an absolute path
; -------------------------------------------------------------------------
red>> dir %/e/src
8th/ go/ python/ red/
spiderbasic/
; -------------------------------------------------------------------------
; Using it without an argument lists the current directory
; -------------------------------------------------------------------------
red>> dir
basehtml.txt colors.txt design/ gensite.red
gensite.txt pages/ publish/ redftp.lnk
rexec.red tst.red words/
top alphanumeric-index category-index
The word dir? returns true if the supplied name is a valid file path!,Examples
otherwise returns false.
A valid file path! must have a trailing /.
NOTE dir? does not look in the file system at all. It only tells you if the
path! is a valid directory path! with a trailing slash!
Has 1 parameter, which must be a valid file path!.
; Non-existent directory
; Returns false because trailing / is missing
dir? %/c/rubbish
== false
; Returns true because trailing / is present
dir? %/c/rubbish/
== true
; Existing directory
; Returns false because trailing / is missing
dir? %/c/windows
== false
; Returns true because trailing / is present
dir? %/c/windows/
== true
top alphanumeric-index category-index
The word dirize turns its argument into a valid directory.Examples
The argument can be of file! string! url!.
Effectively dirize only appends a trailing / if needed.
red>> dirize "/e/src"
== "/e/src/" ; Note trailing / appended
red>> dirize %/e/src
== %/e/src/ ; Note trailing / appended
red>> dirize %/e/src/
== %/e/src/ ; Nothing happened - directory was already valid
red>> dirize %.
== %./ ; Note trailing / appended
top alphanumeric-index category-index
The word divide returns the first value divided by the second.Examples
This is equivalent to the infix operator /.
Has 2 arguments, which must be of the type:
number! char! pair! tuple! vector!
When a combination of integer! and float! values are divided, the result
is a float!.
When integer!s are divided, the result is an integer!, truncated toward zero.
Division by zero produces a run-time error.
red>> divide 8 3.0
== 2.666666666666667
red>> divide 8 3
== 2
red>> divide -8 3
== -2
red>> divide 4.5.6 2
== 2.2.3
red>> v: make vector![4 2 8]
== make vector! [4 2 8]
red>> divide v make vector![1 2 3 4]
== make vector! [4 1 2]
red>> v2: make vector![ 2 2 3]
== make vector! [2 2 3]
red>> divide v v2
== make vector! [2 1 2]
top alphanumeric-index category-index
The word do evaluates the code in its argument.Examples
Single argument:
1. can be a script to execute;
in that case the /args refinement can be used to specify arguments
for that script (a string! or a block!)
2. can be a string! or a block! containing Red code to be
evaluated
Refinements:
/args - arguments for script to be invoked
; -------------------------------------------------------------------------
; Evaluate a script
; -------------------------------------------------------------------------
; Given a separate script %tst.red:
; Red []
; print "Hi there from %tst.red!"
; args: system/script/args
; print ["My arguments are:" system/script/args]
; if not none? system/script/args [
; foreach arg system/script/args [
; print ["Arg:" arg]
; ]
; ]
; You can evaluate the script without arguments like so:
red>> do %tst.red
Hi there from %tst.red!
My arguments are: none
== none
; You can evaluate the script with string! argument like so:
red>> do/args %tst.red "my args"
Hi there from %tst.red!
My arguments are: my args
Arg: m
Arg: y
Arg:
Arg: a
Arg: r
Arg: g
Arg: s
; If you need multiple arguments, use a block!
red>> do/args %tst.red ["my" "individual" "args"]
Hi there from %tst.red!
My arguments are: my individual args
Arg: my
Arg: individual
Arg: args
; -------------------------------------------------------------------------
; Evaluate a block
; -------------------------------------------------------------------------
red>> blk: [x: 10 y: 20 z: x + y print z]
== [x: 10 y: 20 z: x + y print z]
red>> do blk
30
red>> x
== 10
red>> y
== 20
red>> z
== 30
; -------------------------------------------------------------------------
; Evaluate a string
; -------------------------------------------------------------------------
; Here we show how to do complex things using do
; Use variables to contain the name and value for a variable
; to be embedded in the object
red>> var: "vname"
== "vname"
red>> val: 11
== 11
; Prepare the string with the code
red>> str: copy {obj: make object! [}
== "obj: make object! ["
red>> append str var
== "obj: make object! [vname"
red>> append str copy {: }
== "obj: make object! [vname: "
red>> append str val
== "obj: make object! [vname: 11"
red>> append str copy {]}
== "obj: make object! [vname: 11]"
; String is now complete
red>> str
== "obj: make object! [vname: 11]"
; Evaluate the string
red>> do str
== make object! [
vname: 11
]
; Show that the object now exists:
red>> obj
== make object! [
vname: 11
]
red>> obj/vname
== 11
top alphanumeric-index category-index
USAGE:
DO-SAFE code
DESCRIPTION:
Internal Use Only.
DO-SAFE is a function! value.
ARGUMENTS:
code [block!]
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
USAGE:
DO-THRU url
DESCRIPTION:
Evaluates a remote Red script through local disk cache.
DO-THRU is a function! value.
ARGUMENTS:
url [url!] "Remote file address."
REFINEMENTS:
/update => Force a cache update.
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
The word does defines a function with no arguments orExamples
local variables.
welcome: does [
print "Welcome"
print "to Red"
]
welcome
Welcome
to Red
top alphanumeric-index category-index
A pre-defined char! value.
See char!
top alphanumeric-index category-index
The word either evaluates a conditional expression.Examples
If the result thereof is true the first block! will be evaluated.
If false the second block! will be evaluated.
Has 3 parameters, a conditional expression and 2 block!s
either returns the value returned from the conditionally evaluated block!.
either 3 > 2 [print "Indeed!"][print "Nope!"]
Indeed!
either "ABC" = "abc" [print "The same!"] [print "Different!"]
The same!
; Be careful! Contrary to what is common use in most other programming
; languages, Red considers uppercase and lowercase letters to be equal.
either 2 = 2.0 [print "Equal!"][print "Not equal!"]
Equal!
either 2 == 2.0 [print "Equal!"][print "Not equal!"]
Not equal!
top alphanumeric-index category-index
USAGE:
ELLIPSIZE-AT str len
DESCRIPTION:
Truncate and add ellipsis if str is longer than len.
ELLIPSIZE-AT is a function! value.
ARGUMENTS:
str [string!] "(modified)."
len [integer!] "Max length."
; -------------------------------------------------------------------------
; ellipsize-at
; -------------------------------------------------------------------------
>> ellipsize-at "abcdefghijk" 8
== "abcd..."
top alphanumeric-index category-index
This datatype! lets us represent literal email addresses. Note that no
detailed syntax-checking is performed. It must contain an @ character. An
email! is a series!, and can be manipulated character-by-character.
pre.
; -------------------------------------------------------------------------
; Create some email! types, explore them.
; -------------------------------------------------------------------------
; Create, use type? email?
>> e: [email protected]== [email protected]
>> type? e
== email!
>> email? e
== true
; As a series
>> third e
== #"b"
;limited checking
>> em: @x
== @x
>> type? em
== email!
top alphanumeric-index category-index
This function returns true if its argument is an email! datatype!,Examples
otherwise false.
Arguments
Its single argument can be anytype!.
; -------------------------------------------------------------------------
; explore email?
; -------------------------------------------------------------------------
;some 'true' email! values
>> em: [email protected]
== [email protected]
>> email? em
== true
>> email? a@b
== true
>> email? a@
== true
;'false' email literal
>> email? "a@b" ;string
== false
top alphanumeric-index category-index
The word empty? checks whether a series! is empty.Examples
; -------------------------------------------------------------------------
; A few examples
; -------------------------------------------------------------------------
red>> empty? []
== true
red>> empty? [1] ; Not empty - contains a number
== false
red>> empty? [ [] ] ; Not empty - contains a block!
== false
red>> empty? "x" ; Not empty - contains a character
== false
red>> empty? ""
== true
red>> empty? { "" } ; Not empty - contains a 4 character string
== false
top alphanumeric-index category-index
The word enbase encodes a string to a a binary-coded string (BASE-64 default).Examples
Argument:
string to be encoded
Refinement:
/base - base to be used, e.g. 2, 16 or 64 (default)
The word debase decodes a binary-coded to a binary (BASE-64 default).
Argument:
binary-coded string to be decoded
Refinement:
/base - base to be used, e.g. 2, 16 or 64 (default)
; -------------------------------------------------------------------------
; Default base-64 decoding
; -------------------------------------------------------------------------
red>> x: enbase "Red is beautiful"
== "UmVkIGlzIGJlYXV0aWZ1bA=="
red>> y: debase x
== #{5265642069732062656175746966756C}
red>> to string! y
== "Red is beautiful"
; -------------------------------------------------------------------------
; The /base refinement
; -------------------------------------------------------------------------
red>> x: enbase/base "Red is beautiful" 2
== {0101001001100101011001000010000001101001011100110010000001100010
0110010101100001011101010111010001101001011001100111010101101100}
red>> y: debase/base x 2
== #{5265642069732062656175746966756C}
red>> to string! y
== "Red is beautiful"
; -------------------------------------------------------------------------
; Note that debase needs fixed length chunks!
; -------------------------------------------------------------------------
; for base 2: multiple of 8 bits
; for base 16: multiple of 8 bits
; for base 64: multiple of 16 bits
red>> x: enbase/base "A" 2
== "01000001"
red>> y: debase/base x 2 ; works, because bitstring is a multiple of 8 (bits)
== #{41}
red>> to string! y
== "A"
red>> debase/base "01000001" 2 ; same as contents of y
== #{41}
red>> debase/base "010000011" 2 ; 9 bits - won't work
== none
red>> debase/base "0100001" 2 ; 7 bits - won't work
== none
top alphanumeric-index category-index
The word equal? returns true if two values are equal.Examples
This is the functional form of the infix word =. Look
there for more details.
red>> equal? [1 2 3] [1 2 3]
== true
top alphanumeric-index category-index
The word error! is a datatype!.Examples
Red supports first class errors via this datatype!.
Errors can be generated by the user or produced by the system.
The error definitions are stored in the system/catalog/errors object.
Here is how we can view the definitions:
red>> help system/catalog/errors
system/catalog/errors` is an object! of value:
throw object! [code type break return throw continue]
note object! [code type no-load]
syntax object! [code type invalid missing no-header no-rs-h...
script object! [code type no-value need-value not-defined n...
math object! [code type zero-divide overflow positive]
access object! [code type cannot-open invalid-utf8 no-conne...
user object! [code type message]
internal object! [code type bad-path not-here no-memory stack...
red>>
To see the list of errors concerning 'math' for example, we type:
? system/catalog/errors/math
User errors can be created using 'make' followed by an error integer code,
or a block containing the category, error name and (for some errors) an error argument.
; -------------------------------------------------------------------------
; Error code
; -------------------------------------------------------------------------
red>> make error! 402
*** Math error: attempt to divide by zero
*** Where: ???
; -------------------------------------------------------------------------
; Error name
; -------------------------------------------------------------------------
red>> make error! [math zero-divide]
*** Math error: attempt to divide by zero
*** Where: ???
; -------------------------------------------------------------------------
; Using type, id, and argument
; -------------------------------------------------------------------------
red>> make error! [type: 'script id: 'no-value arg1: 'x]
*** Script Error: x has no value
*** Where: ???
top alphanumeric-index category-index
The word error? returns true if the value is ofExamples
type error!. Otherwise false is returned.
This can be used to check the result from try.
red>> error? try [2 + "3"]
== true
red>> error? try[2 + 3]
== false
top alphanumeric-index category-index
A pre-defined char! value.
See char!
top alphanumeric-index category-index
USAGE:
EVAL-SET-PATH value1
DESCRIPTION:
Internal Use Only.
EVAL-SET-PATH is a function! value.
ARGUMENTS:
value1
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
The word even? returns true if a number! is even.Examples
Else returns false.
Arguments
Has 1 argument: a number!, char!, or time!.
even? -4
== true
even? 77
== false
top alphanumeric-index category-index
Event! values are object-like values that represent external activity.
Look in the official View documents:
https://doc.red-lang.org/en/vid.html
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
USAGE:
EVENT? value
DESCRIPTION:
Returns true if the value is this type.
EVENT? is a routine! value.
ARGUMENTS:
value [any-type!]
RETURNS:
[logic!]
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
The word exclude returns a new value, which is the firstExamples
argument where elements of the second argument are excluded.
With strings, difference in case is not significant.
The argument types can be:
block! hash! string! bitset! typeset!
Refinements:
/case - use case sensitive comparison.
/skip - treat the series! as fixed size records.
Specify the size as an integer!.
red>> exclude [1 2 3 4 5 6] [2 4 6]
== [1 3 5]
; Exclude evens
red>> exclude "The Great Wall Of China" "aeiou"
== "Th GrWlfCn"
; Exclude vowels, ignoring case
red>> exclude/case "The Great Wall Of China" "aeiou"
== "Th GrtWlOfCn"
; Exclude vowels, regarding case - note the letter "O"
top alphanumeric-index category-index
USAGE:
EXISTS-THRU? url
DESCRIPTION:
Returns true if the remote file is present in the local disk cache.
EXISTS-THRU? is a function! value.
ARGUMENTS:
url [url! file!] "Remote file address."
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
The word exists? returns true if its argument is an existing path!Examples
or false otherwise.
red>> exists? %/e/drunk
== false
red>> exists? %/e/src
== true
top alphanumeric-index category-index
The word exit exits a function! without returning a value.Examples
Has no parameters.
fun1: func [p1] [
fun2 p1
print "fun2 completed"
]
fun2: func [p2] [
print "Before exit check ..."
if p2 = "exit" [exit]
print "After exit check"
]
fun1 "abc"
Before exit check ...
After exit check
fun2 completed
fun1 "exit"
Before exit check ...
fun2 completed
top alphanumeric-index category-index
The word exp raises E (the natural number) to the power supplied.Examples
Has one number! parameter.
x: exp 0
== 1.0
x: exp 1
== 2.71828182845905
exp -1
== 0.367879441171442
top alphanumeric-index category-index
USAGE:
EXPAND blk
DESCRIPTION:
Preprocess the argument block and display the output (console only).
EXPAND is a function! value.
ARGUMENTS:
blk [block!] "Block to expand."
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
USAGE:
EXPAND-DIRECTIVES code
DESCRIPTION:
Invokes the preprocessor on argument list, modifying and returning it.
EXPAND-DIRECTIVES is a function! value.
ARGUMENTS:
code [block! paren!] "List of Red values to preprocess."
REFINEMENTS:
/clean => Clear all previously created macros and words.
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
The word extend extends an object! or map! valueExamples
with a list of key and value pairs. The first argument is modified.
The first argument must be object! or map!.
The second argument must be block!, hash! or map!
Refinements:
/case - use case-sensitive comparison
red>> users: make map! ["John23" "safe3" "DingSnooker" "mypasswd"]
== #(
"John23" "safe3"
"DingSnooker" "mypasswd"
)
; Create a map of 2 users and their passwords.
red>> more-users: make map! ["Jane" "pass123" "Sue" "secret"]
== #(
"Jane" "pass123"
"Sue" "secret"
)
; Create another map!
red>> extend users more-users
== #(
"John23" "safe3"
"DingSnooker" "mypasswd"
"Jane"... ;truncated output at the console
; Extend the first map! with the second.
red>> print users
"John23" "safe3"
"DingSnooker" "mypasswd"
"Jane" "pass123"
"Sue" "secret"
; Show all the users in the first map!
red>> extend users make map! ["JOHN23" "newPassword"]
== #(
"John23" "newPassword"
"DingSnooker" "mypasswd"
...
; Modify an existing map!
red>> print users
"John23" "newPassword"
"DingSnooker" "mypasswd"
"Jane" "pass123"
"Sue" "secret"
; Note: new password for John23 (case-insensitive)
top alphanumeric-index category-index
EXTERNAL! is a typeset! value: make typeset! [event!]
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
The word extract extracts a value from a series! atExamples
regular intervals, returning a new series!.
Arguments are:
1. a series!
2. a skip size of type integer!.
Refinements:
/index - extract from an offset position (integer!).
/into - we provide an output series! instead
of creating a new one.
The results will be appended to the series!.
red>> extract [1 2 3 4 5 6 7] 3
== [1 4 7]
; Extracts every 3rd element of a block!.
red>> extract "abcdefghij" 2
== "acegi"
; Extracts every 2nd element of a string!.
red>> extract/index "abcdefghij" 2 4
== "dfhj"
; Start from an offset position (position = 4 at char! #"d")
red>> buffer: []
== []
; Create an empty buffer for results.
red>> extract/into [1 2 3 4 5 6 7] 2 buffer
== [1 3 5 7]
red>> extract/into [10 20 30 40 50 60] 2 buffer
== [1 3 5 7 10 30 50]
red>> buffer
== [1 3 5 7 10 30 50]
; The accumulated results in the buffer.
top alphanumeric-index category-index
To do by red-by-example team ...
USAGE:
FACE? value
DESCRIPTION:
Returns TRUE if the value is a face! object.
FACE? is a function! value.
ARGUMENTS:
value "Value to test."
RETURNS:
[logic!]
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
The word false represents the boolean value false.Examples
Is of datatype logic!
1 = 2
== false
top alphanumeric-index category-index
docstartExamples
USAGE:
FETCH-HELP 'word
DESCRIPTION:
Returns information about functions, values, objects, and datatypes.
FETCH-HELP is a function! value.
ARGUMENTS:
'word [any-type!]
.docend
Fetch-help is similar to help (or ?), but it allows the capturing of the information.
A string is returned.
If the word is not found, a "No matching..." string is returned.
;-- Example: ask the user for a word, display its information
;-- note the to word! use.
the-word: ask "Type the word: "
word-info: fetch-help (to word! the-word )
print word-info
top alphanumeric-index category-index
The word fifth returns the fifth value in a series!.Examples
The argument can be one of:
series! tuple!
If there is no fifth item, then none is returned.
red>> fifth [1 2 3 4 5 6 7]
== 5
red>> fifth "ABCDEFG"
== #"E"
red>> fifth 12.16.13.15.22
== 22
red>> fifth [1 2 3]
== none
; No fifth element found
top alphanumeric-index category-index
The word file! is a datatype!; it can be a:Examples
file name
directory name
directory path!.
A file! is a type of series!, rather like a string.
Note that this type is concerned with file names and places, rather than the contents of a file.
Here are some file! examples
%file.txt
%directory/
%directory/my%20stuff/file.txt
%"directory/my stuff/file.txt"
%/c/windows/softwaredistribution
Notes:
- a file! literal must always start with a percent sign %
- use forward slashes / (Unix style); back slashes will be automagically converted to /
Unusual characters in file names must be encoded with a % hexadecimal number,
such as %20 for space. Thus, my%20stuff refers to "my stuff"; alternatively,
we may enclose them in "quotes", and Red generates the hexadecimal codes for us.
; -------------------------------------------------------------------------
; The file! as a series!
; -------------------------------------------------------------------------
red>> find %top/work/red/demo.txt "red"
== %red/demo.txt
; -------------------------------------------------------------------------
; Use a file! to manipulate contents: load, save
; -------------------------------------------------------------------------
red>> save %demo.txt "some data for a file"
red>> print load %demo.txt
some data for a file
top alphanumeric-index category-index
The word file? returns true if its argument is a file!.Examples
In Red, a file! type value is preceded by %.
If spaces are needed in a file name, enclose it in quotes or use hex %20, as in:
%"my files/"
%/my%20files/
In Windows the drive letter is part of the path!, like so:
%/c/windows/system32/xyz.dll
Absolute path!s start with %/ and relative path!s just with %.
red>> my-data: %/top/personal/stuff.txt
== %/top/personal/stuff.txt
; Absolute path
red>> file? my-data
== true
red>> file? %work.doc
== true
; Relative path
red>> file? %/folder1/folder2/
== true
; A path can also be a directory
top alphanumeric-index category-index
The word find returns the series! from the position where a valueExamples
is found, or none.
Arguments:
1. the item to be searched can be:
series! bitset! typeset! any-object! map! none!
2. the value to be found can be any-type!
Refinements
/part - limit length of search to a length or to a position in the series.
The argument must be number! or series!
/only - Treat a series search value as a single value
/case - Perform a case-sensitive search.
/same - Use "same?" as comparator.
/any - Use * and ? wildcards in string searches. (Not available yet)
/with - Use custom wildcards in place of * and ?. (Not available yet)
/skip - Treat the series as fixed size records. Supply a size integer!
/last - Find the last occurrence of value, from the tail.
/reverse - Find the last occurrence of value, from the current index.
/tail - Return the tail of the match found, rather than the head.
/match - Match at current index only and return tail of match.
; -------------------------------------------------------------------------
; Simple finds - string, block, series, returns the series from the found point
; -------------------------------------------------------------------------
red>> find "The catfish" "cat"
== "catfish"
red>> find "The catfish" "CAT" ; find is case-insensitive by default
== "catfish"
red>> find "The catfish" "dog" ; returns none if not found
== none
red>> find [11 22 33 44 55 66] [ 33 44]
== [33 44 55 66]
; -------------------------------------------------------------------------
; The /part refinement with a length = limit on the region where the search takes place
; -------------------------------------------------------------------------
red>> find/part "The dog" "The" 2 ; search region restricted to "Th"
== none
red>> find/part "The dog" "he" 3 ; search region restricted to "The"
== "he dog"
red>> s: [1 2 3 4 5 6 7 ]
== [1 2 3 4 5 6 7]
red>> place: find s 3 ; find "3" in series "s"
== [3 4 5 6 7]
red>> index? place
== 3
red>> find/part s 2 place ; search restriced to pos 1 up to index? place
== [2 3 4 5 6 7]
red>> find/part s 4 place ; 4 is beyond the index of place
== none
; -------------------------------------------------------------------------
; The /only refinement (not valid for strings)
; -------------------------------------------------------------------------
red>> s: [11 22 [33 44] 55 66]
== [11 22 [33 44] 55 66]
red>> find/only s [33 44] ; treat the nested block as one item
== [[33 44] 55 66]
red>> find s [33 44] ; without /only we cannot find it
== none
; -------------------------------------------------------------------------
; The /skip refinement
; -------------------------------------------------------------------------
; In this cas FIND treats the series as a set of records, where each record
; has a fixed size. FIND will only try to match against each first item of such a record.
red>> s: [11 22 33 44 55 66]
== [11 22 33 44 55 66]
; Next examples have a record size of 2
red>> find/skip s 22 2 ; 22 is the second item of a record. So, no match
== none
red>> find/skip s 33 2 ; 33 is the first item of a record. So, a match
== [33 44 55 66]
; Next examples have a record size of 3
red>> find/skip s 22 3 ; 22 is the second item of a record. So, no match
== none
red>> find/skip s 33 3 ; 33 is the third item of a record. So, no match
== none
red>> find/skip s 44 3 ; 33 is the first item of a record. So, a match
== [44 55 66]
; -------------------------------------------------------------------------
; The /last refinement - search from the end, backwards
; -------------------------------------------------------------------------
red>> s: [11 22 33 44 33 22 11]
== [11 22 33 44 33 22 11]
red>> find/last s 22
== [22 11]
; -------------------------------------------------------------------------
; The /reverse refinement
; -------------------------------------------------------------------------
red>> s: "A1A2A3A4"
== "A1A2A3A4"
red>> s: at s 5 ; position the index halfway the string
== "A3A4"
red>> find/reverse s "A" ; searh backwards from current index
== "A2A3A4"
; -------------------------------------------------------------------------
; The /tail refinement
; -------------------------------------------------------------------------
; Normally FIND returns the result INCLUDING the matched item.
; With /TAIL the result returned is the part AFTER the matching search argument.
red>> find/tail "ABCDEF" "BC"
== "DEF" ; result does NOT include the search argument
red>> find "ABCDEF" "BC"
== "BCDEF" ; result DOES include the search argument
; -------------------------------------------------------------------------
; The /match refinement
; -------------------------------------------------------------------------
; Match always tries to match against the beginning of the series.
; Also the result is EXCLUSIVE the search argument.
red>> find/match "ABCDE" "A" ; mact, because "A" found at begin of string
== "BCDE"
red>> find/match "ABCDE" "D" ; no match - "D" not at beginning of string
== none
red>> find/match "ABCDE" "AB" ; match - search argument is at start of string
== "CDE"
red>> find/match "ABCDE" "DE" ; no match - search argument is beyond start of string
== none
top alphanumeric-index category-index
USAGE:
FIND-FLAG? facet flag
DESCRIPTION:
Checks a flag in a face object.
FIND-FLAG? is a routine! value.
ARGUMENTS:
facet [any-type!]
flag [word!]
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
The word first returns the first value in a series!.Examples
The argument can be one of:
series! tuple! pair!
If there is no first item, none is returned.
red>> first [3 4 5]
== 3
; First of a block!.
red>> first "ABC"
== #"A"
; First of a string!.
red>> first []
== none
; There is no first value here.
red>> first 22.33.44
== 22
; First of a tuple!.
red>> first 200x400
== 200
; First of a pair!.
top alphanumeric-index category-index
USAGE:
FLIP-EXE-FLAG path
DESCRIPTION:
Flip the sub-system for the red.exe between console and GUI modes (Windows only).
FLIP-EXE-FLAG is a function! value.
ARGUMENTS:
path [file!] "Path to the red.exe."
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
The datatype! float! represents a number! type containingExamples
IEEE-754 64-bit floating point numbers.
In literals the e-notation can be used.
Floats that cannot be represented are NaN (Not a Number).
; -------------------------------------------------------------------------
; Some examples
; -------------------------------------------------------------------------
red>> 3e2
== 300.0
red>> 3.5 / 2.1
== 1.666666666666667
red>> 4 / 2.0
== 2.0
red>> 3e2 + 1
== 301.0
red>> 1.0 / 0
*** Math error: attempt to divide by zero
*** Where: /
top alphanumeric-index category-index
The word float? returns true if its argument is a float! type,Examples
otherwise false.
red>> float? 1.23
== true
red>> float? 3
== false
; No decimal point
red>> float? 3.0
== true
red>> float? "3"
== false
; Is a string
top alphanumeric-index category-index
The word forall evaluates its body, moving through theExamples
provided series! one position at a time.
Note that it does not use a single value from the series! for each
repetition, as foreach does. Instead, it increments the index position
of the series!.
numbers: [1 2 -4 8 -16]
forall numbers [
print numbers
]
1 2 -4 8 -16
2 -4 8 -16
-4 8 -16
8 -16
-16
forall numbers [
if (first numbers) >= 0 [
print first numbers
]
]
1
2
8
top alphanumeric-index category-index
The word foreach evaluates its body (a block!) for each value in a series!.Examples
A variable (or block! of variables) must be provided, and foreach will take
each value in the series! as the iteration proceeds.
The arguments are:
1. a variable or block! of variables.
2. a series!, e.g. block!, vector!, string! etc
3. a block! - the body, executed for each iteration
foreach country ["China" "Vietnam" "Thailand"] [
print country
]
China
Vietnam
Thailand
; Assigns the values in the block in turn to the variable "country"
country-series: ["China" "Vietnam" "Thailand"]
foreach country country-series [
print country
]
China
Vietnam
Thailand
; Instead of using a literal block, you can also use a variable holding a block.
foreach character "Red" [
print character
]
R
e
d
; Prints in turn each cgaracter of the string specified.
capital-series: ["China" "Beijing" "USA" "Washington" "UK" "London"]
foreach [country city] capital-series [
prin country
prin " - capital is - "
print city
]
China - capital is - Beijing
USA - capital is - Washington
UK - capital is - London
; The prin function prints without outputting a newline.
top alphanumeric-index category-index
The word forever evaluates a block! body repeatedly (= forever).Examples
The word break can be used to exit the loop if required.
forever [
r: random 10
print r
if r > 5 [break]
]
3
7
; This ends when r gets a value higher than 5
forever [
print "hi"
]
"hi"
; The line above will be printed eternally ...
top alphanumeric-index category-index
The form word converts a value to a string!, in a user-friendly format.Examples
NOTE that, depending on the type, the resulting text might not contain extra
type information (such as [ ] { } and "") as would be produced by mold.
It has one argument, a value.
Refinements:
/part - an integer! value limiting the length of the result string!
Because the Red console has an effect on formatting, we will use print.
red>> print form "ABCDEFG"
ABCDEFG
red>> print form/part "ABCDEFG" 2
AB
red>> print form #"Z"
Z
red>> print form {The Great
Wall Of
China}
The Great
Wall Of
China
red>> print form [1 2 3 4 5 6]
1 2 3 4 5 6
red>> print form/part [1 2 3 4 5 6] 4
1 2
top alphanumeric-index category-index
The word fourth returns the fourth value in a series!.Examples
The argument can be one of:
series! tuple!
If there is no fourth item, none is returned.
red>> fourth [1 2 3 4 5 6 7]
== 4
red>> fourth "ABCDEFG"
== #"D"
red>> fourth [1 2 3]
== none
; No fourth value found!
red>> fourth 22.33.1.255
== 255
top alphanumeric-index category-index
The word func is used to create functions.Examples
Items (variables etc) referred to in the body of a func are assumed to be global.
Se also: function.
Arguments:
1. block! of:
- function arguments
- refinements
- local variable definitions
2. the function body, a block!
; Using a global variable
add-up: func[a b] [
number: a + b
print number ; Global
]
number: 0 ; Global
add-up 3 5
print number ; Global
8 ; Global
8 ; Global
; Shadowing a global variable
add-up2: func [a b /local number] [
number: a + b
print number ; Local
]
number: 20 ; Global
add-up2 3 5
print number ; Global
8 ; Local
20 ; Local
; Using a refinement without a value
add-up3: func [a b /neg /local temp] [
temp: a + b
; Note that a refinement is a boolean
if neg [
temp: temp * -1
]
print temp
]
add-up3 3 5
8
add-up3/neg 3 5
-8
; Using a refinement with a value
add-up4: func [a b /c cval /local temp] [
temp: a + b
if c [
temp: temp + cval
]
print temp
]
add-up4 3 5
8
add-up4/c 3 5 6
14
top alphanumeric-index category-index
The word function is used to define functions.Examples
Functions can accept arguments and return a result.
The general pattern of defining a function is:
my-function-name: function [spec] [
body i.e. code
]
In the spec part the following can be present:
- definition of arguments in the form:
name [type1 type2 ...]
- definition a possible return types:
return: [type1 type2 ...]
- /local followed by all variables that should be local to the
function
See note below.
- /extern followed by all variables that should not be defined as
local. This means that these variables should be
available in global scope. See note below.
Variables assigned using `set-word!` syntax (e.g. v: 123) in the body of
a function are automatically local to the function. If `set` is used instead,
the variable is NOT captured. The combination of those behaviours,
and the `/local` and `/extern` refinements, let you specify variable
locality that makes the intent clear in a variety of situations.
Function with `set-word!` usage is the most common case, and will
be most familiar if you're coming to Red from another language.
If we try to pass wrong types to a function, a run-time message
occurs during interpretation, or at execution time if we compiled it; the
lack of a compilation error message concerning invalid argument types
is due to the flexible way that Red handles types.
Returning from a function:
- exit - return without avalue
- return - return with a value
; -------------------------------------------------------------------------
; Shadowing a global variable
; -------------------------------------------------------------------------
add-up2: function [a b ] [
number: a + b ;set-word syntax here, so local
print number ; Local
]
number: 20 ; Global
red>> add-up2 3 5
8 ; Local
red>> print number
20 ; Global
; -------------------------------------------------------------------------
; Specify the type of the arguments
; -------------------------------------------------------------------------
show-bigger: function [
a[integer!]
b[integer!]
][
either a > b [
print a
][
print b
]
]
red>> show-bigger 44 55
55
red>> show-bigger 4 + 5 10
10
red>> show-bigger 4
*** Script error: show-bigger is missing its b argument
*** Where: show-bigger
red>> show-bigger "x" "y"
*** Script error: show-bigger does not allow string! for its a argument
*** Where: show-bigger
; -------------------------------------------------------------------------
; Allow multiple types for an argument - Generic functions
; -------------------------------------------------------------------------
show-bigger: function [
a[integer! float!]
b[integer! float!]
]
; -------------------------------------------------------------------------
; Or use a type that has other types as its subclasses
; -------------------------------------------------------------------------
show-bigger: function [
a[number!]
b[number!]
]
; -------------------------------------------------------------------------
; Returning a value from a function by default
; -------------------------------------------------------------------------
a-result: function[][
39
40 + 2 ; Last expression in a function is the return value
]
red>> print a-result
42
; -------------------------------------------------------------------------
; Returning a value from a function using return
; -------------------------------------------------------------------------
a-result: function[][
39
return 40 + 2
]
red>> print a-result
42
; -------------------------------------------------------------------------
; Using the returned value in an expression
; -------------------------------------------------------------------------
bigger: function [
a[number!]
b[number!]
][
either a > b [
a
][
b
]
]
red>> 2 + bigger 3 4
6
; -------------------------------------------------------------------------
; Sometimes we need parenthesis when calling a function
; -------------------------------------------------------------------------
red>> bigger (bigger 3 4) (bigger 5 6)
6
; -------------------------------------------------------------------------
; But most of the time it works without parenthesis
; -------------------------------------------------------------------------
red>> bigger bigger 4 5 6
6
; -------------------------------------------------------------------------
; Pass by value / by reference
; -------------------------------------------------------------------------
; When we pass scalar types (i.e. single items, such as a number!,
; a char!, a pair!) as arguments, a copy of their value is passed
; to the function!.
; When we pass a series! a reference is passed instead. The reason is
; that series! can be quite large; copying them would cost too much.
; Passing by reference makes it possible to change the series! from
; within a function!.
; Here an example - the series! will be changed in global scope!
my-append: function [data-block [block!] ][
append data-block -999
]
red>> numbers: [45 33 87 55]
== [45 33 87 55]
red>> my-append numbers
== [45 33 87 55 -999]
red>> numbers
== [45 33 87 55 -999]
; -------------------------------------------------------------------------
; A series! local to a function! - closing over a series!
; -------------------------------------------------------------------------
; A local series! when instantiated from a series! without
; copying, will keep its value over function calls!
; Note that this also applies to string!s
x-s: function[] [
xblock: [] ; The [] is modified each time calling the function!
append xblock "X"
print mold xblock
]
red>> x-s
["X"]
red>> x-s
["X" "X"]
red>> x-s
["X" "X" "X"]
; -------------------------------------------------------------------------
; Avoid the previous situation by copying the series! to initialize it
; -------------------------------------------------------------------------
x-s: function[] [
xblock: copy [] ; On each call we get a fresh new block!
append xblock "X"
print mold xblock
]
red>> x-s
["X"]
red>> x-s
["X"]
red>> x-s
["X"]
; -------------------------------------------------------------------------
; Comments in function! definitions
; -------------------------------------------------------------------------
bigger: function [
"return the bigger of 2 numbers"
arg1[number!] "first number argument"
arg2[number!] "second number argument"
][
either arg1 > arg2 [
arg1
][
arg2
]
]
red>> help bigger
USAGE:
bigger arg1 arg2
DESCRIPTION:
return the bigger of 2 numbers. ; Our docstring
bigger is of type: function!
ARGUMENTS:
arg1 [number!] => first number argument.
arg2 [number!] => second number argument.
REFINEMENTS:
; -------------------------------------------------------------------------
; Using refinements as options
; -------------------------------------------------------------------------
red>> a: [1 9 3 8 2 7 5 4 6]
== [1 9 3 8 2 7 5 4 6]
red>> sort a ; No refinement
== [1 2 3 4 5 6 7 8 9]
red>> a ; Note that sort changes the series!
== [1 2 3 4 5 6 7 8 9]
red>> sort/reverse a ; With /reverse refinement
== [9 8 7 6 5 4 3 2 1]
; -------------------------------------------------------------------------
; Using refinements needing an extra argument
; -------------------------------------------------------------------------
red>> sort/part a 4 ; /part refinement needs an extra argument
== [6 7 8 9 5 4 3 2 1]
red>> sort/reverse/part a 4 ; Combining these 2 refinements
== [9 8 7 6 5 4 3 2 1]
; -------------------------------------------------------------------------
; Using the /neg refinement as an option
; -------------------------------------------------------------------------
add-up3: function [a b /neg] [
temp: a + b
; Note that a refinement is a boolean
if neg [
temp: temp * -1
]
return temp
]
red>> add-up3 3 5
8
red>> add-up3/neg 3 5
-8
; -------------------------------------------------------------------------
; Using refinements /c and /d with extra arguments
; -------------------------------------------------------------------------
add-up4: function [a b /c cval /d dval] [
temp: a + b
; Note that a refinement is a boolean
if c [
temp: temp + cval
]
if d [
temp: temp - dval
]
return temp
]
red>> add-up4 3 5
== 8
red>> add-up4/c 3 5 3
== 11
red>> add-up4/d 3 5 3
== 5
red>> add-up4/c/d 3 5 4 5 ; Note that /c = 4 and /d = 5
== 7
red>> add-up4/d/c 3 5 4 5 ; Note that /c = 5 and /d = 4
== 9
; -------------------------------------------------------------------------
; Forward referencing a function!
; -------------------------------------------------------------------------
; By default you should define your function!s at the top of your
; program and call them later on.
; However, this is not required if and only if the call is from within
; another function!
show-all: function [] [
print "Powers of ten:"
display-powers ; Works from inside a function! body
]
display-powers: function[] [
print [10 1000 10000 100000]
]
red>> show-all
Powers of ten:
10 1000 10000 100000
; -------------------------------------------------------------------------
; Assigning functions to variables - use a prefix colon
; -------------------------------------------------------------------------
red>> sorteren: :sort
== make action! [[
{Sorts a series (modified); default sort or...
red>> sorteren [2 5 3 1 4]
== [1 2 3 4 5]
red>> sorteren/reverse [2 5 3 1 4]
== [5 4 3 2 1]
; -------------------------------------------------------------------------
; Returning from a function without a result value
; -------------------------------------------------------------------------
void: function [] [
exit
]
red>> mold void
== "unset"
; -------------------------------------------------------------------------
; Returning a result value
; -------------------------------------------------------------------------
val: function [] [
return "My message"
]
red>> val
== "My message"
; -------------------------------------------------------------------------
; The /extern refinement
; -------------------------------------------------------------------------
test: function [
/extern var1
] [
var1: 11
var2: 22
]
red>> var1: 1
== 1
red>> var2: 2
== 2
red>> test
== 22
red>> var1
== 11 ; Changed because of /extern - global scope
red>> var2
== 2 ; Shadowed - global scope not changed
top alphanumeric-index category-index
See function?
top alphanumeric-index category-index
The word function? returns true if its argument is a function,Examples
otherwise false.
The single argument can be of any type.
; -------------------------------------------------------------------------
; Make a function, try it, use function?
; -------------------------------------------------------------------------
red>> f: function [] [print "in function"]
== func [][print "in function"]
red>> f
in function ; The function works
red>> function? :f ; The colon is needed to prevent execution
== true ; Definitely a function
red>> function? f ; Is the return value of the function call a function?
in function
== false
red>> f2: func [] [func [] [print "Hi!"]]
== func [][func [] [print "Hi!"]]
red>> function? :f2 ; F2 itself is a function
== true
red>> function? f2 ; Executing F2 also returns a function
== true
red>> function? :first ; Is FIRST a function?
== true
red>> a: 3
== 3
red>> function? a ; Is A a function?
== false
top alphanumeric-index category-index
The word get returns the value a word refers to.Examples
Use in to get a word value inside an object!
Arguments
The single argument is a word! or a path!.
Refinements
/any - If word has no value, return UNSET rather than causing an error.
/case - Use case-sensitive comparison (path only).
; -------------------------------------------------------------------------
; Using a word (w) with get
; -------------------------------------------------------------------------
red>> w: 123
== 123
red>> get 'w ; Use ' (quote) to prevent evaluation of w
== 123
red>> get first [w] ; Use [ ] to prevent evaluation of w
== 123
red>> get w ; w does not contain the name of an existing variable
*** Script Error: get does not allow integer! for its word argument
*** Where: get
; -------------------------------------------------------------------------
; Using a word (w) with get which contains a variable name
; -------------------------------------------------------------------------
red>> x: 12
== 12
red>> w: 'x
== x
red>> get w
== 12
; -------------------------------------------------------------------------
; Using get with a path
; -------------------------------------------------------------------------
red>> b: [11 22 33 44 55]
== [11 22 33 44 55]
red>> place: 3
== 3
red>> get first [b/:place]
== 33
red>> get 'b/:place
== 33
; -------------------------------------------------------------------------
; Using get indirect with a path
; -------------------------------------------------------------------------
red>> x: 12
== 12
red>> b: [11 22 x 44 55]
== [11 22 x 44 55]
red>> get first [b/:place]
== x
red>> get 'b/:place
== x
red>> get get first [b/:place]
== 12
red>> get get 'b/:place
== 12
top alphanumeric-index category-index
The word get-current-dir returns the current directory the program is using.Examples
When a program begins executing, its current directory is set to the one it is
stored in, but the current directory can be changed during run time.
; The example program is filefolders.exe, stored in this file structure:
; C:\red\file-dir-tests\
; filefolders.exe
; folder1A\
; folder1B\
; folder1C\
print ["Current-dir:" get-current-dir]
Current-dir: C:\red\file-dir-tests
cd %folder1A
print ["Current-dir:" get-current-dir]
Current-dir: C:\red\file-dir-tests\folder1A
cd %..
print "Current-dir: "print get-current-dir
Current-dir: C:\red\file-dir-tests
top alphanumeric-index category-index
word get-env returns the value of an OS environment variable of theExamples
current process, as a string1.
To get all environment variables/values, use list-env.
Arguments
The single argument can be any string or any word.
NOTE: the case of the argument is not relevant!
; -------------------------------------------------------------------------
; Get the value of the "USERPROFILE" environment variable
; -------------------------------------------------------------------------
red>> get-env "USERPROFILE" ; On Windows here
== "C:\Users\Mike"
top alphanumeric-index category-index
A get-path! is a kind of path! used to access (get) a value.Examples
Refer to path! and get-word! for more details.
To be completed by Red-by-example team.
; -------------------------------------------------------------------------
; Explore a get-path!
; -------------------------------------------------------------------------
red>> b: [11 22 33 44]
;with a variable
red>> place: 2
== 2
;use a get-path! Note preceding colon
red>> :b/:place
== 22
;Find the type - first [block] is used to prevent evaluation
red>> type? first [:b/:place]
== get-path!
top alphanumeric-index category-index
The word get-path? returns true if its argument is a get-path!,Examples
otherwise false.
Its single argument can be any type.
; -------------------------------------------------------------------------
; Use get-path? on various types; must start with colon :
; -------------------------------------------------------------------------
red>> b: [11 22 33]
== [11 22 33]
red>> n: 2
== 2
red>> get-path? b/n
== false
red>> get-path? first [:b/:n]
== true
red>> get-path? first [b/n]
== false
top alphanumeric-index category-index
The datatype! get-word! is the way in which Red gets values from a variable.Examples
The get-word! type gets the value of a word (which is quite normal thus
far) but does not evaluate the value. For that we use a colon in front of
a variable.
For many simple data types, the result is the value. For special datatype!s
like functions, we need to use the get-word notation.
; -------------------------------------------------------------------------
; Use get-word! type
; -------------------------------------------------------------------------
red>> w: 123
== 123
red>> w
== 123
red>> :w
== 123 ; For normal variable same as evaluating it
red>> f: func [] [print "Hi!"]
== func [][print "Hi!"]
red>> f ; Evaluate the function
Hi!
red>> :f ; Use the get-word - retrieve the definition of f
== func [][print "Hi!"]
top alphanumeric-index category-index
Returns true if its argument is a get-word! type, otherwise false.Examples
Its single argument can be of any type.
; -------------------------------------------------------------------------
; A few types of argument
; -------------------------------------------------------------------------
red>> a-word: 123
;just a normal word
red>> get-word? a-word
== false
: :a-word syntax is correct, but word is evaluated
red>> get-word? :a-word
== false
;prevent evaluation, with first and a [block]
red>> get-word? first[:a-word]
== true
;this is a set-word!
red>> get-word? first[a-word:]
== false
top alphanumeric-index category-index
The word greater-or-equal? is the functional form of the >=Examples
infix word.
red>> greater-or-equal? 44 33
== true
top alphanumeric-index category-index
The word greater? is the functional form of the > infix word.Examples
red>> greater? 44 33
== true
top alphanumeric-index category-index
The halt word stops the interpretation of a script immediately.Examples
Always returns a value of 1.
fun1: func [p1] [
fun2 p1
print "fun2 completed"
]
fun2: func [p2] [
print "Before halt check ..."
if p2 = "halt" [halt]
print "After halt check"
]
fun1 "abc"
Before halt check ...
After halt check
fun2 completed
fun1 "halt"
Before halt check ...
top alphanumeric-index category-index
The word has is a way to define a function that does have local variablesExamples
but has no arguments.
It is followed by a block! of local variable names, and a block! of code.
calc-hours: has [number] [
number: 365 * 24 ; number is local
print number
]
calc-hours
8760
top alphanumeric-index category-index
The datatype! hash! provides a block-like interface but with fastExamples
lookups for most values (block series can be stored in hash! too, but they
will not be hashed, so no faster access).
It is a very flexible container for any kind of hashed tables (not only
associative arrays) while keeping the handy navigational abilities of blocks.
A hash might save time with large data sets and repeated searching.
See also0 the datatype! map!.
; -------------------------------------------------------------------------
; Create a hash!, and use it
; -------------------------------------------------------------------------
red>> list: make hash! [a 123 "hello" b c 789]
== make hash! [a 123 "hello" b c 789]
red>> list/c
== 789
red>> find list 'b
== make hash! [b c 789]
red>> dict: make hash! [a 123 b 456 c 789]
== make hash! [a 123 b 456 c 789]
red>> select dict 'c
== 789
; -------------------------------------------------------------------------
; Regard DICT as 2 records of size 3 and find value 456
; -------------------------------------------------------------------------
red>> select/skip dict 456 3
== 123
top alphanumeric-index category-index
Returns true if its argument is a hash! type, otherwise false.Examples
Arguments
One argument, of any type.
; -------------------------------------------------------------------------
; hash? with a hash! and a block!
; -------------------------------------------------------------------------
red>> list: make hash! [a 123 "hello" b c 789]
;yes, a hash!
red>> hash? list
== true
;b is a block
red>> b: [1 2 3 4]
== [1 2 3 4]
red>> hash? b
== false
top alphanumeric-index category-index
The word head resets the index of a series! to its start.Examples
Note that you can use the word head? to test if a series! is at position 1.
Note that you can use the word index? to obtain the current index.
red>> x: [1 2 3 4 5 6 7]
== [1 2 3 4 5 6 7]
red>> index? x
== 1
red>> head? x
== true
red>> x: skip x 3
== [4 5 6 7]
red>> index? x
== 4
red>> head? x
== false
red>> x: head x
== [1 2 3 4 5 6 7]
red>> index? x
== 1
red>> head? x
== true
top alphanumeric-index category-index
The word head? checks whether a series! index is at the headExamples
position (= position 1) and returns true if it is at head or false
otherwise.
Note that you can use the word index? to obtain the current index!
red>> x: [1 2 3]
== [1 2 3]
red>> head? x
== true
red>> index? x
== 1
red>> x: next x
== [2 3]
red>> head? x
== false
red>> index? x
== 2
top alphanumeric-index category-index
The help word provides an overview of Red words and datatypes.Examples
You may also type ? instead of help.
Help has 1 argument:
- a Red word!:
in this case help displays the help info for exactly that word!
- or a string!:
in this case help displays every piece of help info where this
string! occurs.
red>> help add
USAGE:
add value1 value2
DESCRIPTION:
Returns the sum of the two values.
add is of type: action!
ARGUMENTS:
value1 [number! char! pair! tuple! vector!]
value2 [number! char! pair! tuple! vector!]
REFINEMENTS:
red>> help *
USAGE:
value1 * value2
DESCRIPTION:
Returns the product of two values.
* is of type: op!
ARGUMENTS:
value1 [number! char! pair! tuple! vector!]
value2 [number! char! pair! tuple! vector!]
REFINEMENTS:
red>> help native!
all => Evaluates, returning at the first that is not true
any => Evaluates, returning at the first that is true
arccosine => Returns the trigonometric arccosine (in degrees by default)
arcsine => Returns the trigonometric arcsine (in degrees by default)
arctangent => Returns the trigonometric arctangent (in degrees by default)
... rest of output omitted here ...
red>> help "contr"
continue => Throws control back to top of loop
throw => Throws control back to a previous catch
red>> help "sine"
acos => Returns the trigonometric arccosine
arccosine => Returns the trigonometric arccosine (in degrees by default)
arcsine => Returns the trigonometric arcsine (in degrees by default)
asin => Returns the trigonometric arcsine
cos => Returns the trigonometric cosine
cosine => Returns the trigonometric cosine
sin => Returns the trigonometric sine
sine => Returns the trigonometric sine
top alphanumeric-index category-index
docstartExamples
USAGE:
HELP-STRING 'word
DESCRIPTION:
Returns information about functions, values, objects, and datatypes.
HELP-STRING is a function! value.
ARGUMENTS:
'word [any-type!]
.docend
Returns a string containing the spec. See also help, fetch-help.
If the word is not found, a "No matching..." string is returned.
;-- Example: ask the user for a word, display its information
;-- note the to word! use.
the-word: ask "Type the word: "
word-info: help-string (to word! the-word )
print word-info
top alphanumeric-index category-index
USAGE:
HEX-TO-RGB hex
DESCRIPTION:
Converts a color in hex format to a tuple value; returns NONE if it fails.
HEX-TO-RGB is a function! value.
ARGUMENTS:
hex [issue!] "Accepts #rgb, #rrggbb, #rrggbbaa."
RETURNS:
[tuple! none!]
; -------------------------------------------------------------------------
; hex-to-rgb
; -------------------------------------------------------------------------
>> hex-to-rgb #ff0000
== 255.0.0
top alphanumeric-index category-index
The word if evaluates a conditional expression.Examples
If the result thereof is true, the block! will be evaluated.
If false nothing happens.
Has 2 parameters:
1. a conditional expression
2. a block!.
If returns the resulting value of the block! if the condition is true and
else returns none.
Warning:
The if word is a pitfall for Red n00bs, because it doesn't have an else block!.
The either word provides what programmers would expect.
if 3 > 2 [print "3 > 2"]
3 > 2
if 3 > 2 [print "3 > 2"] [print "2 > 3"]
3 > 2
== [print "2 > 3"]
; This is a n00b error.
; The last block is not a part of the if, so the block is evaluated
; as a stand-alone block, which returns the block itself!
if 2 = 2.0 [print "Different datatypes, but still equal!"]
Different datatypes, but still equal!
if 2 == 2.0 [print "Different datatypes NOT equal!"]
none
top alphanumeric-index category-index
IMMEDIATE! is a typeset! value: make typeset! [datatype! none! logic! char!
integer! float! word! set-word! lit-word! get-word! refinement! issue!
typeset! pair! percent! tuple! time! handle! date!]
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
USAGE:
IMMEDIATE? value
DESCRIPTION:
Returns true if the value is any type of immediate.
IMMEDIATE? is a function! value.
ARGUMENTS:
value [any-type!]
; -------------------------------------------------------------------------
; immediate?
; -------------------------------------------------------------------------
>> immediate? 234
== true
>> immediate? [123]
== false
>> immediate? "23"
== false
>> immediate? #"2"
== true
top alphanumeric-index category-index
The word in fetches a word from an object.Examples
2 arguments:
1. an object! to be accessed
2. the word to be fetched - a word!, block!, or paren!
; -------------------------------------------------------------------------
; Use in on a-point
; -------------------------------------------------------------------------
red>> a-point: make object! [xval: 10 yval: 20]
== make object! [
xval: 10
yval: 20
]
red>> in a-point 'xval
== xval ; Existing member of object
red>> in a-point 'xxval
== none ; Not existing member of object%
red>> get in a-point 'xval
== 10 ; Value of member
top alphanumeric-index category-index
The word index? returns the current index of a series! relative to the head,Examples
or of a word in a context.
The argument can be:
series! word!
In Red, the name referring to a series! need not indicate the first item.
It can be moved to index any part of a series.
It is not like an array name in traditional languages.
NOTE: when you assign a variable to a series, that variable points to the same
series! as the one it is assigned to. This can cause big trouble!!!!
Normally always use copy to avoid that.
; Create a series
red>> numbers: [2 4 8 3 9 27]
== [2 4 8 3 9 27]
; Are we at the start of "numbers"? (index starts at 1, not zero)
red>> index? numbers
== 1
; Find returns the part of the series! from the found item's position
; or none if not found
red>> n: find numbers 9
== [9 27]
; "n" is also a series!
red>> n
== [9 27]
; But "n" still refers to the series! called "numbers"
red>> index? n
== 5
; We could write this more concisely
red>> index? find numbers 9
== 5
; Show that "n" is just a "pointer" into "numbers"
; The "head"of "n" is the same as the "head" of "numbers"
; because the underlaying series! is exactly the same!
red>> n: head n
== [2 4 8 3 9 27]
; NOTE: beware that directly assigning to a series! can wreak havoc!
red>> s1: [1 2 3 4]
== [1 2 3 4]
red>> s2: next s1
== [2 3 4]
red>> append s2 5
== [1 2 3 4 5]
; Append always returns the whole series! and does not consider the index
red>> s2
== [2 3 4 5]
; "s2" is modified
red>> s1
= [1 2 3 4 5]
; "s1" also modified!
; NOTE: avoid this "problem" by using copy
red>> s1: [1 2 3 4]
== [1 2 3 4]
red>> s2: copy next s1
== [2 3 4]
red>> append s2 5
== [2 3 4 5]
red>> s2
== [2 3 4 5]
; "s2"is modified
red>> s1
== [1 2 3 4]
; But "s1" is left intact!
top alphanumeric-index category-index
Gets the value that the user types at STDIN.Examples
INPUT is a function! value.
Has 0 parameters.
It is identical to ask with no parameters.
The resulting string value can be used in an expression
my-name: input ;-- I type Mike now:
Mike
== "Mike"
top alphanumeric-index category-index
The word insert inserts (a) value(s) at a series! index.Examples
The original series! is modified and insert returns the part of the series!
from the position following the insert.
The 2 arguments are:
1. a series! i.e series! bitset! map!
2. a value, which can be of any type
Refinements:
/part - Limit the number of values inserted.
We provide a length of type: number!
or a "pointer" of type series!
/only - insert block! types as single values (overrides /part).
Normally each value in a block! would be separately inserted.
/dup - Duplicate the inserted values. Must provide a count of type number!
NOTE: insert is a destructive operation! It alters its argument.
Most of the time you need to copy before using insert!
red>> s: "this is a string"
== "this is a string"
; Insert at current index - which is 1.
red>> insert s "I think "
== "this is a string"
red>> s
== "I think this is a string"
; Insert at other positions (e.g. after "d")
red>> letters: "abcdefg"
== "abcdefg"
red>> insert find letters "d" "XX"
== "defg"
; This result is the result of "find", not of "insert"!
red>> letters
== "abcXXdefg"
; Now you see the result of "insert".
; Insert at the tail
red>> insert tail letters "-END-"
== ""
; Again: this is the result of "find".
red>> letters
== "abcXXdefg-END-"
; And this of "insert".
; Insert a duplicate, 5 times
red>> insert/dup s "-" 5
== "I think this is a string"
red>> s
== "-----I think this is a string"
; Make a string for /part
red>> s2: "YES it is"
== "YES it is"
; Only insert the first 6 characters of "s2" into "s".
red>> insert/part s s2 6
== "-----I think this is a string"
red>> s
== "YES it-----I think this is a string"
; We need a block type to illustrate /only.
red>> a-block: [3 4 5]
== [3 4 5]
; Normal insert, without /only.
red>> insert a-block [33 44 55]
== [3 4 5]
; Note the separate items.
red>> a-block
== [33 44 55 3 4 5]
; Reset the block.
red>> a-block: [3 4 5]
== [3 4 5]
; Now use /only
red>> insert/only a-block [33 44 55]
== [3 4 5]
; Now the block is inserted, instead of its elements.
red>> a-block
== [[33 44 55] 3 4 5]
top alphanumeric-index category-index
The datatype! integer! represents a number! type containingExamples
32 bit whole signed numbers.
; -------------------------------------------------------------------------
; Some examples
; -------------------------------------------------------------------------
red>> 33 + 55
== 88
red>> 6 / 7
== 0 ; Dividing 2 integers gives rounded result
red>> 6.0 / 7
== 0.8571428571428571
red>> add 6 7
== 13
top alphanumeric-index category-index
The word integer? returns true if its argument is an integer! type,Examples
otherwise false.
Its argument can be any type.
; A literal integer value
red>> integer? 123
== true
; A variable holding an integer value
red>> a: 456
== 456
red>> integer? a
== true
red>> integer? 22x30
== false
red>> integer? 12.3
== false
top alphanumeric-index category-index
INTERNAL! is a typeset! value: make typeset! [unset!]
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
The word intersect returns the intersection (the common items) of two sets.Examples
The arguments can be sets of:
block! hash! string! bitset! typeset!
To create a set with no duplicate items, the unique function can be used.
Refinements:
/case - use case-sensitive comparison.
/skip - treat the series! as fixed size records.
The specified size is an integer!.
; Create 2 series
red>> list1: ["India" "China" "Vietnam" "Singapore"]
== ["India" "China" "Vietnam" "Singapore"]
red>> list2: ["uk" "china" "USA" "India" "France"]
== ["uk" "china" "USA" "India" "France"]
; Common values in these sets
red>> intersect list1 list2
== ["India" "China"]
; When case sensitive
red>> intersect/case list1 list2
== ["India"]
top alphanumeric-index category-index
The issue! datatype! can be used to represent values such as product
codes, credit-card numbers etc. Greater-than/less than comparisons are
allowed.
Literal values in a program must begin with #, and end with a space.
Examples:
#AB-999
#+00-1254-776534
#driver:2019,May
#My-tag
; -------------------------------------------------------------------------
; Issue examples
; -------------------------------------------------------------------------
>> a: #AB-999
== #AB-999
>> b: #BB-999
== #BB-999
>> b > a ;-- compare 2 issues
== true
>> to issue! "My-tag" ;-- make an issue from a string
== #My-tag
top alphanumeric-index category-index
USAGE:
ISSUE? value
DESCRIPTION:
Returns true if the value is this type.
ISSUE? is a function! value.
ARGUMENTS:
value [any-type!]
See issue!
top alphanumeric-index category-index
Theword keys-of returns the list of words of a value that supports reflection.Examples
Arguments
One argument, which should support reflection, e.g. map!, object!.
; -------------------------------------------------------------------------
; Extract the keys of an object
; -------------------------------------------------------------------------
red>> a-point: make object! [
[ xval: 10
[ yval: 20
[ ]
== make object! [
xval: 10
yval: 20
]
red>> keys-of a-point
== [xval yval]
; -------------------------------------------------------------------------
; Extract the keys of a map
; -------------------------------------------------------------------------
red>> a-map: make map![height 6.1 weight 250 age 45]
== #(
height: 6.1
weight: 250
age: 45
)
red>> keys-of a-map
== [height weight age]
top alphanumeric-index category-index
The word last returns the last value in a series!.Examples
If the series! is empty, none is returned.
Its single argument is a series!.
Note that a string! is a series of char!.
red>> last ["cat" "dog" "bird"]
== "bird"
red>> last [33 44]
== 44
red>> last []
== none
red>> last "abcd"
== #"d"
top alphanumeric-index category-index
To do by red-by-example team ...
The word length? returns the number of values in a series!,Examples
from the current index to the tail.
The length of an empty series! is 0.
The length of none is none.
Its single argument can be one of these types:
series! bitset! map! tuple! none
red>> length? [10 20 30]
== 3
red>> length? []
== 0
; Tuple
red>> length? 22.33.44.100
== 4
; Find returns the series from the found item (30) onwards,
; which is [30 40 50]
red>> length? find [10 20 30 40 50] 30
== 3
red>> length? none
== none
; Find returns none; then length is none.
red>> length? find [10 20 30 40 50] 123
== none
top alphanumeric-index category-index
The word lesser-or-equal? returns true if the first argumentExamples
is less than or equal to the second argument, otherwise false.
It is the functional equivalent of the infix word <=.
red>> lesser-or-equal? 33 44
== true
red>> "ABC" <= "abc"
== true
red>> "abc" <= "ABC"
== true
red>> [4 8 5] <= [4 2 1]
== false
red>> [4 1 1] <= [4 2 1]
== true
top alphanumeric-index category-index
The word lesser? returns true if the first argumjent isExamples
less than the second argument, otherwise false.
It is the functional equivalent of the infix word <.
red>> lesser? 33 44
== true
red>> lesser? [8 5 2] [ 7 5 2]
== false
red>> lesser? [6 5 2] [ 7 5 2]
== true
top alphanumeric-index category-index
A pre-defined char! value.
See char!
top alphanumeric-index category-index
The word list-dir shows a list of all files and directories within theExamples
directory given as its argument.
Refinements:
/col - displays in n columns instead of the default 4
; -------------------------------------------------------------------------
; Show contents of current directory
; -------------------------------------------------------------------------
red>> list-dir %/e/src
8th/ go/ python/ red/
spiderbasic/
red>> list-dir/col %/e/src 3
8th/ go/ python/
red/ spiderbasic/
red>> list-dir/col %/e/src 2
8th/ go/
python/ red/
spiderbasic/
top alphanumeric-index category-index
The word list-env returns a map! of OS environment variables/valuesExamples
of the current process.
No arguments.
To get the value of a specific variable, use get-env.
; -------------------------------------------------------------------------
; Use list-env on a Windows PC
; -------------------------------------------------------------------------
red>> type? list-env
== map!
red>> list-env
== #(
"=::" "::\"
"=E:" "E:\Websites\Red-by-Example\auto-v5"
"ALLUSERSPROFILE" "C:\ProgramData"
"APPDATA" "C:\Users\xapwi\AppData\Roaming"
"CommonP... ; The three dots indicate that more ouput is skipped
red>> print list-env ; Prints all the output
... ; Output suppressed - too big
top alphanumeric-index category-index
Look in the official VID documents:
https://doc.red-lang.org/en/vid.html
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
USAGE:
LIT-PATH? value
DESCRIPTION:
Returns true if the value is this type.
LIT-PATH? is a function! value.
ARGUMENTS:
value [any-type!]
; -------------------------------------------------------------------------
; lit-path
; -------------------------------------------------------------------------
>> p: 'a/b/c ;-- a lit-path
== a/b/c ;-- evaluates to a path
>> type? p
== path!
>> p: first ['a/b/c] ;-- no evaluation
== 'a/b/c
>> type? p
== lit-path!
top alphanumeric-index category-index
The datatype! lit-word! is needed to get the literal value (the name)Examples
of a word.
To get the lit-word of variable w, we use 'w.
; -------------------------------------------------------------------------
; Use a lit-word type
; -------------------------------------------------------------------------
red>> a-word: 123
== 123
red>> a-word
== 123
red>> 'a-word
== a-word
top alphanumeric-index category-index
Returns true if its argument is a lit-word! type, otherwise false.Examples
Its single argument can be of any type.
; -------------------------------------------------------------------------
; A few types of argument
; -------------------------------------------------------------------------
red>> a-word: 123
red>> lit-word? a-word
== false
;correct lit-word syntax, but we should not evaluate it
red>> lit-word? 'a-word
== false
;prevent evaluation with first [block]
red>> lit-word? first ['a-word]
== true
top alphanumeric-index category-index
To do by red-by-example team ...
The word load returns a value or block! of values from a file.Examples
Text files and certain types of binary files can be handled.
Its argument specifies the path! to the data and can be:
file! url! string! binary!
Refinements:
/part - load a part of the data, where the part length is
integer! or string!
/into - put results into an existing block!, instead
of creating a new block!.
We need then to provide a block! to be used.
/as - specify the type of the data to be loaded;
we need to specify either:
- none -> load data as code
- word! -> e.g. json, html, jpeg, png, etc.
/header - include Red header as a loaded value
/all - does not evaluate Red header
; one-number.txt has one number in it.
red>> load %one-number.txt
== 234
; This file has 3 numbers, one per line.
; For more than 1 item, a block will be created.
red>> load %three-numbers.txt
== [234 567 789]
; Store the result of load.
red>> some-numbers: load %three-numbers.txt
== [234 567 789]
red>> some-numbers
== [234 567 789]
red>> genres: load %music-types.txt
== ["Blues" "Rock" "Jazz"]
; It is easy to put program code in a file
; and load it ...
red>> prog: load %code-type.txt
== [print "Hello from a block of code in a file!"]
; Use do to execute the result of a load.
red>> do prog
Hello from a block of code in a file!
; face.jpg contains an image, 216 x 216 pixels.
; (the console truncates long values to ... )
red>> p: load %face.jpg
== make image! [216x216 #{
AFB3B6AFB3B6AFB3B6AFB3B6AEB2B5AEB2B5AEB...
top alphanumeric-index category-index
USAGE:
LOAD-THRU url
DESCRIPTION:
Loads a remote file through local disk cache.
LOAD-THRU is a function! value.
ARGUMENTS:
url [url!] "Remote file address."
REFINEMENTS:
/update => Force a cache update.
/as => Specify the type of data; use NONE to load as code.
type [word! none!] "E.g. bmp, gif, jpeg, png."
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
The word log-10 returns the logarithm (base 10) of its argument.Examples
The argument can be a number! (a float! or an integer! )
red>> log-10 9.9
== 0.99563519459755
red>> log-10 1000
== 3.0
top alphanumeric-index category-index
The word log-2 returns the logarithm (base 2) of its argument.Examples
The argument can be a number! (a float! or an integer!)
red>> log-2 2.1
== 1.070389327891398
red>> log-2 32
== 5.0
top alphanumeric-index category-index
The word log-e returns the natural (base E) logarithm of its argument.Examples
The argument can be a number! (a float! or an integer!)
red>> log-e 2.7
== 0.9932517730102835
red>> log-e 3
== 1.09861228866811
top alphanumeric-index category-index
The datatype! logic! consists of two states representing
true and false.
They are returned from comparisons using > <= etc.
The provided boolean words and their values are:
- the pair:
true = true
false = false
- the pair:
on = true
off = false
- the pair:
yes = true
no = false
Note that on and off and yes and no have no built-in logic.
They are preset words which can be more meaningful in certain situations.
.pre
red>> 3 > 2
== true
red>> a: 3
== 3
red>> answer: a > 2
== true
red>> answer
== true
red>> not answer
== false
red>> if a > 2 [print "bigger"]
bigger
red>> yes
== true
; Note: we get false, not no
red>> not yes
== false
red>> power-switch: off
== false
red>> if power-switch = off [power-switch: on]
== true
; Note, not on but true.
red>> power-switch
== true
top alphanumeric-index category-index
The word logic? returns true if its argument is ofExamples
the logic! datatype!, otherwise false.
its argument can be on any type.
red>> open: true
== true
red>> logic? open
== true
red>> large: false
== false
; It is true that 'false' is a logic! type.
red>> logic? large
== true
; Expressions resulting in a logic! result
red>> logic? 3 > 2
== true
red>> logic? 3 + 2
== false
; yes, no, on and off are also logic!
red>> logic? yes
== true
red>> logic? no
== true
red>> logic? on
== true
red>> logic? off
== true
top alphanumeric-index category-index
The word loop executes a given block! a given number of times.Examples
Has 2 parameters:
1. a number! (number of times to evaluate the block!)
2. a block! (to be evaluated the specified number of times)
loop has no return value.
loop 3 [print "Hi!"]
Hi!
Hi!
Hi!
i: 0
loop 10 [i: i + 1]
print ["Value of i =" i]
Value of i = 10
top alphanumeric-index category-index
The word lowercase converts a string of characters to lower-case.Examples
The single argument can be a string! or a char!.
Refinements:
/part - limits to a given length or position.
NOTE: this is a destructive operation. When applying it to a variable,
the variable will be changed!
See also uppercase.
; -------------------------------------------------------------------------
; Try lowercase on char and string
; -------------------------------------------------------------------------
red>> lowercase #"A"
== #"a"
red>> lowercase "ABCD EFGH ijk"
== "abcd efgh ijk"
; -------------------------------------------------------------------------
; Use /part to specify first 2 chars only
; -------------------------------------------------------------------------
red>> lowercase/part "ABCD EFGH ijk" 2
== "abCD EFGH ijk"
top alphanumeric-index category-index
This word is a synonym for list-dirtop alphanumeric-index category-index
The word make returns a new value, based on a type and a specification for the type.Examples
It has 2 arguments:
1. the datatype!, an example or a prototype value
2. the specification of the new value or the number of items (for vector!).
The actual detail of these arguments depends on the datatype!.
For series! types, the specification is often a value for the initial size
of the series!.
red>> make block! 10 ;; size 10 initially
== []
red>> make [] 3
== []
red>> make vector! [1 5 3]
== make vector! [1 5 3]
red>> make vector! 6
== make vector! [0 0 0 0 0 0]
v: make vector! 0
== make vector! []
top alphanumeric-index category-index
The word make-dir creates a directory.Examples
Returns no error if the directory already exists; the contents of existing
directories will be preserved.
The single argument is a file!.
Refinements
/deep - create subdirectories as well
; -------------------------------------------------------------------------
; Make directory in the directory that the interpreter was run from
; -------------------------------------------------------------------------
red>> make-dir %make2/
== %make2/
; -------------------------------------------------------------------------
; The /deep refinement - new directory make3, and its sub-dir
; -------------------------------------------------------------------------
red>> make-dir/deep %make3/sub-dir/
== %make3/sub-dir/
top alphanumeric-index category-index
The datatype! map! represents a series! type.
This datatype provides a dictionary-like data structure, to make it
easy to store key/value pairs while providing very fast lookups.
Note that a map! is NOT a series!, so does not have the
concept of offset or positions.
A map! can have a wide range of types for its keys and values.
Entering a map! in literal format:
#(key1: val1 key2: val2 ... key3: val3)
Note that select and put are case-insensitive about keys.
To make them case-sensitive, use the case refinement, as in select/case.
.pre
; -------------------------------------------------------------------------
; Create 2 users with their respective passwords in a map!
; -------------------------------------------------------------------------
red>> users: make map! ["John23" "safe3" "DingSnooker" "mypasswd"]
== #(
"John23" "safe3"
"DingSnooker" "mypasswd"
)
red>> print users
"John23" "safe3"
"DingSnooker" "mypasswd"
; -------------------------------------------------------------------------
; Retrieve passwords
; -------------------------------------------------------------------------
red>> print select users "john23" ; Note: case insensitive!
safe3
red>> print select users "mr-x"
none ; Not found in the map!
; -------------------------------------------------------------------------
; Add a new user/password
; -------------------------------------------------------------------------
red>> put users "JaneUK" "magicpass"
== "magicpass"
red>> print users
"John23" "safe3"
"DingSnooker" "mypasswd"
"JaneUK" "magicpass"
; -------------------------------------------------------------------------
; Change a password for an existing user
; -------------------------------------------------------------------------
red>> put users "John23" "mygoodPass"
== "mygoodPass"
red>> print users
"John23" "mygoodPass"
"DingSnooker" "mypasswd"
"JaneUK" "magicpass"
; -------------------------------------------------------------------------
; Delete a user
; -------------------------------------------------------------------------
red>> put users "John23" none
== none
red>> print users
"DingSnooker" "mypasswd"
"JaneUK" "magicpass"
; -------------------------------------------------------------------------
; How many users left?
; -------------------------------------------------------------------------
red>> print length? users
2
; -------------------------------------------------------------------------
; Select a user by his key
; -------------------------------------------------------------------------
red>> users: make map! ["John23" "safe3" "DingSnooker" "mypasswd"]
== #(
"John23" "safe3"
"DingSnooker" "mypasswd"
)
red>> select users "John23"
== "safe3"
top alphanumeric-index category-index
The word map? returns true if its argument is a map! datatype!,Examples
otherwise false.
This datatype! provides a dictionary-like data structure,
storing key/value pairs.
; Using a map literal
red>> p: #(a: 3 b: 4 c: 5)
== #(
a: 3
b: 4
c: 5
)
; Use make to create a map
red>> users: make map! ["John23" "safe3" "DingSnooker" "mypasswd"]
== #(
"John23" "safe3"
"DingSnooker" "mypasswd"
)
red>> map? users
== true
red>> ages: [1 1949 2 1980 3 1999]
== [1 1949 2 1980 3 1999]
red>> map? ages
== false
; Extending a map with new keys/values:
red>> x: make map! [1 "January" 2 "Februari" 3 "March"]
== #(
1 "January"
2 "Februari"
3 "March"
)
red>> extend x [4 "April" 5 "May"]
== #(
1 "January"
2 "Februari"
3 "March"
4 "April"...
red>> print mold x
#(
1 "January"
2 "Februari"
3 "March"
4 "April"
5 "May"
)
; Retrieving a value using a key.
red>> x/1
== "January"
red>> x/5
== "May"
; Out of range
red>> x/7
== none
top alphanumeric-index category-index
This function evaluates a block! using mathematical precedence rules,Examples
returning the last result.
Arguments
The single argument is a block!.
. box Evaluate a block with math, then without.
>> a: 6
== 6
; Evaluate the / before the +
>> math [a + 8 / 2]
== 10
; Now left-to right, the + first
>> print [a + 8 / 2]
7
top alphanumeric-index category-index
The word max returns the greater of its 2 arguments.Examples
The 2 arguments must be of:
scalar! series!
red>> max 4 3
== 4
red>> max 3.1 3.11
== 3.11
red>> max #"B" #"C"
== #"C"
red>> max "ABC" "BBC"
== "BBC"
red>> max "ABC" "ABCD"
== "ABCD"
red>> max [1 2 3] [2 2 3 3]
== [2 2 3 3]
top alphanumeric-index category-index
The word min returns the smaller of its two arguments.Examples
There are 2 arguments of type:
scalar! series!
red>> min 4 3
== 3
red>> min 3.1 3
== 3
red>> min #"B" #"C"
== #"B"
red>> min "ABC" "BBC"
== "ABC"
red>> min "ABC" "ABCD"
== "ABC"
red>> min [1 2 3] [3 2 1 0]
== [1 2 3]
top alphanumeric-index category-index
USAGE:
MOD a b
DESCRIPTION:
Compute a nonnegative remainder of A divided by B.
MOD is a function! value.
ARGUMENTS:
a [number! char! pair! tuple! vector! time!]
b [number! char! pair! tuple! vector! time!] "Must be nonzero."
RETURNS:
[number! char! pair! tuple! vector! time!]
See also modulo
; -------------------------------------------------------------------------
; mod
; -------------------------------------------------------------------------
>> mod 7 3 ;-- integer - divides 2 times
== 1
>> mod -7 3 ;-- negative - divides -3 times, mod must be non-negative
== 2
>> mod 7.5 2.1 ;-- float - divides 3 times
== 1.2
top alphanumeric-index category-index
USAGE:
MODIFY target field value
DESCRIPTION:
Change mode for target aggregate value.
MODIFY is an action! value.
ARGUMENTS:
target [object! series!]
field [word!]
value [any-type!]
REFINEMENTS:
/case => Perform a case-sensitive lookup.
RETURNS:
[map! file!]
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
USAGE:
MODULO a b
DESCRIPTION:
Wrapper for MOD that handles errors like REMAINDER. Negligible values
(compared to A and B) are rounded to zero.
MODULO is a function! value.
ARGUMENTS:
a [number! char! pair! tuple! vector! time!]
b [number! char! pair! tuple! vector! time!]
RETURNS:
[number! char! pair! tuple! vector! time!]
See also mod
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
The word mold returns a Red-readable string! representing a value.Examples
This means that, for example, values can be stored in files and re-input to
a program when molded.
Type information is included in the string!for example:
[ ] around a block,
#"" around a character
and { } around multi-line strings
A single argument of any type is required
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!
red>> print mold 12.34
12.34
red>> print mold "a string"
"a string"
red>> mold #"A"
#"A"
red>> print mold { The Great
Wall Of
China}
" The Great^/ Wall Of^/China"
red>> print mold [1 2 3]
[1 2 3]
red>> print mold/only [1 2 3]
1 2 3
red>> print mold/part [1 2 3 4 5] 3
[1
top alphanumeric-index category-index
Moves one or more elements from one series to another position or series.Examples
By default, one element is moved. The origin series is modified and
returned. The target series is modified.
Arguments
origin - a series!
target - a series!
Refinements
/part - control the number of values inserted. We supply an
integer! length.
; -------------------------------------------------------------------------
; move, and its effects
; -------------------------------------------------------------------------
;make 2 series
red>> s1: [a b c d]
red>> s2: [11 12]
;move first element of s1 to s2
;note the returned value: a modified s1
red>> move s1 s2
== [b c d]
;s1 is modified
red>> s1
== [b c d]
;s2 is modified
red>> s2
== [a 11 12]
; -------------------------------------------------------------------------
; /part refinement
; -------------------------------------------------------------------------
;make 2 series
red>> s1: [a b c d e]
red>> s2: [1 2 3 4]
;move 2 elements, not 1
red>> move/part s1 s2 2
== [c d]
red>> s2
== [a b 1 2 3 4]
top alphanumeric-index category-index
The word multiply multiplies 2 number!s.Examples
This is equivalent to the infix operator *.
Has 2 arguments, which must be of type:
number! char! pair! tuple! vector!
red>> multiply 3 4.2
== 12.6
red>> multiply 11x20 4
== 44x80
red>> multiply 1.4.5 2
== 2.8.10
red>> v: make vector![2 3 4]
== make vector! [2 3 4]
red>> v2: make vector![20 20 3]
== make vector! [20 20 3]
red>> multiply v v2
== make vector! [40 60 12]
top alphanumeric-index category-index
The word NaN? returns true if the argument is 'not a number',Examples
otherwise false.
'Not a number' means that it is impossible to represent it internally.
The argument is of type: number!
; 1.5 is a valid number!
red>> NaN? 3 / 2
== false
; 3 is a valid number!
red>> NaN? square-root 9
== false
; Square root of a negative number.
red>> NaN? square-root -9
== true
top alphanumeric-index category-index
Represents external functions that are written in Red/System code, not in Red.
Type:
? native!
to get a list of them.
top alphanumeric-index category-index
USAGE:
NATIVE? value
DESCRIPTION:
Returns true if the value is this type.
NATIVE? is a function! value.
ARGUMENTS:
value [any-type!]
See native
top alphanumeric-index category-index
Used to incorporate modules of code into your program.
You will meet this early on when using VID,as in:
Red[ needs 'View]
...rest of program.
top alphanumeric-index category-index
The word negate inverses its argument (positive <-> negative).Examples
red>> negate 3
== -3
red>> negate -3
== 3
red>> negate 5x4
== -5x-4
red>> negate 5x-4
== -5x4
red>> negate -5x4
== 5x-4
top alphanumeric-index category-index
The word negative? returns true if the numeric parameterExamples
is less than zero, otherwise false.
NOTE that zero returns false.
Has one numeric parameter.
negative? -2
== true
negative? 0
== false
negative? 0.1
== false
top alphanumeric-index category-index
word new-line sets or clears the new-line markers within a block! or paren!.Examples
These markers are not considered when Red examines values, but do affect the displayed
format. The original series! is modified.
Arguments
1. position - a block! or paren!, positioned to the required place.
2. value - logic!. Use true/on/yes for newlines to be added.
Refinements
/all - set/clear marker to end of series.
/skip - set/clear marker periodically to the end of the series.
We must provide and integer! skip size.
; -------------------------------------------------------------------------
; Show that series can contain new-lines
; -------------------------------------------------------------------------
red>> b1: [1 2 3 4 5 6]
== [1 2 3 4 5 6] ; No new-lines here
red>> b2:[ 1 2 ; First new-line in series
[ 3 4 ; Second new-line in series
[ 5 6]
== [1 2
3 4
5 6
]
red>> b2
== [1 2
3 4
5 6
]
red>> b1 = b2
== true ; Equal despite new-lines difference
; -------------------------------------------------------------------------
; Manipulate series with new-lines
; -------------------------------------------------------------------------
red>> b: [1 2 3 4 5 6]
== [1 2 3 4 5 6]
red>> new-line next next b on
== [
3 4 5 6 ; Returns the series beginning with the new new-line
]
red>> b
== [1 2 ; Now the new-line becomes visible
3 4 5 6
]
; -------------------------------------------------------------------------
; The /all refinement
; -------------------------------------------------------------------------
red>> new-line/all b off
== [1 2 3 4 5 6] ; All new-lines removed
red>> new-line/all b on
== [ ; Now new-lines everywhere
1
2
3
4
5
6
]
red>> new-line/all b off
== [1 2 3 4 5 6] ; All new-lines removed
; -------------------------------------------------------------------------
; The /skip refinement
; -------------------------------------------------------------------------
red>> new-line/all/skip b on 2
== [
1 2
3 4
5 6
]
top alphanumeric-index category-index
USAGE:
NEW-LINE? position
DESCRIPTION:
Returns the state of the new-line marker within a list series.
NEW-LINE? is a native! value.
ARGUMENTS:
position [any-list!] "Position to change marker."
RETURNS:
[any-list!]
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
A pre-defined char! value.
See char!
top alphanumeric-index category-index
The word next moves the index of a series! 1 position towardsExamples
the tail (= the end) of the series!.
When a series! index is already at tail position, next won't
change the index; it stays at tail.
The opposite of next is back.
red>> ser: [1 2 3]
== [1 2 3]
red>> ser: [1 2 3]
== [1 2 3]
red>> next ser
== [2 3]
red>> ser ; next is not destructive
== [1 2 3]
red>> ser: next ser
== [2 3]
red>> ser
== [2 3]
red>> ser: next ser
== [3]
red>> ser
== [3]
red>> ser: next ser
== [] ; We are at tail postion
red>> tail? ser
== true
red>> ser
== []
red>> ser: next ser ; Already at tail - do nothing
== []
red>> tail? ser
== true
red>> ser
== []
top alphanumeric-index category-index
The value no can be used as the boolean value false.Examples
red>> no
== false
red>> not no
== true
top alphanumeric-index category-index
The word none is a special value, and its datatype is none!Examples
none is not the same as an empty block!, empty string!,
zero number! or null char!.
It is an actual value that represents non-existence.
none is returned in certain circumstances from several functions,
primarily those involving series!.
pick [11 22 33] 2
== 22
; Elements in a block are numbered 1 thru n
pick [11 22 33] 4
== none
; Out of range results in a none value
pick [true false true] 2
== false
pick [true false true] 4
== none
; Out of range results in a none value
a: 123
== 123
type? a
== integer!
a: none
== none
; none is just a value, like 123 before
type? a
== none!
either pick [11 22 33] 4 [print "got it"] [print "problem"]
problem
; In a comparison none behaves the same as false
some-function: func [arg1] [
; code omitted
return none ; no answer found
]
; This function returns none as the return value
answer: some-function 42
== none
print none? answer
true
top alphanumeric-index category-index
The none! datatype contains a single value that represents nothing or no
value. See none for more details.
top alphanumeric-index category-index
The word none? returns true if its argument has a none value,Examples
otherwise false.
Its single argument can be of any type.
red>> none? 3 / 4
== false
; Find searches and returns none if not found
red>> none? find "ABCD" "Z"
== true
; If found, returns the series! from the found item ("BCD").
red>> none? find "ABCD" "B"
== false
top alphanumeric-index category-index
USAGE:
NORMALIZE-DIR dir
DESCRIPTION:
Returns an absolute directory spec.
NORMALIZE-DIR is a function! value.
ARGUMENTS:
dir [file! word! path!]
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
The word not returns the logical complement of a value.Examples
NOTE that only the logic! values false and none
return true.
For other types such as integer!, float!, bitset!,
false is returned, irrespective of the data value.
Use complement to perform a bitwise not.
red>> not (3 > 2)
== false
red>> not (10 > 20)
== true
red>> not 1
== false
red>> not 0
== false
red>> not -1
== false
red>> not none
== true
top alphanumeric-index category-index
The word not-equal? returns true if two values (of any type)Examples
are not equal.
When comparing string!s lowercase is considered equal to uppercase.
There is also an infix <> operator, doing the same.
red>> not-equal? 12 12.0
== false
red>> not-equal? "abc" "abc"
== false
red>> not-equal? "abC" "ABC"
== false
top alphanumeric-index category-index
The word now returns the current date and time.
Refinements allow the selection of, for example, only the date (as a date! type),
or only the time (as a time! type).
There are no arguments
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!
Examples
; -------------------------------------------------------------------------
; No refinements (NB executed on 10 September 2017, in UK)
; -------------------------------------------------------------------------
>> d: now
== 10-Sep-2017/10:41:58+01:00
>> type? d
== date!
; -------------------------------------------------------------------------
; Some refinements
; -------------------------------------------------------------------------
>> now/time
== 10:42:57
>> now/year
== 2017
>> now/date
== 10-Sep-2017
top alphanumeric-index category-index
A pre-defined char! value.
See char!
top alphanumeric-index category-index
The datatype! number! is a typeset! containing other datatype!s.Examples
; -------------------------------------------------------------------------
; Show all subtypes of number!
; -------------------------------------------------------------------------
red>> ? number!
number! is a typeset! of value:
make typeset! [
integer!
float!
percent!
]
top alphanumeric-index category-index
docstartExamples
USAGE:
NUMBER? value
DESCRIPTION:
Returns true if the value is any type of number.
NUMBER? is a function! value.
ARGUMENTS:
value [any-type!]
.docend
Checks for a number! type. This is type-checking, not input validation.
>> number? "123" ;-- a string
== false
>> number? 123 ;-- integer!
== true
>> f: 12.34
>> number? f ;-- float!
== true
>> number? 33% ;-- percent
== true
top alphanumeric-index category-index
An object is a container that groups data and/or functions withinExamples
one variable.
Red implements 'prototype-based objects'.
Creating new objects is done by cloning existing objects
or the base object! value.
During the creation process, existing field values can be modified
and new fields can be added.
It is a very simple and efficient model to encapsulate your Red code.
; make an object - a pretend square box
box: make object! [
size: 10
show: function [] [
print ["Showing size =" size "from within the box!"]
]
]
; Use the encapsulated function
box/show
Showing size = 10 from within the box!
top alphanumeric-index category-index
Object Model
------------
Red implements prototype-based objects. Creating new objects is done
by cloning existing objects or the base object! value. During the creation
process, existing field values can be modified and new fields can be added.
It is a very simple and efficient model to encapsulate your Red code.
Object Creation
---------------
We create new objects by:
make object! <specification block>
There are also some shorter alternative syntaxes (just handy shortcuts):
object <specification block>
context <specification block>
The specification block can contain any valid Red code. Words set at the root
level of that block will be collected and will constitute the new object's fields.
You can put any valid code into a specification block, and it will be evaluated
during the object construction, and only then. Here is an example. Note that
"hello" is printed when we make the object, but not when we evaluate it:
red>> obj: make object! [print "hello" a: 1 b: 2]
hello
== make object! [
a: 1
b: 2
]
;now use it
red>> obj
== make object! [
a: 1
b: 2
]
Nesting
-------
Objects can also be nested easily:
obj: object [
a: 123
b: object [
c: "hello"
d: object [
data: none
]
]
]
Another way to create an object is to use the copy action which does not
require a specification block, so does just a simple cloning of the object.
Existing functions will be re-bound to the new object.
Syntax:
copy <object>
Object Access Paths
-------------------
In order to access object fields, the common path syntax is used (words
separated by a slash character). Each word (or expression) in a path is
evaluated in the context given by the left side of the path. Evaluation
of a word referring to a function will result in invoking the function,
with its optional refinements.
Example:
book: object [
title: author: none
show: does [print [mold title "was written by" author]]
]
book/title: "The Time Machine"
book/author: "H.G.Wells"
print book/title
book/show
will output:
The Time Machine
"The Time Machine" was written by H.G.Wells
SELF Reference
--------------
A special keyword named self has been reserved when self-referencing
the object is required.
Example:
book: object [
title: author: none
list-fields: does [words-of self]
]
book/list-fields
will output:
[title author list-fields]
Object inheritance
------------------
Though cloning produces exact replicas of the prototype object, it is also
possible to extend it in the process, using make action.
Syntax:
make <prototype> <spec>
<prototype> : object that will be cloned and extended
<spec> : specification block
Example:
a: object [value: 123]
c: make a [
increment: does [value: value + 1]
]
print c/increment
print c/increment
will output:
124
125
It is also possible to use another object as <spec> argument. In such
case, both objects are merged to form a new one. The second object takes
priority in case both objects share same field names.
Example:
a: object [
value: 123
show: does [print value]
]
b: object [value: 99]
c: make a b
c/show
will output:
99
Extended Actions and Natives for Objects
----------------------------------------
You can use set on an object to set all fields at the same time, and
get on an object will return a block of all the fields values.
Example:
obj: object [a: 123 b: "hello"]
probe get obj
set obj none
?? obj
set obj [hello 0]
?? obj
probe :obj/a
will output:
[123 "hello"]
obj: make object! [
a: none
b: none
]
obj: make object! [
a: 'hello
b: 0
]
hello
The find function gives you a simple way to check for a field name in
an object. If found it will return true, else none.
The select action does the same check as find, but returns the field
value for matched word.
obj: object [a: 123]
probe find obj 'a
probe select obj 'a
probe find obj 'hello
will output:
true
123
none
The in word will allow you to bind a word to a target context:
a: 0
obj: object [a: 123]
probe a
probe get in obj 'a
will output:
0
123
The bind function is also available.
Reflectors
----------
The reflective functions words-of, values-of, body-of can be
used to access an object's internal structure.
These notes are heavily based on
http://www.red-lang.org/2014/12/050-objects-support.html
top alphanumeric-index category-index
The word object? returns true if its argument is an object!.Examples
The single argument can be of any type.
; make an object - a pretend square box
red>> box: make object! [
[ size: 10
[ show: function [] [
[ print ["Showing size =" size "from within the box!"]
[ ]
[ ]
== make object! [
size: 10
show: func [][
print ["...
; Is this an object?
red>> object? box
== true
; Definitely not an object
red>> amounts: [2 3 4]
== [2 3 4]
red>> object? amounts
== false
top alphanumeric-index category-index
The word odd? returns true if its numeric parameter is odd,Examples
else returns false.
Has 1 numeric parameter, which should be an integer!.
odd? -3
== true
odd? 10
== false
top alphanumeric-index category-index
The word off has the logical value "false"Examples
and its datatype! is logic!
Logical values are produced from various comparisons.
The logic! type also provides:
false, no, on, true, yes
red>> ? off
off is a logic! of value: false
red>> either off [print "t is true"] [print "t is NOT true"]
t is NOT true
top alphanumeric-index category-index
USAGE:
OFFSET? series1 series2
DESCRIPTION:
Returns the offset between two series positions.
OFFSET? is a function! value.
ARGUMENTS:
series1 [series!]
series2 [series!]
; -------------------------------------------------------------------------
; offset?
; -------------------------------------------------------------------------
>> s: "the big cat" ;-- nb "c" is 9th char
== "the big cat"
>> s2: find s "cat"
== "cat"
>> offset? s s2
== 8
top alphanumeric-index category-index
The word on has the logical value "true"Examples
and its datatype! is logic!
Logical values are produced from various comparisons.
The logic! type also provides:
false, no, off, true, yes
red>> ? on
on is a logic! of value: true
red>> either on [print "It is true!"] [print ["It is NOT true!"]]
It is true!
top alphanumeric-index category-index
USAGE:
ON-FACE-DEEP-CHANGE* owner word target action new index part state
forced?
DESCRIPTION:
Internal use only.
ON-FACE-DEEP-CHANGE* is a function! value.
ARGUMENTS:
owner
word
target
action
new
index
part
state
forced?
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
USAGE:
ON-PARSE-EVENT event match? rule input stack
DESCRIPTION:
Standard parse/trace callback used by PARSE-TRACE.
ON-PARSE-EVENT is a function! value.
ARGUMENTS:
event [word!] {Trace events: push, pop, fetch, match, iterate,
paren, end.}
match? [logic!] "Result of last matching operation."
rule [block!] "Current rule at current position."
input [series!] "Input series at next position to match."
stack [block!] "Internal parse rules stack."
RETURNS:
TRUE: continue parsing, FALSE: stop and exit parsing.
[logic!]
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
The datatype! op! represents the Red functions that act as infixExamples
operators.
; -------------------------------------------------------------------------
; List all infix operators with type op!
; -------------------------------------------------------------------------
red>> ? op!
% => Returns what is left over when one value is divided by another
* => Returns the product of two values
** => Returns a number raised to a given power (exponent)
+ => Returns the sum of the two values
- => Returns the difference between two values
/ => Returns the quotient of two values
// => Compute a nonnegative remainder of A divided by B
< => Returns TRUE if the first value is less than the second
<<
<= => Returns TRUE if the first value is less than or equal
to the second
<> => Returns TRUE if two values are not equal
= => Returns TRUE if two values are equal
== => Returns TRUE if two values are equal, and also the
same datatype
=? => Returns TRUE if two values have the same identity
> => Returns TRUE if the first value is greater than the second
>= => Returns TRUE if the first value is greater than or equal to
the second
>>
>>>
and => Returns the first value ANDed with the second
is => Defines a local reactive relations inside a reactor
or => Returns the first value ORed with the second
xor => Returns the first value exclusive ORed with the second
; -------------------------------------------------------------------------
; Some examples of infix operations
; -------------------------------------------------------------------------
red>> 1 + 2
== 3
red>> 2 * 5
== 10
red>> 10 / 2
== 5
red>> 10 // 2
== 0
red>> 10 // 3
== 1
top alphanumeric-index category-index
The word op? returns true if its argument is an op! type.Examples
This type includes infix operators, such as + - * etc).
Note the use of : to prevent evaluation of the argument!
red>> op? :and
== true
red>> op? :print
== false
red>> op? :+
== true
red>> op? :-
== true
red>> op? :float?
== false
top alphanumeric-index category-index
The infix word or performs a logical "or" of two values.Examples
The values are restricted to these types:
logic! integer! char! bitset! typeset!
pair! tuple! #vector!
It is the infix version of the word or~.
In the case of oring two integer!s (or closely related types,
such as char! and pair!), a bitwise (bit-by-bit) process is performed.
In general, types can be mixed, but logic! types cannot be mixed with other types.
The type of the result is the same type as the first value for a bitwise or.
For a logic! or, true or false are returned.
red>> (3 > 2) or false
== true
red>> 8 or 16
== 24
red>> 16 or false
*** Script error: logic type is not allowed here
*** Where: or
red>> my-pair: 3x4
== 3x4
red>> my-pair or 1
== 3x5
red>> 1 or my-pair
*** Script error: or does not allow pair for its value2 argument
*** Where: or
red>> v1: make vector![1 2 3]
== make vector! [1 2 3]
red>> v2: make vector![1 1 1]
== make vector! [1 1 1]
red>> v1 or v2
== make vector! [1 3 3]
top alphanumeric-index category-index
The or~ word performs a logical "or" of its 2 arguments.Examples
The values are restricted to these types:
logic! integer! char! bitset! typeset! pair! tuple! vector!
It is the functional version of the infix word or.
In the case of or~ing two integer!s (or types closely related to integers,
such as char!, pair!), a bitwise (bit-by-bit) process is performed.
In general, types can be mixed, but logic! types cannot be mixed with other types.
The type of the result is the same type as the first value for a bitwise or~.
For a logic or~, true or false are returned.
red>> or~ (3 > 2) false
== true
red>> or~ 8 16
== 24
red>> or~ 16 false
*** Script error: logic type is not allowed here
*** Where: or~
red>> my-pair: 3x4
== 3x4
red>> or~ my-pair 1
== 3x5
red>> or~ 1 my-pair
*** Script error: or~ does not allow pair for its value2 argument
*** Where: or~
red>> v1: make vector![ 1 2 3]
== make vector! [1 2 3]
red>> v2: make vector![1 1 1]
== make vector! [1 1 1]
red>> or~ v1 v2
== make vector! [1 3 3]
top alphanumeric-index category-index
USAGE:
OS-INFO
DESCRIPTION:
Returns detailed operating system version information.
OS-INFO is a routine! value.
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
USAGE:
OVERLAP? A B
DESCRIPTION:
Return TRUE if the two faces bounding boxes are overlapping.
OVERLAP? is a function! value.
ARGUMENTS:
A [object!] "First face."
B [object!] "Second face."
RETURNS:
TRUE if overlapping.
[logic!]
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
The word pad pads a string! on the right side with spaces.Examples
The arguments are:
1. the string! to pad
2. the total size (integer!) of the new string!.
Refinements:
/left - pads the string! at its left side.
; Note the 4 (i.e. 6 - 2) spaces between B and X
red>> prin "X" prin pad "AB" 6 print "X" print "-123456-"
XAB X
-123456-
; Note the 4 spaces between the X and A
red>> prin "X" prin pad/left "AB" 6 print "X" print "-123456-"
X ABX
-123456-
top alphanumeric-index category-index
The datatype! pair! represents a scalar! type containingExamples
2 values.
A pair looks like:
axb
Pairs can be used to represent points in Cartesian space. (Coordinates)
The constituents of a pair! can be retrieved as follows:
- retrieve left value by either pairname/1 or pairname/x
- retrieve right value by either pairname/2 or pairname/y
; -------------------------------------------------------------------------
; Some examples
; -------------------------------------------------------------------------
red>> coord1: 10x20
== 10x20
red>> coord2: 100x100
== 100x100
red>> coord1 + coord2
== 110x120
red>> coord1 * coord2
== 1000x2000
red>> coord1/x
== 10
red>> coord1/1
== 10
red>> coord2/y: 234
== 234
red>> coord2
== 100x234
top alphanumeric-index category-index
The word pair? returns true if its argument is of datatype! pair!.
The argument can be of any type.
; Integer
red>> pair? 123
== false
; Tuple
red>> pair? 1.2.33
== false
; Pair
red>> pair? 22x33
== true
; Result of adding 2 pairs
red>> pair? 22x33 + 1x1
== true
top alphanumeric-index category-index
A paren! is a series! of items enclosed in parentheses ( ). It isExamples
evaluated when it is encountered, and a result is returned.
In Red, infix operators have no priority, and paren! types can sometimes add
clarity.
; -------------------------------------------------------------------------
; Work with infix operators
; -------------------------------------------------------------------------
red>> (1 + 2)
== 3
red>> 3 + ((4 + 5) * (2 + 3))
== 48
; -------------------------------------------------------------------------
; Preventing evaluation with e.g. [ ]
; -------------------------------------------------------------------------
red>> first [(2 + 3)]
== (2 + 3)
;a paren! is a series!
red>> first first [(2 + 3)]
== 2
;here 2 + 3 is evaluated first, so first tries to work on 5
red>> first (2 + 3)
*** Script Error: first does not allow integer! for its s argument
*** Where: first
top alphanumeric-index category-index
Returns true if its argument is a paren! type, otherwise false.Examples
The single argument can be of any type.
; -------------------------------------------------------------------------
; Exercise paren?
; -------------------------------------------------------------------------
; The paren! is evaluated first. Paren? 3 is false
red>> paren? (1 + 2)
== false
; the paren! is evaluated first
red>> paren? [(1 + 2)]
== false
;'first fetches a paren!
red>> paren? first [(1 + 2)]
== true
top alphanumeric-index category-index
USAGE:
PARSE input rules
DESCRIPTION:
Process a series using dialected grammar rules.
PARSE is a native! value.
ARGUMENTS:
input [binary! any-block! any-string!]
rules [block!]
REFINEMENTS:
/case => Uses case-sensitive comparison.
/part => Limit to a length or position.
length [number! series!]
/trace =>
callback [function! [event [word!] match? [logic!]
rule [block!] input [series!] stack [block!] return: [logic!]]]
RETURNS:
[logic! block!]
See the Red-by-example notes,at:
http://www.red-by-example.org/parse.html
top alphanumeric-index category-index
USAGE:
PARSE-TRACE input rules
DESCRIPTION:
Wrapper for parse/trace using the default event processor.
PARSE-TRACE is a function! value.
ARGUMENTS:
input [series!]
rules [block!]
REFINEMENTS:
/case => Uses case-sensitive comparison.
/part => Limit to a length or position.
limit [integer!]
RETURNS:
[logic! block!]
The same as parse but produces trace information.
See the Red-by-example notes,at:
http://www.red-by-example.org/parse.html
top alphanumeric-index category-index
A path! is used to access items, often those contained in largerExamples
structures. The path! syntax is basically a series of items (including
paren!) separated by '/', and is applicable to a range of data types.
It might help your understanding if you look at the lower-level
get-word!, set-word! and lit-word! types first. Here are some
introductory examples of path!.
; -------------------------------------------------------------------------
; with series - string and block
; -------------------------------------------------------------------------
red>> str: "ABCDE"
;get element 3
red>> str/3
== #"C"
;get element 2. NB b/2, not b / 2, which divides.
red>> b: [11 22 33 44]
red>> b/2
== 22
;with a paren! in a path
red>> b/(1 + 2)
== 33
red>> n: 1
red>> b/(n + 1)
== 22
;here, b has a nested block at position 4
red>> b: [11 22 33 [444 555] 66]
;get element 4, position 1
red>> b/4/1
== 444
;now with words, not literal numbers
red>> x: 4
red>> y: 1
;get element with a get-path! Note preceding : (more on this below)
red>> b/:x/:y
== 444
;set elementwith a set-path! Note following : (more on this below)
red>> b/:x/:y: 1111
== 1111
;note the changed value
red>> b
== [11 22 33 [1111 555] 66]
;with symbols - here, the value AFTER the symbol is returned
red>> sales: [UK 10000 USA 15000 China 33000]
red>> sales/USA
== 15000
; -------------------------------------------------------------------------
; Path Examples With Refinement
; -------------------------------------------------------------------------
Many function words use 'refinements' to modify their action. Here is
sort with its reverse refinement:
red>> ages: [55 33 73 73 19]
red>> sort/reverse ages
== [73 73 55 33 19]
Using Paths - set/get/lit Paths
-------------------------------
When we use colons or ticks in a path, the type of the path becomes a
set-path!, a get-path!, or a lit-path. This is similar in
concept to get-word!, set-word! and lit-word!.
If we have a block such as:
b: [11 22 33 44]
then we can use numbers in a straightforward way, as in:
b/2 ; get element at position 2
b/2: 2222 ; set element at position 2
With words used as variables, we must use set-path! and get-path!
types, as in:
red>> b/:x/:y ;get-path! type
red>> b/:x/:y: 1111 'set-path! type
We can also use a lit-path! type, as in:
'b/x
which evaluates to
b/x
Paths As Series
---------------
A path is a series, and its elements can be accessed with the series! functions.
Here, the first [block] is used to prevent evaluation
red>> second first [the/white/cat]
== white
top alphanumeric-index category-index
USAGE:
PATH-THRU url
DESCRIPTION:
Returns the local disk cache path of a remote file.
PATH-THRU is a function! value.
ARGUMENTS:
url [url!] "Remote file address."
RETURNS:
[file!]
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
The word path? returns true if its argument is a path! type.Examples
Otherwise false.
Its single argument can be of any type.
There are several types of path! related words:
path! lit-path! set-path! get-path!
To test if an argument is any of these types, use any-path?.
To restrict the test to path! only, use path?
For more details on the ways of interpreting a word (involving ' and :),
the reader should look at the entries on lit-word! get-word! and set-word!
; -------------------------------------------------------------------------
; With a get-path! type - false
; -------------------------------------------------------------------------
red>> path? first [:a/b/c]
== false
; -------------------------------------------------------------------------
; With a path! - true
; -------------------------------------------------------------------------
red>> path? first [a/b/c]
== true
top alphanumeric-index category-index
The datatype! percent! represents a number! type containingExamples
32 bit whole signed numbers.
; -------------------------------------------------------------------------
; Some examples
; -------------------------------------------------------------------------
red>> 35 * 10%
== 3.5
red>> 35 * (10.0 / 100) ; Exactly the same
== 3.5
red>> 100% / 40% ; Not so useful?
== 250%
top alphanumeric-index category-index
The word percent? returns true if its argument is a percent! type,Examples
otherwise false.
The argument can be any type.
red>> x: 100%
== 100%
red>> percent? x
== true
red>> y: 200
== 200
red>> percent? y
== false
top alphanumeric-index category-index
The word pi is a mathematical constant.Examples
It can be roughly approached by dividing 22 by 7.
red>> ? pi
pi is a float! of value: 3.141592653589793
red>> 22.0 / 7
== 3.142857142857143
top alphanumeric-index category-index
The word pick takes the nth value of a series!, where n is the second argumentExamples
; -------------------------------------------------------------------------
; Using a block! of numbers
; -------------------------------------------------------------------------
red>> x: [1 2 3 4 5 6 7 8]
== [1 2 3 4 5 6 7 8]
red>> y: pick x 4
== 4
red>> x
== [1 2 3 4 5 6 7 8]
red>> y
== 4
; -------------------------------------------------------------------------
; Using a string!
; -------------------------------------------------------------------------
red>> s: "abcdefghijklmnopqrstuvwxyz"
== "abcdefghijklmnopqrstuvwxyz"
red>> pick s 1
== #"a"
red>> pick s 26
== #"z"
red>> pick s 27
== none ; Index out of range
; -------------------------------------------------------------------------
; Using a block! of string!s
; -------------------------------------------------------------------------
red>> p: ["abc" "def" "ghi" "jkl" "mno" "pqr" "stu"]
== ["abc" "def" "ghi" "jkl" "mno" "pqr" "stu"]
red>> pick p 1
== "abc"
red>> pick p 4
== "jkl"
red>> pick p -1
== none ; Index out of range
top alphanumeric-index category-index
The word poke changes an element of a series! to a value.Examples
Arguments:
- name of the series!
- the index of the element to be replaced
- the new value for that element
; -------------------------------------------------------------------------
; Using a block! of numbers
; -------------------------------------------------------------------------
red>> x: [1 2 3 4 5 6 7 8 9]
== [1 2 3 4 5 6 7 8 9]
red>> poke x 3 66
== 66
red>> x
== [1 2 66 4 5 6 7 8 9]
red>> poke x 11 12
*** Script Error: value out of range: 11
*** Where: poke
; -------------------------------------------------------------------------
; using a string!
; -------------------------------------------------------------------------
red>> s: "abcdefghijklmnop"
== "abcdefghijklmnop"
red>> poke s 3 #"C"
== #"C"
red>> s
== "abCdefghijklmnop"
top alphanumeric-index category-index
The word positive? returns true if the numeric parameterExamples
is greater than 0, otherwise false.
NOTE that zero returns false.
Has one numeric parameter.
positive? -2
== false
positive? 0
== false
positive? 0.1
== true
top alphanumeric-index category-index
The word power returns the value of a number! raised to a given power (exponent).Examples
It is the functional version of the infix word **.
It has 2 arguments: a base value and a power, both number!s.
red>> power 3.1 2
== 9.61
red>> power 100.9 0.5
== 10.04489920307815
red>> power 2 10
== 1024
top alphanumeric-index category-index
The word prin outputs a value to the Red console in a similar manner toExamples
the print word, but without emitting an end-of-line: "^(line)".
For more details, look at print.
red>> prin 3 + 3 prin 4 + 4 print "done"
68done
top alphanumeric-index category-index
The word print outputs its evaluated argument to the Red console,Examples
followed by a new-line: "^(line)".
The single argument can be any type (also a block!). In this case
every block element is is evaluated. If you don't want evaluation,
investigate probe.
The twin word prin does the same but without outputting new-line.
red>> area: 3 * 2
== 6
red>> print area
6
red>> print area * 10
60
red>> print "Hello"
Hello
; Block is useful for printing several items
; Note: print will insert a space between the items in the block!
red>> print ["Area is" area "square units"]
Area is 6 square units
top alphanumeric-index category-index
The word probe returns its argument, after printing it in its molded form.Examples
Its argument can be any type. Blocks are not evaluated. This is useful for debugging.
Have a look at mold for specifics on the formatting.
; Behaviour of print
red>> print [1 + 2 44]
3 44
; Behaviour of probe and evaluation
red>> probe [1 + 2 44]
[1 + 2 44]
; Behaviour of mold
red>> probe [1 2 3]
[1 2 3]
== [1 2 3]
; Note the molded form
red>> probe #"A"
#"A"
== #"A"
; Same but now printed
red>> print probe #"A"
#"A"
A
top alphanumeric-index category-index
USAGE:
PUT series key value
DESCRIPTION:
Replaces the value following a key, and returns the new value.
PUT is an action! value.
ARGUMENTS:
series [series! map! object!]
key [scalar! any-string! any-word! binary!]
value [any-type!]
REFINEMENTS:
/case => Perform a case-sensitive search.
RETURNS:
[series! map! object!]
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
USAGE:
PWD
DESCRIPTION:
Displays the active directory path (Print Working Dir).
PWD is a function! value.
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
This word is a synonym for quittop alphanumeric-index category-index
USAGE:
QUERY target
DESCRIPTION:
Returns information about a file.
QUERY is an action! value.
ARGUMENTS:
target [file!]
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
The word quit quits the interpreter (and thus the program) immediately.Examples
Refinement:
/return p - When this refinement is used value p is returned by quit.
quit
; returns immediately to the OS command prompt
quit/return 3
; returns to the OS command prompt and hands the OS the value 3
top alphanumeric-index category-index
The word quit-return stops evaluation and exits the programExamples
with a given return code.
The argument is the return code, an integer!.
data: 1234
either data > 0
[quit-return 1]
[quit-return 0]
; This program returns 1 to the OS. This is the program's return code.
; Ways of handling this depend on the particular OS.
top alphanumeric-index category-index
To do by red-by-example team ...
The word random returns a random value of the same type as its single argument.Examples
For certain types and refinements, it can shuffle or pick from a series!.
For an integer! argument of value n, the random value is between 1 and n inclusive.
Refinements
/seed - Restart or randomize
/secure - Returns a cryptographically secure random number!
/only - Pick a random value from a series!
red>> random 2
== 1
red>> random 2
== 2
red>> random 1.0
== 0.4107365828988778
red>> random "fish"
== "ifhs"
red>> random/only "abcde"
== #"a"
red>> random [1 2 3 4]
== [2 1 4 3]
red>> random/only ["stone" "paper" "scissors"]
== "scissors"
top alphanumeric-index category-index
USAGE:
REACT reaction
DESCRIPTION:
Defines a new reactive relation between two or more objects.
REACT is a function! value.
ARGUMENTS:
reaction [block! function!] "Reactive relation."
REFINEMENTS:
/link => Link objects together using a reactive relation.
objects [block!] "Objects to link together."
/unlink => Removes an existing reactive relation.
src [word! object! block!] "'all word, or a reactor or a
list of reactors."
/later => Run the reaction on next change instead of now.
/with => Specifies an optional face object (internal use).
ctx [object! set-word! none!] {Optional context for VID
faces or target set-word.}
RETURNS:
The reactive relation or NONE if no relation was processed.
[block! function! none!]
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
USAGE:
REACT? reactor field
DESCRIPTION:
Returns a reactive relation if an object's field is a reactive source.
REACT? is a function! value.
ARGUMENTS:
reactor [object!] "Object to check."
field [word!] "Field to check."
REFINEMENTS:
/target => Check if it's a target instead of a source.
RETURNS:
Returns reaction, type or NONE.
[block! function! word! none!]
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
The word read reads and returns data from a file!.Examples
The argument must be a file! type.
Refinements:
/binary - reads, and preserves contents exactly
/lines - reads lines of text, placing them in a block! of string!s.
/info -
/part
/as
/seek
; The following example was executed on Windows, using a text file created with
; Notepad, and placed in the same folder as the program. The file contains 2 lines:
; LINE1
; LINE2
; Inside the file, the end of each line is marked by a CRLF pair.
; However, when Red reads the data as text, it presents the data to us with only
; the LF character at the end, just as on Linux.
; 1) read a file as strings
text: read %read-data.txt
print length? text ; 12 characters
12
prin text ; Displays all the lines. No extra newline
LINE1
LINE2
; 2) read a file as binary
bin: read/binary %read-data.txt
print length? bin ; 14 bytes
14
print bin ; In hex format
#{4C494E45310D0A4C494E45320D0A}
; 3) read individual lines (strings)
text: read/lines %read-data.txt
print length? pick text 1 ; Length is 5 - no LF
5
print pick text 2 ; Print second line
LINE
top alphanumeric-index category-index
USAGE:
READ-CLIPBOARD
DESCRIPTION:
Return the contents of the system clipboard.
READ-CLIPBOARD is a routine! value.
; -------------------------------------------------------------------------
; write and read clipboard
; -------------------------------------------------------------------------
>> write-clipboard "Hello!!!"
== true
>> read-clipboard
== "Hello!!!"
top alphanumeric-index category-index
USAGE:
READ-THRU url
DESCRIPTION:
Reads a remote file through local disk cache.
READ-THRU is a function! value.
ARGUMENTS:
url [url!] "Remote file address."
REFINEMENTS:
/update => Force a cache update.
/binary => Use binary mode.
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
REBOL is a logic! value: false
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
USAGE:
RECYCLE
DESCRIPTION:
RECYCLE is a native! value.
REFINEMENTS:
/on =>
/off =>
Triggers a manual garbage collection pass, rather than waiting for internal
heuristics and timing events to do so. Recycle has 2 refinements: /off and /on.
They allow you to disable the garbage collector, and re-enable it. This
gives you control in scenarios where you want to manage things more directly
yourself.
top alphanumeric-index category-index
USAGE:
RED-COMPLETE-INPUT str console?
DESCRIPTION:
RED-COMPLETE-INPUT is a function! value.
ARGUMENTS:
str [string!]
console? [logic!]
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
The word reduce returns a copy of a block!, evaluating all expressionsExamples
in the block!.
Its single argument can be of any type.
See also: rejoin - which means 'reduce and join'.
Refinements
/into - puts results into a provided block!, instead of creating a new one.
red>> reduce[1 + 2 5 * 6]
[3 30]
red>> b: reduce[1 + 5]
[6]
red>> b
[6]
; -------------------------------------------------------------------------
; Use the /into refinement
; -------------------------------------------------------------------------
red>> a-block: []
[]
red>> reduce/into [4 5 + 6] a-block
[]
red>> a-block
[4 11]
; -------------------------------------------------------------------------
; Evaluating can also mean assignment
; -------------------------------------------------------------------------
red>> var: 3
3
red>> reduce [ 3 * 3 var: 22]
[9 22]
red>> var
22
; -------------------------------------------------------------------------
; Single values, variables or expressions (not in a block)
; -------------------------------------------------------------------------
red>> reduce 123
123
red>> reduce 1 + 2
3
top alphanumeric-index category-index
A refinement! datatype! indicates a variation in the use of, orExamples
extension in the meaning of, a function!, object!, file!, url!, or
path!. The exact usage of a refinement varies. For example,
function refinements can be written so they require additional
arguments. We might also use several refinements at once. Refinements
start with a /, followed by a valid Red word. Below, we use the
refinement? function to examine some possibilities.
; -------------------------------------------------------------------------
; try refinement?
; -------------------------------------------------------------------------
red>> refinement? /part
== true
red>> refinement? /45
== true
red>> refinement? /:b ; this is a set-word! type
== false
; -------------------------------------------------------------------------
; refinement with function. The 3 is the argument of /part
; -------------------------------------------------------------------------
red>> s: [8 7 6 5 4 3 2]
red>> sort/part s 3
== [6 7 8 5 4 3 2] ; Only the first 3 items were sorted
; -------------------------------------------------------------------------
; refinement with an object
; -------------------------------------------------------------------------
red>> print system/version
== 0.6.1
; -------------------------------------------------------------------------
; refinement as a path to a block element
; -------------------------------------------------------------------------
red>> b: [11 22 33 44]
red>> b/2
== 22
top alphanumeric-index category-index
The refinement? word returns true if the single argument is aExamples
refinement, otherwise false.
Note that the argument is not checked against all existing refinements,
merely that it fits the syntax for a refinement.
The single argument can be of any type.
red>> refinement? /all
true
red>> refinement? /nonsense
true
red>> refinement? /123
true
; -------------------------------------------------------------------------
; Invalid: string is not a refinement
; -------------------------------------------------------------------------
red>> refinement? "/abc"
false
red>> r: "/abc"
"/abc"
red>> refinement? r
false
; -------------------------------------------------------------------------
; Valid: a refinement can be stored in a variable
; -------------------------------------------------------------------------
red>> r: /abc
== /abc
red>> refinement? r
== true
top alphanumeric-index category-index
Returns internal details about a value via reflection.Examples
Arguments
value - of a type that supports reflection
field - a word! such as spec, body, words, etc. Each datatype!
defines its own reflectors.
; -------------------------------------------------------------------------
; reflect with a map: 'body, 'values
; -------------------------------------------------------------------------
red>> m: make map![a 1 b 2]
red>> reflect m 'body
== [a: 1 b: 2]
red>> reflect m 'values
== [1 2]
; -------------------------------------------------------------------------
; reflect with object: 'body 'values 'words
; -------------------------------------------------------------------------
red>> place: make object! [
x-pos: 100
y-pos: 200
]
red>> reflect place 'body
== [x-pos: 100 y-pos: 200]
red>> reflect place 'values
== [100 200]
red>> reflect place 'words
== [x-pos y-pos]
top alphanumeric-index category-index
Accepts a block of items, reduces them, and joins them together. No
spaces are inserted between items. The rejoined item is returned, and the
original block is unaltered.
REBOL users please note - Red does not have 'join'.
Arguments
A block.
Examples
; -------------------------------------------------------------------------
; Some simple rejoins - note the string! result
; -------------------------------------------------------------------------
>> b: ["sum is " 3 + 2]
== ["sum is " 3 + 2]
>> rejoin b
== "sum is 5"
>> type? rejoin b
== string!
>> b2: [3 + 4 " is result"]
== [3 + 4 " is result"]
>> rejoin b2
== "7 is result"
; -------------------------------------------------------------------------
; Join some blocks - note the appended series
; -------------------------------------------------------------------------
>> rejoin [b b2]
== ["sum is " 3 + 2 [3 + 4 " is result"]]
; -------------------------------------------------------------------------
; The tag! type acts differently. The result is a tag! type
; -------------------------------------------------------------------------
>> rejoin [<a href=test.html> "test" </a>]
>> <a href=test.htmltest</a>>
;-- we could convert to strings to get a more useful result:
>> rejoin ["<a href=test.html>" "test" </a>]
== "<a href=test.html>test</a>"
top alphanumeric-index category-index
The word remainder returns the rest of dividing the first by the second value.Examples
Has 2 arguments of type:
number! char! pair! tuple! vector!
It is the functional version of the infix word //.
If the first argument is positive, then the remainder is positive.
If the first argument is negative, then the remainder is negative,
as in remainder -5 4, which results in -1.
If the first argument is zero, then the remainder is also zero.
If the second argument is zero, a run-time error occurs.
red>> remainder 5 4
== 1
red>> remainder 5 5
== 0
red>> remainder 5 6
== 5
; 5 divided by 6 gives zero
; with remainder of 5
red>> remainder -5 4
== -1
red>> remainder 10 3.3
== 0.1000000000000005
red>> remainder 5x10 4
== 1x2
red>> remainder make vector![5 5 10] 4
== make vector! [1 1 2]
top alphanumeric-index category-index
The word remove removes the first value(s) from a series!Examples
and returns the modified series! from the removal point onwards.
The single argument can be of type:
series!
bitset!
none!
Refinements
/part - removes a number of values, or values up to the
given index.
This refinement requires one of:
number!
char!
series!
Note that for a bitset! argument, the /part refinement is required.
Note that remove is destructive. It changes the series! involved!!
; -------------------------------------------------------------------------
; Setup 2 variables to be used in the examples
; -------------------------------------------------------------------------
red>> data: [22 11 44 66 10]
== [22 11 44 66 10]
red>> s: "abcdefg"
== "abcdefg"
; -------------------------------------------------------------------------
; Remove first item of a series!
; -------------------------------------------------------------------------
red>> remove data
== [11 44 66 10]
red>> data
== [11 44 66 10]
; -------------------------------------------------------------------------
; Remove first item of a string!
; -------------------------------------------------------------------------
red>> remove s
== "bcdefg"
red>> s
== "bcdefg"
; -------------------------------------------------------------------------
; Remove the first 2 items using /part
; -------------------------------------------------------------------------
red>> remove/part data 2
== [66 10]
red>> data
== [66 10]
red>> remove/part s 2
== "defg"
red>> s
== "defg"
; -------------------------------------------------------------------------
; Bitset (E.g. hex code for "A" is 65, so the 65th bit is set)
; -------------------------------------------------------------------------
red>> b: make bitset![#"A" #"N"]
make bitset! #{00000000000000004002}
red>> remove/part b #"N"
make bitset! #{00000000000000004000}
; -------------------------------------------------------------------------
; Use /part with a series! index
; -------------------------------------------------------------------------
red>> nums: [1 2 3 4 5 6 7 8]
== [1 2 3 4 5 6 7 8]
red>> ref: skip nums 4
== [5 6 7 8]
red>> remove/part nums ref
== [5 6 7 8]
red>> nums
== [5 6 7 8]
red>> ref
== []
top alphanumeric-index category-index
Removes one or more values from a series!.Examples
A block is evaluated for each element, and the element is removed
if the block is true. The original series is modified.
Arguments
word - a word! or block! of words to set.
These are used in the evaluation. They are not local.
data - a series!, which includes string!
body - a block! to be evaluated.
;remove-each, with only one 'word' argument
;make a series s to work with
red>> s: [3 6 "text" 12 15 "end"]
;remove each string element
red>> remove-each this-item s [string? this-item]
;note the modified series
red>> s
== [3 6 12 15]
;remove each item > 6
red>> remove-each this-item s [this-item > 6]
;note modified series
red>> s
== [3 6]
;a string of characters - remove all spaces
red>> text: "a few words with spaces"
red>> remove-each ch text [ch = #" "]
;check the result
red>> text
== "afewwordswithspaces"
top alphanumeric-index category-index
The word repeat evaluates a given block! a specified number of timesExamples
and exposes the count value to the block! being executed.
Has 3 parameters:
1. name of a count variable, which contains the current value of the counter
2. number of times the block! should be evaluated
3. the block! to be evaluated
Warning:
1. the count variable can only be referred within the block!;
so, if before the repeat a variable exists with the same name,
that variable will be untouched after repeat
2. when repeat introduces a unique name for count, that name
cannot be referenced anymore after repeat
counter: 33
== 33
repeat counter 4 [print ["Evaluation #" counter]]
Evaluation # 1
Evaluation # 2
Evaluation # 3
Evaluation # 4
>> counter
== 33
repeat x 3 [print ["Evaluation #" x]]
Evaluation # 1
Evaluation # 2
Evaluation # 3
>> x
** Script Error: x has no value
** Near: x
repeat y 5 [y: y - 1 print ["y =" y]]
y = 0
y = 1
y = 2
y = 3
y = 4
top alphanumeric-index category-index
Appends a reduced value to a series and returns the series head.Examples
The original series is modified.
Arguments
- a series!
- a value to be appended, of any type.
Refinements
/only - Appends a block value as a block.
; -------------------------------------------------------------------------
; repend a block - note the evaluation
; -------------------------------------------------------------------------
;make a series to work with
red>> s: [10 "eleven" 12]
red>> repend s [7 + 7 "fifteen"]
== [10 "eleven" 12 14 "fifteen"]
;s has changed
red>> s
== [10 "eleven" 12 14 "fifteen"]
; -------------------------------------------------------------------------
; /only refinement - note nested block
; -------------------------------------------------------------------------
red>> repend/only s [7 + 7 "fifteen"]
== [10 "eleven" 12 14 "fifteen" [14 "fifteen"]]
top alphanumeric-index category-index
The word replace replaces the search-value with the replace valueExamples
in a series!.
It can be used with strings, because a string! is a series!.
It has 3 arguments:
series - the series! to be modified.
search-value - the value to be replaced (any type)
replace-value - the value to replace with (any type)
The search is case-insensitive and only the first occurrence is replaced,
unless the refinement /all is used.
The replaced and replacing item can have a different length.
Refinements
/all - replace all occurrences
Note that this is a destructive operation. It alters the series!
involved!
; -------------------------------------------------------------------------
; Replace a by W
; -------------------------------------------------------------------------
red>> s: "abcde"
"abcde"
red>> replace s "a" "W"
"Wbcde"
red>> s
"Wbcde"
; -------------------------------------------------------------------------
; Note the case insensitivity here
; -------------------------------------------------------------------------
red>> a-string: "The cat and the dog"
"The cat and the dog"
red>> replace a-string "the" "a"
"a cat and the dog"
; -------------------------------------------------------------------------
; Using the /all refinement
; -------------------------------------------------------------------------
red>> river: "Mississippi"
"Mississippi"
red>> replace/all river "is" "IS"
"MISsISsippi"
; -------------------------------------------------------------------------
; A series of numbers
; -------------------------------------------------------------------------
red>> nums: [21 22 23 -1 44 45 46]
[21 22 23 -1 44 45 46]
red>> replace nums -1 [33 34 35]
[21 22 23 33 34 35 44 45 46]
red>> replace nums [21 22 23] -1
[-1 33 34 35 44 45 46]
top alphanumeric-index category-index
The word return returns a value from a function.Examples
It needs one argument, of any type.
In functions which don't have a return value, you can use the word exit.
Under special conditions it is useful to return the value none.
The value of a block! is the value of last evaluation that takes place in it,
so this can be used to return a value, without using the return word.
If we want to return a value partway through a block!, or want to make it
more explicit for a reader of the code, we also can use return.
bigger: func [a b] [
either a > b [
return a
][
return b
]
]
print bigger 8 6
8
top alphanumeric-index category-index
The word reverse reverses the order of elements in its argument.Examples
The argument will be changed. Returns at the same position in the argument.
The argument can be a:
series!
pair!
tuple!
Refinements
/part - limits to a given length or position (number! series!)
Note:
- for series! reverse is destructive
- but not for tuple! and pair!
; -------------------------------------------------------------------------
; Reverse a block! series!
; -------------------------------------------------------------------------
red>> b: [1 2 3 4]
== [1 2 3 4]
red>> reverse b
== [4 3 2 1]
red>> b
== [4 3 2 1]
red>> s: "ABCD"
== "ABCD"
red>> reverse s
== "DCBA"
red>> s
== "DCBA"
; -------------------------------------------------------------------------
; Reverse a section of a string! series!
; -------------------------------------------------------------------------
red>> words: "The black cat"
== "The black cat"
red>> reverse find words "cat"
== "tac"
red>> words
== "The black tac"
; -------------------------------------------------------------------------
; Use refinement /part - reverse first 3
; -------------------------------------------------------------------------
red>> s: "abcdefg"
== "abcdefg"
red>> reverse/part s 3
== "cbadefg"
red>> s
== "cbadefg"
; -------------------------------------------------------------------------
; Reverse all values before value 50
; -------------------------------------------------------------------------
red>> b: [10 20 30 40 50 60 70]
== [10 20 30 40 50 60 70]
red>> reverse/part b find b 50
== [40 30 20 10 50 60 70]
red>> b
== [40 30 20 10 50 60 70]
; -------------------------------------------------------------------------
; Reverse a tuple - note unchanged value of t afterwards!
; -------------------------------------------------------------------------
red>> t: 11.22.33.44.55
== 11.22.33.44.55
red>> reverse t
== 55.44.33.22.11
red>> t
== 11.22.33.44.55
; -------------------------------------------------------------------------
; Reverse a pair - note unchanged value of p afterwards!
; -------------------------------------------------------------------------
red>> p: 100x200
== 100x200
red>> reverse p
== 200x100
red>> p
== 100x200
top alphanumeric-index category-index
Returns the nearest integer value. Halves round up (away from zero) byExamples
default. Note that refinements allow decimal places (etc) to be specified.
Arguments
The single argument can be a number! or a time!
Refinements
/to - Return the nearest multiple of the scale parameter.
We supply a non-zero number!
/even - Halves round toward even results.
/down - Round toward zero, ignoring discarded digits - i.e. truncate.
/half-down - Halves round toward zero, not away from zero.
/floor - Round in negative direction.
/ceiling - Round in positive direction.
/half-ceiling - Halves round in positive direction.
; -------------------------------------------------------------------------
; round - no refinements
; -------------------------------------------------------------------------
;basic use
red>> round 2.3
== 2.0
;note -ve - round toward zero
red>> round -3.3
== -3.0
;halves round away from zero
red>> round 2.5
== 3.0
red>> round -3.5
== -4.0
;time argument - note rounded seconds
red>> round 13:14:15.6
== 13:14:16
; -------------------------------------------------------------------------
; /to refinement
; -------------------------------------------------------------------------
red>> round/to 12.36 0.1
== 12.4
red>> round/to 2888 100
== 2900
; -------------------------------------------------------------------------
; /even refinement
; -------------------------------------------------------------------------
;towards even: 4 not 5
red>> round/even 4.5
== 4.0
;towards even: 4 not 3
red>> round 3.5
== 4.0
red>> round 4.5
== 5.0
; -------------------------------------------------------------------------
; /down refinement
; -------------------------------------------------------------------------
red>> round/down 3.9
== 3.0
red>> round/down -3.9
== -3.0
; -------------------------------------------------------------------------
; /half-down refinement
; -------------------------------------------------------------------------
red>> round/half-down 2.5
== 2.0
red>> round/half-down -3.5
== -3.0
; -------------------------------------------------------------------------
; /floor refinement
; -------------------------------------------------------------------------
red>> round/floor 3.9
== 3.0
red>> round/floor -3.9
== -4.0
; -------------------------------------------------------------------------
; /ceiling refinement
; -------------------------------------------------------------------------
red>> round/ceiling 3.2
== 4.0
red>> round/ceiling -3.9
== -3.0
; -------------------------------------------------------------------------
; /half-ceiling refinement
; -------------------------------------------------------------------------
red>> round/half-ceiling 3.5
== 4.0
red>> round/half-ceiling -3.5
== -3.0
top alphanumeric-index category-index
USAGE:
ROUTINE spec body
DESCRIPTION:
Defines a function with a given Red spec and Red/System body.
ROUTINE is a function! value.
ARGUMENTS:
spec [block!]
body [block!]
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
The datatype! routine! is used to link up to external code.Examples
Once a library has been loaded, it is possible to create a routine!
type to access the external function from Red.
You can use make to create a new, named routine!.
; -------------------------------------------------------------------------
; Use make to create a routine!
; -------------------------------------------------------------------------
; red-routine-name: make routine! specs library-name func-id
; -------------------------------------------------------------------------
; Docs to be extended by red-by-example team ...
; -------------------------------------------------------------------------
top alphanumeric-index category-index
The word routine? returns true if its argument is a routine!,Examples
otherwise false.
A routine! is a function in an external library.
Its single argument can be of any type.
; -------------------------------------------------------------------------
; The word cos is external
; -------------------------------------------------------------------------
red>> routine? :cos ; Note the colon!
true
; -------------------------------------------------------------------------
; The word sort is not in an external library
; -------------------------------------------------------------------------
red>> routine? :sort
false
top alphanumeric-index category-index
The word same? returns true if the arguments refer toExamples
identical objects, otherwise false.
For example, true would be returned if two strings are the
same string (that is: they occupy the same memory location).
The two arguments can be of any type.
; -------------------------------------------------------------------------
; Same string, but in differnet memory locations
; -------------------------------------------------------------------------
red>> a: "fish"
"fish"
red>> b: "fish"
"fish"
red>> same? a b
false
; -------------------------------------------------------------------------
; Same string and same memory location
; -------------------------------------------------------------------------
red>> a: "meat"
"meat"
red>> b: a
"meat"
red>> same? a b
true
top alphanumeric-index category-index
The word save is used to save Red code to a file!, url!,Examples
binary! or string!.
The load word is intended for reading back items created with save.
Note that write is intended for storing data, not code.
Red uses brackets [ ] { } and quotes " " etc. to enclose data items.
The items might or might not be code. In a sense they become code when
we instruct Red to interpret them. Because of this, save can be
used to save data as well as code. In fact, there is no difference.
Red has the same adagium as Lisp: ; Code = Data.
The word save has 2 arguments:
where to save - one of
file! url! binary! string! none!
value - the value(s) to save
Refinements:
/header - provide a Red header block (or output non-code datatypes).
This requires header-data (a block!) or an object!
/all - save in serialized format. (Not available yet)
/length - save the length of the script content in the header
/as - specify the format of data; use NONE to save as plain text;
format [word! none!] => E.g. json, html, jpeg, png, redbin etc.
; -------------------------------------------------------------------------
; Save 2 lines of code in a file mydata.r
; -------------------------------------------------------------------------
red>> code: [
[ print "Hello"
[ print "Goodbye"
[ ]
== [
print "Hello"
print "Goodbye"
]
red>> save %mydata.r code
; -------------------------------------------------------------------------
; Load the just saved data back
; -------------------------------------------------------------------------
red>> load %mydata.r
[print "Hello"
print "Goodbye"
]
; -------------------------------------------------------------------------
; We can use do to interpret the code in the file
; -------------------------------------------------------------------------
red>> do %mydata.r
Hello
Goodbye
red>> do code
Hello
Goodbye
; -------------------------------------------------------------------------
; Save some structured data
; -------------------------------------------------------------------------
red>> data: [ [1 10] [2 20] 30]
== [[1 10] [2 20] 30]
red>> save %mydata.r data
; -------------------------------------------------------------------------
; Load that data back
; -------------------------------------------------------------------------
red>> load %mydata.r
== [[1 10] [2 20] 30]
top alphanumeric-index category-index
scalar! is a typeset! value:
make typeset! [char! integer! float! pair! percent! tuple! time! date!]
top alphanumeric-index category-index
Returns true if the value is any type of scalar!.
Arguments
One, which can be any-type!
Examples
; -------------------------------------------------------------------------
; Explore several types
; -------------------------------------------------------------------------
>> scalar? "abc" ;-- a string is a series type, not scalar
== false
>> scalar? 24
== true
>> scalar? 2.3
== true
>> scalar? 20x30
== true
top alphanumeric-index category-index
The word second returns the second value of a series!.Examples
The argument can be one of:
series! tuple! pair!
If there is no second item in the series!, none is returned.
; -------------------------------------------------------------------------
; Second of a block!
; -------------------------------------------------------------------------
red>> second [3 4 5]
4
; -------------------------------------------------------------------------
; Second of a string! (a char!)
; -------------------------------------------------------------------------
red>> second "ABC"
#"B"
; -------------------------------------------------------------------------
; A series! having no second element
; -------------------------------------------------------------------------
red>> second [44]
none
; -------------------------------------------------------------------------
; Second of a tuple!
; -------------------------------------------------------------------------
red>> second 66.77.88
77
; -------------------------------------------------------------------------
; Second of a pair!
; -------------------------------------------------------------------------
red>> second 22x55
55
top alphanumeric-index category-index
The word select finds a value in a series! and if succesful returnsExamples
the element right after the element found. Returns none if the search fails.
Select is similar to the find function.
The 2 arguments are:
a series - of type series! any-object! map! none!
a value - of any type
Refinements
/part - Limit the length (a number!) of the search or a series! reference.
/only - Treat a series search value as a single value.
/case - Perform a case-sensitive search.
/any - Use * and ? wildcards in string searches.
/with - Use custom wildcards * or ?.
/skip - Treat the series as fixed size records. Requires an integer! size.
/last - Find the last occurrence of value (from the tail).
/reverse - Find the last occurrence of value (from the current index).
; -------------------------------------------------------------------------
; Select a month, return item following it
; -------------------------------------------------------------------------
red>> months: ["Mar" 31 "April" 30 "May" 31]
== ["Mar" 31 "April" 30 "May" 31]
red>> select Months "April"
== 30
red>> select Months 30
== "May" ; Beware that select can match any element!
; -------------------------------------------------------------------------
; Select a character in a string! (after D)
; -------------------------------------------------------------------------
red>> str: "ABCDEFGH"
== "ABCDEFGH"
red>> select str "D"
== #"E" ; Because E follows the found element D
; -------------------------------------------------------------------------
; /pairs - do a normal select, then use /pairs
; -------------------------------------------------------------------------
red>> pairs: [44 4 3 33 4 55]
== [44 4 3 33 4 55]
red>> select pairs 4
== 3 ; Because 3 follows the found element 4
; -------------------------------------------------------------------------
; Select only matches the first element in a skip group
; -------------------------------------------------------------------------
red>> select/skip pairs 4 2
== 55
; -------------------------------------------------------------------------
; Search form the tail
; -------------------------------------------------------------------------
red>> animal: "The black cat"
== "The black cat"
red>> select/last animal "a"
== #"t"
; -------------------------------------------------------------------------
; Using the /only refinement
; -------------------------------------------------------------------------
red>> blk: [11 12 13 [22 33 44] 15]
== [11 12 13 [22 33 44] 15]
red>> select blk 13
== [22 33 44]
red>> select blk [22 33 44]
== none ; Not found, because we searched for a series!
red>> select/only blk [22 33 44]
15
red>> select/only blk [22 33 44]
== 15 ; Success! With /only we can search for a series!
; -------------------------------------------------------------------------
; Search backwards from a position with the /reverse refinement
; -------------------------------------------------------------------------
red>> words: "a-few-words"
== "a-few-words"
red>> current: find/last words "-"
"-words"
red>> current: find/last words "-"
== "-words"
red>> index? current
== 6
red>> select/reverse current "-"
== #"f"
top alphanumeric-index category-index
The datatype! series! is a typeset! containing several datatype!s.Examples
; -------------------------------------------------------------------------
; Show all series! types
; -------------------------------------------------------------------------
red>> ? series!
series! is a typeset! of value:
make typeset! [
block!
paren!
string!
file!
url!
path!
lit-path!
set-path!
get-path!
vector!
hash!
binary!
image!
]
top alphanumeric-index category-index
word series? returns true if its argument is aExamples
series!, otherwise false.
The argument can be of any type.
The series! type includes:
block!
paren!
string!
file!
url!
path!
lit-path!
set-path!
get-path!
vector!
hash!
binary!
image!
; -------------------------------------------------------------------------
; A block!
; -------------------------------------------------------------------------
red>> series? [1 "text"]
true
; -------------------------------------------------------------------------
; A tuple!
; -------------------------------------------------------------------------
red>> series? 12.33.12
false
; -------------------------------------------------------------------------
; A string!
; -------------------------------------------------------------------------
red>> series? "ABC"
true
; -------------------------------------------------------------------------
; A float!
; -------------------------------------------------------------------------
red>> series? 12.34
false
top alphanumeric-index category-index
USAGE:
SET word value
DESCRIPTION:
Sets the value(s) one or more words refer to.
SET is a native! value.
ARGUMENTS:
word [any-word! block! object! path!] "Word, object, map path
or block of words to set."
value [any-type!] "Value or block of values to assign to words."
REFINEMENTS:
/any => Allow UNSET as a value rather than causing an error.
/case => Use case-sensitive comparison (path only).
/only => Block or object value argument is set as a single value.
/some => None values in a block or object value argument, are
not set.
RETURNS:
[any-type!]
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
USAGE:
SET-CURRENT-DIR path
DESCRIPTION:
Sets the platform’s current process directory.
SET-CURRENT-DIR is a routine! value.
ARGUMENTS:
path [string!]
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
USAGE:
SET-ENV var value
DESCRIPTION:
Sets the value of an operating system environment variable (for current process).
SET-ENV is a native! value.
ARGUMENTS:
var [any-string! any-word!] "Variable to set."
value [string! none!] "Value to set, or NONE to unset it."
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
A set-path! is a kind of path! used to change (set) a value. ReferExamples
to path! and set-word! for more details.
; -------------------------------------------------------------------------
; Use a set-path!, and check the type.
; -------------------------------------------------------------------------
red>> b:[11 22 33 44]
;note the trailing colon
red>> b/2: -999
== -999
;note the changed value
red>> b
== [11 -999 33 44]
;now with a variable
red>> place: 3
== 3
;the first : gets the value of place, second makes a set-path!
red>> b/:place: -1000
== -1000
red>> b
== [11 -999 -1000 44]
;Find the type - first [block] is used to prevent evaluation
red>> type? first [b/:place:]
== set-path!
top alphanumeric-index category-index
This word returns true if its argument is a set-path! type, otherwise false.Examples
Its single argument can be any type.
; -------------------------------------------------------------------------
; use set-path? on various types
; -------------------------------------------------------------------------
;create a block and position to work with
red>> b: [11 22 33]
red>> n: 2
;get-path? with an evaluated path
red>> set-path? b/2:
*** Script Error: b/2: needs a value
*** Where: set-path?
;now use first [ block ] to prevent evaluation
red>> set-path? first [b/2:]
== true
red>> set-path? first [b/:n:]
== true
;this is a path!
red>> set-path? first [b/n]
== false
top alphanumeric-index category-index
USAGE:
SET-QUIET word value
DESCRIPTION:
Set an object's field to a value without triggering object's events.
SET-QUIET is a routine! value.
ARGUMENTS:
word [any-type!]
value [any-type!]
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
Red has a notation which modifies how a word is evaluated. The colon andExamples
single quote mark ('tick') are used. For example, for a word w, we have:
w: - sets the word to a value. Its type is set-word!
:w - gets the word's value, but does not evaluate it. Its type is
get-word!
'w - treat the word as a symbol, with no evaluation. Its type is
lit-word! (i.e. literal word).
w - normal evaluation. If the word is a function, evaluate it.
Note that : and ' are not functions. Attaching them to a word changes the
type of the word, and how it is used.
We can use the set-word? , get-word?, lit-word? functions to
test whether a word is one of these types.
The set-word! type sets a word to a value. It is tempting for beginners
to assume that a colon is like an assignment operator in other languages, but
this is not the case. It specifies the use of a set-word! type, which expects
a value.
; -------------------------------------------------------------------------
; Use a set-word! type
; -------------------------------------------------------------------------
;provide a value
red>> a-word: 123
;a-word set to 999, then w set to a-word's value, 999
red>> w: a-word: 999
;the type of an unevaluated set-word
red>> type? first [ a: ]
== set-word!
top alphanumeric-index category-index
Returns true if its argument is a set-word! type, otherwise false.Examples
Its single argument can be of any type.
; -------------------------------------------------------------------------
; A few types of argument
; -------------------------------------------------------------------------
red>> a-word: 123
;just a normal word
red>> set-word? a-word
== false
;correct set-word! syntax, but its value is not a reference
red>> set-word? a-word:
*** Script Error: a-word: needs a value
*** Where: a-word
;prevent evaluation with first [block]
red>> set-word? first [a-word:]
== true
;a get-word!
red>> set-word? first [:a-word]
== false
top alphanumeric-index category-index
The word shift performs an arithmetical bit shift operation,Examples
preserving the sign.
By default it shifts to the right, but left shifting is also possible.
A single right shift divides by 2.
A single left shift multiplies by 2.
Also logical shifting is possible.
It has 2 arguments:
- an integer! to be shifted
- an integer! stating how many places to shift
Note: the infix << >> >>> operators provide similar facilities.
Refinements
/left - shift bits to the left instead of to the right
/logical - use logical shift (unsigned, fill with zeroes)
; -------------------------------------------------------------------------
; Right-shift a positive number
; -------------------------------------------------------------------------
red>> shift 64 2
== 16
red>> to-hex 64
== #00000040
red>> to-hex 16
== #00000010
; -------------------------------------------------------------------------
; Right-shift a negative number, preserving sign
; -------------------------------------------------------------------------
red>> shift -1 4
== -1
red>> to-hex -1
== #FFFFFFFF ; Left most bit stays 1 (= negative)
; -------------------------------------------------------------------------
; Left-shift a positive number
; -------------------------------------------------------------------------
red>> shift/left 64 2
== 256
red>> to-hex 64
== #00000040
red>> to-hex 256
== #00000100
; -------------------------------------------------------------------------
; Left-shift a negative number, preserving sign
; -------------------------------------------------------------------------
red>> shift/left -1 4
== -16
red>> to-hex -1
== #FFFFFFFF
red>> to-hex -16
== #FFFFFFF0
; -------------------------------------------------------------------------
; Logical right-shift, no sign preservation
; -------------------------------------------------------------------------
red>> shift/logical -1 4
== 268435455
red>> to-hex -1
== #FFFFFFFF
red>> to-hex 268435455
== #0FFFFFFF
top alphanumeric-index category-index
The word shift-left performs a logical shift-left operation.Examples
The sign-bit is not preserved.
A single shift-left multiplies by 2.
It has 2 arguments:
- an integer! value to be shifted
- an integer! stating how many places to shift.
Note that the infix words << >> >>> provide similar
facilities.
; -------------------------------------------------------------------------
; Normal shift-left
; -------------------------------------------------------------------------
red>> to-hex 64
== #00000040
red>> shift-left 64 2
256
red>> to-hex shift-left 64 2
== #00000100
; -------------------------------------------------------------------------
; Sign bit changed by shift ...
; -------------------------------------------------------------------------
red>> to-hex 1
== #00000001
red>> to-hex shift-left 1 31
== #80000000 ; Left most bit (sign bit) changed
red>> shift-left 1 31
== -2147483648 ; See, it it negative!
red>> to-hex shift-left 1 32
== #00000001 ; Wrapped around by 32 bits now
top alphanumeric-index category-index
The word shift-logical performs a logical right shift.Examples
The sign is not preserved.
It has 2 arguments:
- an integer! value to be shifted
- an integer! stating how many bitss to shift
Note that the infix << >> >>> operators provide similar facilities.
; -------------------------------------------------------------------------
; Shift (right) by 2 bits
; -------------------------------------------------------------------------
red>> to-hex 64
== #00000040 ; Last byte = binary 0100 0000
red>> shift-logical 64 2
== 16
red>> to-hex shift-logical 64 2
== #00000010 ; Last byte = binary 0001 0000
; -------------------------------------------------------------------------
; Shift (right) by 1 bit - note zero entering at the left
; -------------------------------------------------------------------------
red>> to-hex -1
== #FFFFFFFF ; First byte = binary 1111 1111
red>> shift-logical -1 1
== 2147483647
red>> to-hex shift-logical -1 1
== #7FFFFFFF ; First byte = binary 0111 1111
top alphanumeric-index category-index
The word shift-right performs a right-shift operation.Examples
A single shift-right divides by 2.
It has 2 arguments:
- an integer! value to be shifted
- an integer! stating how many places to shift
Note that the infix << >> >>> operators provide
similar facilities.
; -------------------------------------------------------------------------
; Normal shift
; -------------------------------------------------------------------------
red>> shift-right 16 2
== 4
; -------------------------------------------------------------------------
; Negative sign (left most bit) will be preserved
; -------------------------------------------------------------------------
to-hex -2147483648
== #80000000 ; #80 is binary 1000 0000
red>> shift-right -2147483648 1
== -1073741824
red>> to-hex -1073741824
== #C0000000 ; #C0 is binary 1100 0000
top alphanumeric-index category-index
docstartExamples
USAGE:
SIGN? number
DESCRIPTION:
Returns sign of N as 1, 0, or -1 (to use as a multiplier).
SIGN? is a native! value.
ARGUMENTS:
number [number! time!]
.docend
>> sign? 33.5
== 1
top alphanumeric-index category-index
This word is a synonym for sinetop alphanumeric-index category-index
The word sine returns the trigonometric sine.Examples
Has 1 parameter, a number! (representing an angle).
Refinements:
/radians : expects the input angle in radians;
without refinement expects the input angle in degrees.
sine 90
== 1.0
sine/radians pi
== 0.0
sine/radians pi + 0.003
== -0.002999995500002016
top alphanumeric-index category-index
Returns the size of a file content.
Arguments
A file!
Examples
; -------------------------------------------------------------------------
; www.red exists, bad.red does not
; -------------------------------------------------------------------------
>> size? %www.red
== 31
>> size? %bad.red
== none
top alphanumeric-index category-index
The word skip moves the index of a series!.Examples
Note: you can test the position of the index using index?.
red>> x: [1 2 3 4 5 6 7 8 9]
== [1 2 3 4 5 6 7 8 9]
red>> index? x
== 1
red>> skip x 3
== [4 5 6 7 8 9] ; Skip does not change the target series!
red>> x
== [1 2 3 4 5 6 7 8 9]
red>> x: skip x 3
== [4 5 6 7 8 9]
red>> x
== [4 5 6 7 8 9]
red>> index? x
== 4 ; Original position (1) + skip offset (3)
red>> x: skip x -1 ; Skipping backward is allowed
== [3 4 5 6 7 8 9]
red>> x
== [3 4 5 6 7 8 9]
red>> index? x
== 3
red>> x: skip x 100 ; Skip forward out of range moves to tail
== []
red>> index? x
== 10
red>> tail? x
== true
red>> x: skip x -100 ; Skip backward out of range moves to head
== [1 2 3 4 5 6 7 8 9]
red>> index? x
== 1
red>> head? x
== true
top alphanumeric-index category-index
A pre-defined char! value.
See char!
top alphanumeric-index category-index
The word sort sorts a series!, modifying the original series!.Examples
The default order is ascending and the sort is by default case-insensitive.
The argument must be a series! type:
- block!
- string!
- vector!
etc.
Refinements
/case - perform a case-sensitive sort.
/reverse - reverse the sort order (descending instead of ascending)
/skip - treat the series! as fixed size records (groups of items).
Sorting is performed based on the value of the first item in
a group of items. The group size is specified as an integer!.
/part - sort only part of a series!.
We provide a length (a number!) or a position in the series!
/compare - we specify a comparator, which can be an offset integer!,
a block! or a function!.
/all - will force a group of items to be passed as a series! to
the compare function. You also will need /skip to specify the
size of such a group.
/stable - sort uses Quicksort as its default sorting algorithm.
Quicksort is very fast, but it is an unstable algorithm.
If you need stable sorting, add the /stable refinement, and it
will then use the Merge algorithm instead.
This issue is not significant for sorting simple series!.
Read more about sort algorithms here:
Wikipedia on sort algorithm
; -------------------------------------------------------------------------
; Sort some numbers ascending (changing the series! involved)
; -------------------------------------------------------------------------
red>> ages: [55 33 73 73 19]
== [55 33 73 73 19]
red>> sort ages
== [19 33 55 73 73]
red>> ages
== [19 33 55 73 73]
; -------------------------------------------------------------------------
; Sort some numbers descending
; -------------------------------------------------------------------------
red>> ages: [55 33 73 73 19]
== [55 33 73 73 19]
red>> sort/reverse ages
== [73 73 55 33 19]
red>> ages
== [73 73 55 33 19]
; -------------------------------------------------------------------------
; Sort strings (case-insensitive sort)
; -------------------------------------------------------------------------
red>> names: ["dog" "Ape" "Cat" "ant" "aardvark"]
== ["dog" "Ape" "Cat" "ant" "aardvark"]
red>> sort names
== ["aardvark" "ant" "Ape" "Cat" "dog"]
red>> names
== ["aardvark" "ant" "Ape" "Cat" "dog"]
; -------------------------------------------------------------------------
; Sort strings (case-insensitive sort)
; -------------------------------------------------------------------------
red>> names: ["dog" "Ape" "Cat" "ant" "aardvark"]
== ["dog" "Ape" "Cat" "ant" "aardvark"]
red>> sort/case names
== ["Ape" "Cat" "aardvark" "ant" "dog"]
red>> names
== ["Ape" "Cat" "aardvark" "ant" "dog"]
; -------------------------------------------------------------------------
; Sort on the first item of each group of 2 items (the name)
; -------------------------------------------------------------------------
red>> name-ages: ["Larry" 45 "Curly" 50 "Mo" 42]
== ["Larry" 45 "Curly" 50 "Mo" 42]
red>> sort/skip name-ages 2
== ["Curly" 50 "Larry" 45 "Mo" 42]
red>> name-ages
== ["Curly" 50 "Larry" 45 "Mo" 42]
; -------------------------------------------------------------------------
; Sort on a specific item (not the first which is default) of each group
; -------------------------------------------------------------------------
red>> name-ages: ["Larry" 45 "Curly" 50 "Mo" 42]
== ["Larry" 45 "Curly" 50 "Mo" 42]
red>> sort/skip/compare name-ages 2 2
== ["Mo" 42 "Larry" 45 "Curly" 50]
red>> name-ages
== ["Mo" 42 "Larry" 45 "Curly" 50]
; -------------------------------------------------------------------------
; Sort only a subset of the series! using a number
; -------------------------------------------------------------------------
red>> s: [8 7 6 5 4 3 2]
== [8 7 6 5 4 3 2]
red>> sort/part s 3
== [6 7 8 5 4 3 2] ; Only the first 3 items were sorted
red>> s
== [6 7 8 5 4 3 2]
; -------------------------------------------------------------------------
; Sort only a subset of the series! using a position
; -------------------------------------------------------------------------
red>> n: [9 8 7 6 5 4 3 2 1]
== [9 8 7 6 5 4 3 2 1]
red>> pos: skip n 4
== [5 4 3 2 1]
red>> sort/part n pos
== [6 7 8 9 5 4 3 2 1] ; Only the first 4 items were sorted
red>> n
== [6 7 8 9 5 4 3 2 1]
; -------------------------------------------------------------------------
; Using /compare to specify a function to compare 2 items
; -------------------------------------------------------------------------
red>> names: ["Larry" "Curly" "Mo" ]
== ["Larry" "Curly" "Mo"]
red>> sort/compare names function [a b] [a > b]
== ["Mo" "Larry" "Curly"]
red>> names
== ["Mo" "Larry" "Curly"]
red>> sort/compare names function [a b] [a < b]
== ["Curly" "Larry" "Mo"]
red>> names
== ["Curly" "Larry" "Mo"]
; -------------------------------------------------------------------------
; Specify which columns to compare using a path (e.g. /2)
; -------------------------------------------------------------------------
red>> names2: ["Larry" 45 "Curly" 50 "Mo" 42]
== ["Larry" 45 "Curly" 50 "Mo" 42]
red>> sort/skip/compare/all names2 2 function [a b][a/2 > b/2]
== ["Curly" 50 "Larry" 45 "Mo" 42] ; Sorted by descending age
red>> name-ages
== ["Curly" 50 "Larry" 45 "Mo" 42]
top alphanumeric-index category-index
The word source shows the source code of a mezzanine word.Examples
Does not return a value.
A mezzanine word is a Red word! that is defined in terms of Red itself.
Has 1 parameter, the name of an existing Red word,
>> source rejoin
rejoin: func [
"Reduces and joins a block of values."
block [block!] "Values to reduce and join"
][
if empty? block: reduce block [return block]
append either series? first block [copy first block] [
form first block
] next block
]
top alphanumeric-index category-index
A pre-defined char! value.
See char!
top alphanumeric-index category-index
A pre-defined char! value.
See char!
top alphanumeric-index category-index
Returns the spec of a value that supports reflection.
Arguments
A value of a type that supports reflection.
top alphanumeric-index category-index
The word split breaks a string! into pieces using the provided delimiters.Examples
A block! is returned containing the individual pieces.
The arguments are:
a series - any-string! - the string to be split.
a delimiter - string! or char! - the delimiter
Note that the char! datatype has some common delimeters
predefined (such as 'newline', 'tab', 'space'. 'escape' etc.)
; -------------------------------------------------------------------------
; Split with a space
; -------------------------------------------------------------------------
red>> s1: "A few words"
== "A few words"
red>> split s1 " "
== ["A" "few" "" "" "" "" "" "words"]
; -------------------------------------------------------------------------
; Use the predefined constant "space"
; -------------------------------------------------------------------------
red>> split s1 space
== ["A" "few" "" "" "" "" "" "words"]
top alphanumeric-index category-index
Splits a file! or url! path. Returns a block containing path andExamples
target.
Arguments
A file! or a url!
; -------------------------------------------------------------------------
; Use split-path on a url, and a file
; -------------------------------------------------------------------------
red>> split-path http://www.red-lang.org/p/about.html
== [http://www.red-lang.org/p/ %about.html]
red>> split-path %data/personal/notes.txt
== [%data/personal/ %notes.txt]
top alphanumeric-index category-index
USAGE:
SQRT number
DESCRIPTION:
Returns the square root of a number.
SQRT is a function! value.
ARGUMENTS:
number [number!]
RETURNS:
[float!]
; -------------------------------------------------------------------------
; sqrt
; -------------------------------------------------------------------------
>> sqrt 2.1
== 1.449137674618944
>> sqrt 16
== 4.0
top alphanumeric-index category-index
The word square-root returns the square-root of a number!.Examples
Its argument must be a number! type.
; -------------------------------------------------------------------------
; Square-root of integer! gives a float!
; -------------------------------------------------------------------------
red>> square-root 16
4.0
; -------------------------------------------------------------------------
; Square-root of a float!
; -------------------------------------------------------------------------
red>> square-root 4.1
2.024845673131658
; -------------------------------------------------------------------------
; Square-root of a negative number (NaN = Not a Number)
; -------------------------------------------------------------------------
red>> square-root -9
1.#NaN
; -------------------------------------------------------------------------
; Square-root of a pair! (invalid operation))
; -------------------------------------------------------------------------
red>> square-root 22x33
*** Script error: square-root does not allow pair! for its value argument
*** Where: square-root
top alphanumeric-index category-index
The word stats returns interpreter statistics, includingExamples
memory management data.
Refinements
/show - not yet implemented
/info - more detailed data
; -------------------------------------------------------------------------
; Basic stats
; -------------------------------------------------------------------------
red>> stats
== 86605824
; -------------------------------------------------------------------------
; More detailed stats (Can be a large block!, hence the ...)
; -------------------------------------------------------------------------
red>> print mold stats/info
== [[[9999 1 10000] [9999 1 10000] [9999 1 10000] [9999 1 10000] [...
top alphanumeric-index category-index
USAGE:
STOP-REACTOR face
DESCRIPTION:
STOP-REACTOR is a function! value.
ARGUMENTS:
face [object!]
REFINEMENTS:
/deep =>
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
The word strict-equal? returns true if two values are equalExamples
and are of the same datatype!, otherwise returns false.
When comparing strings lowercase is considered not equal to uppercase.
red>> strict-equal? 12 12.0
== false
red>> strict-equal? "abc" "abc"
== true
red>> strict-equal? "abC" "ABC"
== false
top alphanumeric-index category-index
The datatype! string! represents a series! type containing char!.Examples
For string functions (such as substring etc), we use the series functions, here.
(Awaiting additional documentation by red-by-example team.)
; -------------------------------------------------------------------------
; Some examples
; -------------------------------------------------------------------------
red>> str: "abcdefg"
== "abcdefg"
;Using { } allows a string to spread over several lines - spaces and newlines are significant
red>> s: { a two-line
string}
red>> str/3
== #"c" ; Representation of a single character in Red
top alphanumeric-index category-index
The word string? returns true if its argument isExamples
a string!, otherwise false.
Its argument can be of any type.
; -------------------------------------------------------------------------
; Test a string!
; -------------------------------------------------------------------------
red>> string? "abc"
true
; -------------------------------------------------------------------------
; Test a char!
; -------------------------------------------------------------------------
red>> string? #"A"
false
; -------------------------------------------------------------------------
; Test a number!
; -------------------------------------------------------------------------
red>> string? 12.34
false
top alphanumeric-index category-index
The word subtract subtracts the second value from the first.Examples
It is equivalent of the infix operator -.
It has 2 arguments, which can be of these types:
number! char! pair! tuple! vector!
red>> subtract 3.4 2
== 1.4
red>> v: make vector! [2 3 4]
== make vector! [2 3 4]
red>> v2: make vector! [10 100 200]
== make vector! [10 100 200]
red>> subtract v v2
== make vector! [-8 -97 -196]
red>> subtract 20x10 3x3
== 17x7
red>> subtract 10 3x3
*** Script error: subtract does not allow pair for its value2 argument
*** Where: subtract
red>> subtract 3x3 10
== -7x-7
top alphanumeric-index category-index
The word suffix? returns the extension of a file!,Examples
the suffix of a url!, otherwise returns none.
Note that when a suffix is found, a file! type is returned.
Its single argument can be:
file! url! string!
; -------------------------------------------------------------------------
; File with extension (The % is used in literal files)
; -------------------------------------------------------------------------
red>> suffix? %myfile.txt
%.txt
; -------------------------------------------------------------------------
; File without extension
; -------------------------------------------------------------------------
red>> suffix? %myfile
none
; -------------------------------------------------------------------------
; URL with suffix
; -------------------------------------------------------------------------
red>> suffix? http://www.place.org/info.html
%.html
; -------------------------------------------------------------------------
; URL without suffix
; -------------------------------------------------------------------------
red>> suffix? http://www.google.com/
none
; -------------------------------------------------------------------------
; File with extension as a string!
; -------------------------------------------------------------------------
red>> suffix? "data.txt"
%.txt
top alphanumeric-index category-index
USAGE:
SUM values
DESCRIPTION:
Returns the sum of all values in a block.
SUM is a function! value.
ARGUMENTS:
values [block! vector! paren! hash!]
; -------------------------------------------------------------------------
; sum - note diffent types used
; -------------------------------------------------------------------------
>> sum [1 2 3 4 5]
== 15
>> sum [1.2 3.4]
== 4.6
>> sum [3 4 5x5]
== 12x12
top alphanumeric-index category-index
The word swap swaps elements between two series! or the same series!.Examples
It modifies both arguments.
The 2 arguments must be of type series!
; -------------------------------------------------------------------------
; Create 2 series to work with (different lengths)
; -------------------------------------------------------------------------
red>> s1: [1 2 3 4 5 6]
== [1 2 3 4 5 6]
red>> s2: [100 200 300 400]
== [100 200 300 400]
red>> swap s1 s2 ; Swap first elements
== [100 2 3 4 5 6]
red>> s1
== [100 2 3 4 5 6] ; s1 was changed
red>> s2
== [1 200 300 400] ; s2 also changed
; -------------------------------------------------------------------------
; Swap within same series; arg 1 is element 1; arg 2 is element 100
; -------------------------------------------------------------------------
red>> ser: [ 1 2 3 4 5 100 200 300 400]
== [1 2 3 4 5 100 200 300 400]
red>> swap ser find ser 100
== [100 2 3 4 5 1 200 300 400]
; -------------------------------------------------------------------------
; Also string!s are series!!
; -------------------------------------------------------------------------
red>> swap "ABC" "xyz" ; Swap first characters of each string
== "xBC"
; -------------------------------------------------------------------------
; Swap in the middle of some series!
; -------------------------------------------------------------------------
red>> sa: [22 11 33 55]
== [22 11 33 55]
red>> sb: [105 101 107 109]
== [105 101 107 109]
red>> swap find sa 33 find sb 101
== [101 55]
red>> sa
== [22 11 101 55]
red>> sb
== [105 33 107 109]
top alphanumeric-index category-index
The word switch chooses a block! based on a value.Examples
We provide a value (which can be an expression of any type), then a
series of block!s, each prefixed with a value.
The block associated with the matching value is evaluated.
The prefixed values are not evaluated (Note that differing types are allowed).
The value of the last evaluation in the matched block is returned.
Refinements
/default - allows us to specify a default block! at
the bottom of the switch, which is executed
when no value matches
; -------------------------------------------------------------------------
; We try to match on different datatypes here
; -------------------------------------------------------------------------
switch 23.77.44 [
1 [print "1: integer"]
23.77.44 [print "23.77.44: a tuple"]
"India" [print "India: string"
country: true]
]
23.77.44: a tuple
; -------------------------------------------------------------------------
; Using the /default refinement where nothing matches with "China"
; -------------------------------------------------------------------------
item: "China"
switch/default item [
1 [print "1: integer"]
23.77.44 [print "23.77.44: a tuple"]
"India" [print "India: string"
country: true]
] [
print "No match"
]
No match
top alphanumeric-index category-index
A pre-defined char! value.
See char!
top alphanumeric-index category-index
This datatype! provides a literal way of expressing items enclosedExamples
in '<' and '>'. It is a type of series. There are also the related
to-tag and tag? functions.
; -------------------------------------------------------------------------
; Create a tag! type, explore it.
; -------------------------------------------------------------------------
>> t: <picture 100 200>
== <picture 100 200>
; Examine its type
>> type? t
== tag!
; Tag as series:
>> first t
== #"p"
>> append t " square"
== <picture 100 200 square>
top alphanumeric-index category-index
This function returns true if its argument is an tag! datatype!,Examples
otherwise false.
Arguments
Its single argument can be anytype!.
; -------------------------------------------------------------------------
; explore tag?
; -------------------------------------------------------------------------
>> t:
==
>> tag? t
== true
>> tag? ""
== false
top alphanumeric-index category-index
The word tail can be used to set a series! index rightExamples
after the last item in the series!.
The word tail? can be used to test if a series! is at tail.
The word index? can be used to obtain the current index.
red>> x: [1 2 3 4 5]
== [1 2 3 4 5]
red>> head? x
== true
red>> index? x
== 1
red>> x: tail x
== []
red>> tail? x
== true
red>> index? x
== 6 ; Note that the index is one beyond the last element!
top alphanumeric-index category-index
The word tail? tests if a series! is at tail position;Examples
it returns true if at tail or false otherwise.
The word index? can be used to obtain the current index.
red>> x: [1 2 3 4 5]
== [1 2 3 4 5]
red>> head? x
== true
red>> index? x
== 1
red>> x: tail x
== []
red>> tail? x
== true
red>> index? x
== 6 ; Note that the index is one beyond the last element!
top alphanumeric-index category-index
The word take removes and returns one or more elements of a series!.Examples
Using append with take and its /tail refinement lets us use a series!
as a stack or queue.
Arguments
The single argument is a series! or none!
Refinements
/part - specifies a length or end position.
Supply a length (a number!) or a position in a series!
/deep - copy nested series values.
/last - take from from the tail end.
; -------------------------------------------------------------------------
; Examples of take, and some refinements
; -------------------------------------------------------------------------
red>> s: [11 22 33 44 55]
red>> take s
== 11
red>> s
== [22 33 44 55]
red>> s: [11 22 33 44 55]
== [11 22 33 44 55]
red>> take/last s
== 55
red>> s
== [11 22 33 44]
red>> s: [11 22 33 44 55]
== [11 22 33 44 55]
red>> take/part s 3
== [11 22 33]
red>> s
== [44 55]
top alphanumeric-index category-index
To do by red-by-example team ...
The word tangent returns the trigonometric tangent of a number!Examples
of degrees.
It has one numeric parameter.
Refinements:
/radians : expects the input value in radians;
without refinement expects the input value in degrees.
tangent 45
== 1.0
tangent 60 * 1.1
== 2.246036773904216
tangent/radians 1.05
== 1.74331530998317
top alphanumeric-index category-index
The word third returns the third value in a series! (also string!s).Examples
The argument can be one of:
series! tuple! pair!
If there is no third item, none is returned.
; -------------------------------------------------------------------------
; Third of block
; -------------------------------------------------------------------------
red>> third [2 3 4 5]
== 4
; -------------------------------------------------------------------------
; Third of a string is a character type
; -------------------------------------------------------------------------
red>> third "ABCDE"
== #"C"
; -------------------------------------------------------------------------
; Third of a tuple
; -------------------------------------------------------------------------
red>> third 22.33.44.55
== 44
; -------------------------------------------------------------------------
; No third here
; -------------------------------------------------------------------------
red>> third[44]
== none
top alphanumeric-index category-index
Throws control to a catch. Note that catch and throw are mainly
intended for creating new control structures, and are not the main
exception-handling functions. For this, look at attempt, try,
cause-error error?, and error!.
For throw examples, look at the entry for catch.
Arguments
The single argument value, can be any type, and will be the value returned
from the matching catch.
Refinements
/name - Throws to a named catch. We supply a word.
top alphanumeric-index category-index
The Time! datatype! lets us express an amount of time in hours, minutes,Examples
seconds, and subseconds. Both positive and negative times are permitted.
Colons separate items, apart from subseconds, which use a decimal point.
Times are stored in a standard form, irrespective of how they are entered.
(Thus, 59 minutes and 63 seconds are stored as 1 hour, 0 minutes, and 3 seconds.)
The refinements /hour /minute /second can be used to get part of a time.
Times can be compared, and used in calculations where a time result is meaningful.
For example, we can add two times, but not multiply them.
The to-time function can be used to convert other values into a time!.
; -------------------------------------------------------------------------
; Create some times, work with them.
; -------------------------------------------------------------------------
; Create times
>> t1: 3:4:58
== 3:04:58
>> t2: 24:0:63
== 24:01:03
>> t3: 3:0.95
== 0:03:00.950000001
; Refinement
>> t1/second
== 58.0
; Comparison
>> t1 > t2
== false
; Calculations
>> t1 * 2.9
== 8:56:24.2
>> t1 + t2
== 27:06:01
>> t1 * t2
*** Script Error: incompatible argument for multiply of time!
*** Where: *
; Use of to-time function
>> to-time [11 22 33]
== 11:22:33
>> to-time [11 22 61]
== 11:23:01
>> to-time "12:24"
== 12:24:00
top alphanumeric-index category-index
The word time? returns true if the value is a time!,Examples
otherwise false.
Arguments
Has one argument, which can be of any type.
; -------------------------------------------------------------------------
; Show usage here
; -------------------------------------------------------------------------
red>> time? 3:15
== true
red>> type? 3:15
== time!
red>> time? 3.15
== false
red>> type? 3.15
== float!
red>> t: 3:45
== 3:45:00.0
red>> time? t
== true
top alphanumeric-index category-index
The word to converts a value to a specified datatype!.Examples
Only specific conversions are allowed.
Arguments:
type - a datatype! name. Note that this name can be the result of an evaluation.
spec - the attributes of the new value. Often, this is simply the value itself.
; -------------------------------------------------------------------------
; Do some simple conversions
; -------------------------------------------------------------------------
red>> to integer! 2.333
2
red>> to string! 234
"234"
red>> to integer! "23456"
23456
red>> to integer! "33"
== 33
red>> to set-word! "foo"
== foo:
red>> to word! "bar"
== bar
; -------------------------------------------------------------------------
; Compute the new type
; -------------------------------------------------------------------------
red>> affect: true
true
red>> to either affect [float!] [string!] 567
567.0
top alphanumeric-index category-index
USAGE:
TO-BINARY value
DESCRIPTION:
Convert to binary! value.
TO-BINARY is a function! value.
ARGUMENTS:
value
Equivalent to using: to binary!
See binary!
; -------------------------------------------------------------------------
; to-binary with various types
; -------------------------------------------------------------------------
>> to-binary 15
== #{0000000F}
>> to-binary 2.3
== #{4002666666666666}
>> to-binary "ABCDE"
== #{4142434445}
top alphanumeric-index category-index
USAGE:
TO-BITSET value
DESCRIPTION:
Convert to bitset! value.
TO-BITSET is a function! value.
ARGUMENTS:
value
Equivalent to using: to bitset!
See bitset!
; -------------------------------------------------------------------------
; to-bitset example:
; -------------------------------------------------------------------------
>> to-bitset #"A"
== make bitset! #{000000000000000040}
top alphanumeric-index category-index
USAGE:
TO-BLOCK value
DESCRIPTION:
Convert to block! value.
TO-BLOCK is a function! value.
ARGUMENTS:
value
Equivalent to using: to block!
See block!
; -------------------------------------------------------------------------
; to-block
; -------------------------------------------------------------------------
to-block 12.34
== [12.34]
>> to-block [12.34]
== [12.34]
>> to-block (1 2 3 4)
== [4]
top alphanumeric-index category-index
USAGE:
TO-CHAR value
DESCRIPTION:
Convert to char! value.
TO-CHAR is a function! value.
ARGUMENTS:
value
Equivalent to using: to char!
See char!
; -------------------------------------------------------------------------
; to-char with various types
; -------------------------------------------------------------------------
>> to-char 65
== #"A"
>> to-char "A" ;-- NB "A" is a string type
== #"A"
>> to-char "CAT" ;-- first char from string of chars
== #"C"
top alphanumeric-index category-index
USAGE:
TO-DATE value
DESCRIPTION:
Convert to date! value.
TO-DATE is a function! value.
ARGUMENTS:
value
Equivalent to using: to date!
See date!
; -------------------------------------------------------------------------
; to-date
; -------------------------------------------------------------------------
>> to-date [3 5 2022]
== 3-May-2022
top alphanumeric-index category-index
Converts a value to an email! type.Examples
Arguments
It takes a single value.
; -------------------------------------------------------------------------
; Do some conversions to email!
; -------------------------------------------------------------------------
; String
>> to-email "[email protected]"
== [email protected]
; Block
>> to-email [a @b.c]
== [email protected]
; and confirm the type
>> type? to-email [a @b.c]
== email!
top alphanumeric-index category-index
USAGE:
TO-FILE value
DESCRIPTION:
Convert to file! value.
TO-FILE is a function! value.
ARGUMENTS:
value
Equivalent to using: to file!
See file!
; -------------------------------------------------------------------------
; to-file
; -------------------------------------------------------------------------
>> f: "my-name.txt"
== "my-name.txt"
>> to-file f
== %my-name.txt
top alphanumeric-index category-index
USAGE:
TO-FLOAT value
DESCRIPTION:
Convert to float! value.
TO-FLOAT is a function! value.
ARGUMENTS:
value
Equivalent to using: to float!
See float!
; -------------------------------------------------------------------------
; to-float, various types
; -------------------------------------------------------------------------
>> f: "12.34" ;-- string type
== "12.34"
>> to-float f
== 12.34
>> to-float 123 ;-- integer
== 123.0
top alphanumeric-index category-index
USAGE:
TO-GET-PATH value
DESCRIPTION:
Convert to get-path! value.
TO-GET-PATH is a function! value.
ARGUMENTS:
value
Equivalent to using: to get-path!
See get-path!
; -------------------------------------------------------------------------
; to-get-path
; -------------------------------------------------------------------------
>> g: [aa bb cc]
== [aa bb cc]
>> to-get-path g
== :aa/bb/cc
>> g: "aa bb cc"
== "aa bb cc"
>> to-get-path g
== :aa/bb/cc
top alphanumeric-index category-index
USAGE:
TO-GET-WORD value
DESCRIPTION:
Convert to get-word! value.
TO-GET-WORD is a function! value.
ARGUMENTS:
value
Equivalent to using: to get-word!
See get-word!
; -------------------------------------------------------------------------
; to-get-word
; -------------------------------------------------------------------------
>> to-get-word "abc"
== :abc
top alphanumeric-index category-index
USAGE:
TO-HASH value
DESCRIPTION:
Convert to hash! value.
TO-HASH is a function! value.
ARGUMENTS:
value
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
The word to-hex converts an integer! to a hexadecimalExamples
value, with a leading # and leading zeroes.
Its argument is an integer!.
Refinements
/size - an integer! specifying the number of
hexadecimal digits in the result.
No errors result if a small size value
removes significant parts of the result.
; -------------------------------------------------------------------------
; Conversions of positive and negative integer!s
; -------------------------------------------------------------------------
red>> to-hex 64
#00000040
red>> to-hex -1
#FFFFFFFF
red>> to-hex 2 ** 31
== #80000000 ; Left most bit (= sign bit) is set
; -------------------------------------------------------------------------
; Limit digits with /size (from the right)
; -------------------------------------------------------------------------
red>> to-hex/size 64 4
#0040
top alphanumeric-index category-index
USAGE:
TO-IMAGE value
DESCRIPTION:
Convert to image! value.
TO-IMAGE is a function! value.
ARGUMENTS:
value
(Awaiting additional documentation by red-by-example team.)
See image! datatype.
top alphanumeric-index category-index
USAGE:
TO-INTEGER value
DESCRIPTION:
Convert to integer! value.
TO-INTEGER is a function! value.
ARGUMENTS:
value
Equivalent to using: to integer!
See integer!
; -------------------------------------------------------------------------
; to-integer with various types
; -------------------------------------------------------------------------
>> to-integer 2.3
== 2
>> to-integer 2.9
== 2
>> to-integer #"A" ;-- char!
== 65
>> to-integer "ABC" ;-- error in string!
*** Script Error: cannot MAKE/TO integer! from: "ABC"
>> to-integer "123" ;-- allowed string!
== 123
top alphanumeric-index category-index
USAGE:
TO-ISSUE value
DESCRIPTION:
Convert to issue! value.
TO-ISSUE is a function! value.
ARGUMENTS:
value
Equivalent to using: to issue!
See issue!
top alphanumeric-index category-index
USAGE:
TO-LIT-PATH value
DESCRIPTION:
Convert to lit-path! value.
TO-LIT-PATH is a function! value.
ARGUMENTS:
value
Equivalent to using: to lit-path!
See lit-path!
; -------------------------------------------------------------------------
; lit-path
; -------------------------------------------------------------------------
>> to-lit-path [aa bb cc]
== 'aa/bb/cc
top alphanumeric-index category-index
USAGE:
TO-LIT-WORD value
DESCRIPTION:
Convert to lit-word! value.
TO-LIT-WORD is a function! value.
ARGUMENTS:
value
The equivalent of: to lit-word!
See lit-word!
; -------------------------------------------------------------------------
; to-lit-word
; -------------------------------------------------------------------------
>> to-lit-word "abc"
== 'abc
>> to-lit-word first [abc]
== 'abc
top alphanumeric-index category-index
USAGE:
TO-LOCAL-DATE date
DESCRIPTION:
Returns the date with local zone.
TO-LOCAL-DATE is a function! value.
ARGUMENTS:
date [date!]
RETURNS:
[date!]
See date!
; -------------------------------------------------------------------------
; to-local-date - note added zone time-difference
; -------------------------------------------------------------------------
>> to-local-date 3-June-2020
== 3-Jun-2020/1:00:00+01:00
top alphanumeric-index category-index
The word to-local-file converts standard, system independent RedExamples
file paths to the file format used by the local operating system.
The path argument must be a file! or a string!.
Refinements
/full - prepends current dir for full path (for relative paths only)
; -------------------------------------------------------------------------
; Windows - using Red file!
; -------------------------------------------------------------------------
red>> to-local-file %/c/data.txt
"c:\data.txt"
; -------------------------------------------------------------------------
; Windows - using a string!
; -------------------------------------------------------------------------
red>> to-local-file "/c/data.txt"
"c:\data.txt"
; -------------------------------------------------------------------------
; Windows - with /full refinement
; -------------------------------------------------------------------------
red>> what-dir
== %/E/Websites/Red-by-Example/auto-v5/ ; Current directory
red>> to-local-file/full %data.txt ; Relative path
== "E:\Websites\Red-by-Example\auto-v5\data.txt"
top alphanumeric-index category-index
USAGE:
TO-LOGIC value
DESCRIPTION:
Convert to logic! value.
TO-LOGIC is a function! value.
ARGUMENTS:
value
Returns true for any value except for false and none.
Equivalent to using: to logic!
See logic!
; -------------------------------------------------------------------------
; to-logic
; -------------------------------------------------------------------------
>> find "ABCDE" "X" ;-- normal find usage
== none
>> find "ABCDE" "B"
== "BCDE"
;-- we might only want a there/missing result
>> to-logic find "ABCDE" "X"
== false
>> to-logic find "ABCDE" "B"
== true
top alphanumeric-index category-index
USAGE:
TO-MAP value
DESCRIPTION:
Convert to map! value.
TO-MAP is a function! value.
ARGUMENTS:
value
Equivalent to using: to map!
See map!
; -------------------------------------------------------------------------
; to-map
; -------------------------------------------------------------------------
>> to-map [a 1 'b 2 "c" 3]
== #(
a: 1
b: 2
"c" 3
)
top alphanumeric-index category-index
USAGE:
TO-NONE value
DESCRIPTION:
Convert to none! value.
TO-NONE is a function! value.
ARGUMENTS:
value
(Awaiting additional documentation by red-by-example team.)
Equivalent to using: to none!
See none!
top alphanumeric-index category-index
USAGE:
TO-PAIR value
DESCRIPTION:
Convert to pair! value.
TO-PAIR is a function! value.
ARGUMENTS:
value
Equivalent to using: to pair!
See pair!
; -------------------------------------------------------------------------
; to-pair with various types
; -------------------------------------------------------------------------
>> to-pair 22
== 22x22
>> to-pair [22 33]
== 22x33
top alphanumeric-index category-index
USAGE:
TO-PAREN value
DESCRIPTION:
Convert to paren! value.
TO-PAREN is a function! value.
ARGUMENTS:
value
Equivalent to using: to paren!
See paren!
; -------------------------------------------------------------------------
; to-paren with various types
; -------------------------------------------------------------------------
>> to-paren "ABC"
== (ABC)
>> to-paren [3 + 4]
== (3 + 4)
top alphanumeric-index category-index
USAGE:
TO-PATH value
DESCRIPTION:
Convert to path! value.
TO-PATH is a function! value.
ARGUMENTS:
value
Equivalent to using: to path!
See path!
; -------------------------------------------------------------------------
; to-path with various types
; -------------------------------------------------------------------------
>> to-path [a b c]
== a/b/c
>> to-path "aa bb cc"
== aa/bb/cc
top alphanumeric-index category-index
USAGE:
TO-PERCENT value
DESCRIPTION:
Convert to percent! value.
TO-PERCENT is a function! value.
ARGUMENTS:
value
Equivalent to using: to percent!
See percent!
; -------------------------------------------------------------------------
; to-percent with various types
; -------------------------------------------------------------------------
>> to-percent 0.75
== 75%
>> to-percent 33
== 3300%
top alphanumeric-index category-index
The word to-red-file converts a local file system path to Red'sExamples
standard machine independent path format.
The argument can be:
- a Red file!
- a string!.
Note that a Red file path - preceded by % - cannot contain a colon (:)
as used in Windows paths, though it can contain \.
The string argument can contain a colon (:).
; -------------------------------------------------------------------------
; Convert a file containing backslashes
; -------------------------------------------------------------------------
red>> to-red-file %\data\notes.txt
%/data/notes.txt
; -------------------------------------------------------------------------
; Convert a Windows file in a string
; -------------------------------------------------------------------------
red>> to-red-file "C:\data\notes.txt"
%/C/data/notes.txt
top alphanumeric-index category-index
USAGE:
TO-REFINEMENT value
DESCRIPTION:
Convert to refinement! value.
TO-REFINEMENT is a function! value.
ARGUMENTS:
value
Equivalent to using: to refinement!
See refinement!
; -------------------------------------------------------------------------
; to-refinement
; -------------------------------------------------------------------------
>> r: /abcd
== /abcd
>> r: to-refinement r
== /abcd
>> type? r
== refinement!
top alphanumeric-index category-index
USAGE:
TO-SET-PATH value
DESCRIPTION:
Convert to set-path! value.
TO-SET-PATH is a function! value.
ARGUMENTS:
value
Equivalent to using: to set-path!
See set-path!
; -------------------------------------------------------------------------
; to-set-path
; -------------------------------------------------------------------------
>> to-set-path 'abc
== abc:
top alphanumeric-index category-index
USAGE:
TO-SET-WORD value
DESCRIPTION:
Convert to set-word! value.
TO-SET-WORD is a function! value.
ARGUMENTS:
value
Equivalent to using: to set-word!
See set-word!
; -------------------------------------------------------------------------
; to-set-word
; -------------------------------------------------------------------------
>> to-set-word 'abc
== abc:
top alphanumeric-index category-index
USAGE:
TO-STRING value
DESCRIPTION:
Convert to string! value.
TO-STRING is a function! value.
ARGUMENTS:
value
Equivalent to using: to string!
See string!
; -------------------------------------------------------------------------
; to-string with various types
; -------------------------------------------------------------------------
>> to-string 123
== "123"
>> to-string #"A"
== "A"
>> to-string [1 2 3]
== "123"
top alphanumeric-index category-index
Converts its single argument to a tag!.Examples
Arguments
A single argument, of any type.
; -------------------------------------------------------------------------
; Explore to-tag, with several types of arguments.
; -------------------------------------------------------------------------
; integer!
>> to-tag 1234
== <1234>
>> v: 3 + 5
== 8
>> to-tag v
== <8>
; string!
>> to-tag "the cat"
==
>> block!
to-tag [a b]
==
top alphanumeric-index category-index
Converts a value to a time!.Examples
Arguments
A single value.
>> to-time [11 22 33]
== 11:22:33
>> to-time [11 22 61]
== 11:23:01
>> to-time "12:24"
== 12:24:00
>> t1 + t2
== 27:06:01
top alphanumeric-index category-index
USAGE:
TO-TUPLE value
DESCRIPTION:
Convert to tuple! value.
TO-TUPLE is a function! value.
ARGUMENTS:
value
Equivalent to using: to tuple!
See tuple!
; -------------------------------------------------------------------------
; to-tuple
; -------------------------------------------------------------------------
>> to-tuple "12.34.88"
== 12.34.88
top alphanumeric-index category-index
USAGE:
TO-TYPESET value
DESCRIPTION:
Convert to typeset! value.
TO-TYPESET is a function! value.
ARGUMENTS:
value
Equivalent to using: to typeset!
See typeset!
; -------------------------------------------------------------------------
; to-typeset
; -------------------------------------------------------------------------
>> num-type!: to-typeset [integer! percent! float!]
== make typeset! [integer! float! percent!]
>> type? num-type!
== typeset!
top alphanumeric-index category-index
USAGE:
TO-UNSET value
DESCRIPTION:
Convert to unset! value.
TO-UNSET is a function! value.
ARGUMENTS:
value
(Awaiting additional documentation by red-by-example team.)
See unset!
top alphanumeric-index category-index
USAGE:
TO-URL value
DESCRIPTION:
Convert to url! value.
TO-URL is a function! value.
ARGUMENTS:
value
Equivalent to using: to url!
See url!
; -------------------------------------------------------------------------
; to-url
; -------------------------------------------------------------------------
>> to-url [aa bb cc]
== aa://bb/cc
top alphanumeric-index category-index
USAGE:
TO-UTC-DATE date
DESCRIPTION:
Returns the date with UTC zone.
TO-UTC-DATE is a function! value.
ARGUMENTS:
date [date!]
RETURNS:
[date!]
See date!
; -------------------------------------------------------------------------
; to-UTC-date
; -------------------------------------------------------------------------
;-- I'm in the UK, British Summertime, my watch says 9:32 a.m
;-- This time is offset from UTC time by 1 hour:
>> to-UTC-date now
== 30-May-2019/8:32:21
top alphanumeric-index category-index
USAGE:
TO-WORD value
DESCRIPTION:
Convert to word! value.
TO-WORD is a function! value.
ARGUMENTS:
value
Equivalent to using: to word!
See word!
; -------------------------------------------------------------------------
; to-word
; -------------------------------------------------------------------------
>> w: to-word 'aaa
== aaa
>> w
== aaa
>> type? w
== word!
top alphanumeric-index category-index
word trim removes white space (tabs and spaces) from a string! or noneExamples
from a block! or object!. The value of the argument is altered.
The argument can be one of: series! object! error! map!
Refinements
/head - Removes only from the head.
/tail - Removes only from the tail.
/auto - Auto indents lines relative to first line.
/lines - Removes all line breaks and extra spaces.
/all - Removes all whitespace (but not line breaks).
/with - Same as /all, but removes characters in a 'with' argument we supply.
It must be one of: char! string! integer!
; -------------------------------------------------------------------------
; Basic trim.
; -------------------------------------------------------------------------
red>> str: " some text "
== " some text "
red>> trim str ; Both head and tail are trimmed
== "some text"
red>> str
== "some text" ; str was altered by trim
; -------------------------------------------------------------------------
; Multiple lines of text - enclosed in { }
; -------------------------------------------------------------------------
red>> str: {Four lines
{ of varying levels
{ of
{ indentation.}
== {Four lines^/ of varying levels^/ of^/ indentation.}
red>> trim str ; ^/ means a newline
== {Four lines^/of varying levels^/of^/indentation.}
; -------------------------------------------------------------------------
; Trim a block! - NB reduce evaluates every element in a block
; -------------------------------------------------------------------------
red>> blk: [none none 3 none]
== [none none 3 none]
red>> trim blk
== [none none 3 none] ; Because elements are not yet evaluated
red>> blk
== [none none 3 none] ; Trim did not alter the block
red>> trim reduce blk
== [3] ; Now trim worked succesfully!
; -------------------------------------------------------------------------
; /head - trim head only
; -------------------------------------------------------------------------
red>> trim/head " AAA "
== "AAA "
; -------------------------------------------------------------------------
; /tail - trim tail only
; -------------------------------------------------------------------------
red>> trim/tail " AAA "
== " AAA"
; -------------------------------------------------------------------------
; /lines - NB last line has line break at end
; -------------------------------------------------------------------------
red>> str: { Two lines
{ of text
{ }
== " Two lines ^/ of text^/ "
red>> trim/lines str
== "Two lines of text" ; Multiple spaces and line breaks are gone
; -------------------------------------------------------------------------
; /all - remove all space
; -------------------------------------------------------------------------
red>> trim/all " some text "
== "sometext"
; -------------------------------------------------------------------------
; /with - Note that spaces are not removed automatically
; -------------------------------------------------------------------------
red>> str: "some extra-characters,, to use"
== "some extra-characters,, to use"
red>> trim/with str ",-"
== "some extracharacters to use" ; , and - removed
red>> str: "some extra-characters,, to use"
== "some extra-characters,, to use"
red>> trim/with str ",- "
== "someextracharacterstouse" ; Now also spaces removed
top alphanumeric-index category-index
The word true has the logical value "true"Examples
and its datatype! is logic!
Logical values are produced from various comparisons.
The logic! type also provides:
false, no, off, on, true, yes
3 = 3.0
== true
3 == 3.0
== false
3 = 4
== false
"abc" = "abc"
== true
"ABC" = "abc"
== true
if 4 > 3 [print "it is"]
-- it is
a: true
== true
b: 4 > 3
== true
top alphanumeric-index category-index
Tries to evaluate a block, similar to do. The value of the block isExamples
returned, but if an error! occurs, the block is abandoned, and an
error value is returned.
Normally, the error is displayed, but we can intercept it with a
function to prevent the display. Often, the error? function is
used for this. We can also access the returned error! object
to get details of the error.
Note that try is concerned with error-handling, and should not
be used with catch and throw, which are intended for building
control structures.
For other error/exception-handling words, look at the related entries
for attempt, cause-error, error?, and the error! type.
Arguments
Its single argument is a block!
Refinements
/all - detects also break, continue, return, exit
and throw exceptions.
; -------------------------------------------------------------------------
; try, cause-error, and error? usage examples
; -------------------------------------------------------------------------
;A toy function for the examples. It basically does a divide, but
;we cause a 'positive' error if the first argument is not positive
;and we also anticipate that a zero-divide error might occur.
do-average: function [
"calc average from positive total, non-zero count"
total [integer!] count [integer!]] [
print ["In do-average with " total count]
if not positive? total [
cause-error 'math 'positive []
]
total / count ;might cause an error
]
print "No errors here:"
print do-average 10 2
print ""
;the following line (commented out now) displayed:
; *** Math Error: positive number required
; *** Where: do
; and then halted.
; print do-average -10 2 ;I am commented-out now
print "Use try and error? - should trigger 'cause-error'"
either error? result: try [do-average -10 2] [
print ["Error id: " result/id]
] [
print ["No errors, result is: " result]
]
print "Use try and error? - force a zero-divide"
either error? result: try [do-average 10 0] [
print ["Error id: " result/id]
] [
print ["No errors, result is: " result]
]
print "Use try and error? - no errors expected "
either error? result: try [do-average 10 2] [
print ["Error id: " result/id]
] [
print ["No errors, result is: " result]
]
print "Done."
THE OUTPUT IS:
--------------
No errors here:
In do-average with 10 2
5
Use try and error? - should trigger 'cause-error'
In do-average with -10 2
Error id: positive
Use try and error? - force a zero-divide
In do-average with 10 0
Error id: zero-divide
Use try and error? - no errors expected
In do-average with 10 2
No errors, result is: 5
Done.
; -------------------------------------------------------------------------
; /all refinement examples
; -------------------------------------------------------------------------
; make a 'break' error. Try does not detect it.
red>> error? try [print 3 + 4 break "done"]
7
*** Throw Error: no loop to break
*** Where: break
; now use /all to detect it (as well as any other errors)
red>> error? try/all [print 3 + 4 break "done"]
7
== true
top alphanumeric-index category-index
The datatype! tuple! is a list of 3 up to 12 bytes.Examples
It offers a versatile way to represent different kind of values such as
version numbers, IP addresses , and colours (example: 0.255.0).
The value of each item in a tuple must be in the range 0 to 255.
Once a tuple! value is created, its size cannot be changed anymore (it is
not a series!),
but its elements can still be modified, using, for example, path syntax.
The following actions are supported by tuple! values:
random, add, divide, multiply, remainder, subtract,
and, or, xor, length?, pick, poke, reverse.
Math operations are allowed with some other scalar datatypes where a result is sensible,
like integer!, float! and percent!.
Tuples can be used to represent RGB colors. To get a list of the predefined
colors, type:
help tuple!
; -------------------------------------------------------------------------
; Create 2 tuples
; -------------------------------------------------------------------------
red>> t1: 6.3.2
== 6.3.2
red>> t2: 100.200.64.32
== 100.200.64.32
; -------------------------------------------------------------------------
; Path syntax to access and modify an element
; -------------------------------------------------------------------------
red>> t2/2
== 200
red>> t2/2: 250
== 250
red>> t2
== 100.250.64.32
; -------------------------------------------------------------------------
; Add the tuples
; -------------------------------------------------------------------------
red>> t1 + t2
== 106.253.66.32
; -------------------------------------------------------------------------
; Add an integer - note clipping at 255
; -------------------------------------------------------------------------
red>> t2 + 200
== 255.255.255.232
; -------------------------------------------------------------------------
; Get help on defined colours
; -------------------------------------------------------------------------
red>> help tuple!
aqua : 40.100.130
beige : 255.228.196
black : 0.0.0
blue : 0.0.255
brick : 178.34.34
... and 100 or so lines more, not shown here
; -------------------------------------------------------------------------
; Compare pre-defined colours
; -------------------------------------------------------------------------
red>> red + green = yellow
== true
top alphanumeric-index category-index
The word tuple? returns true if its argument is a tuple!,Examples
otherwise false.
Its argument can be of any type.
; -------------------------------------------------------------------------
; Test a valid tuple!
; -------------------------------------------------------------------------
red>> t: 22.44.33
22.44.33
red>> tuple? t
true
; -------------------------------------------------------------------------
; Test an integer
; -------------------------------------------------------------------------
red>> n: 123
123
red>> tuple? n
false
top alphanumeric-index category-index
The word type? returns the datatype! of a value.Examples
The argument can be of any type.
Refinements
/word - returns a word value, rather than a datatype value.
; -------------------------------------------------------------------------
; Get the type of several items
; -------------------------------------------------------------------------
red>> type? 123
== integer!
red>> type? [1 2 3]
== block!
red>> x: 22x33
== 22x33
red>> type? x
== pair!
top alphanumeric-index category-index
The datatype! typeset! lets us create a set of types and name them.Examples
Internally, a typeset is stored in a compact array of bits, for fast access
at run-time. By convention, typeset! names should end with a !
The following words are supported with typeset!s:
make
form
mold
and
or
xor
complement
clear
find
insert
append
length?.
Comparison operators are also supported.
Typesets cannot be built from existing typesets; use the low-level types instead.
; -------------------------------------------------------------------------
; Red uses typeset!s internally:
; -------------------------------------------------------------------------
red>> ? typeset!
any-block! : make typeset! [block! paren! path! lit-path! set-path!
get-path! hash!]
any-function! : make typeset! [native! action! op! function! routine!]
any-list! : make typeset! [block! paren! hash!]
any-object! : make typeset! [object! error!]
any-path! : make typeset! [path! lit-path! set-path! get-path!]
any-string! : make typeset! [string! file! url!]
any-type! : make typeset! [datatype! unset! none! logic! block!
paren! string! file! url! char! integer!
float! word! set-word! lit-word! get-word!
refinement! issue! native! action! op!
function! path! lit-path! set-path!
get-path! routine! bitset! object! typeset!
error! vector! hash! pair! percent! tuple!
map! binary! time! image! event!]
any-word! : make typeset! [word! set-word! lit-word! get-word!
refinement! issue!]
default! : make typeset! [datatype! none! logic! block! paren!
string! file! url! char! integer! float!
word! set-word! lit-word! get-word!
refinement! issue! native! action! op!
function! path! lit-path! set-path!
get-path! routine! bitset! object!
typeset! error! vector! hash! pair!
percent! tuple! map! binary! time! image!]
immediate! : make typeset! [datatype! none! logic! char! integer!
float! word! set-word! lit-word!
get-word! refinement! issue! typeset!
pair! percent! tuple! time!]
internal! : make typeset! [unset! event!]
my-type! : make typeset! [integer! pair! tuple!]
number! : make typeset! [integer! float! percent!]
scalar! : make typeset! [char! integer! float! pair! percent! tuple! time!]
series! : make typeset! [block! paren! string! file! url! path! lit-path!
set-path! get-path! vector! hash! binary! image!]
; -------------------------------------------------------------------------
; Make a new type
; -------------------------------------------------------------------------
red>> my-type!: make typeset! [pair! tuple! integer!]
== make typeset! [integer! pair! tuple!]
; -------------------------------------------------------------------------
; Create a function which uses the type
; -------------------------------------------------------------------------
red>> show-it: function [x [my-type!] ] [ print x ]
; -------------------------------------------------------------------------
; Try it with pair, tuple, integer
; -------------------------------------------------------------------------
red>> show-it 30x40 ; pair! is part of this typeset!
30x40
red>> show-it 33.44.2 ; tuple! is part of this typeset!
33.44.2
red>> show-it 234 ; integer! is part of this typeset!
234
red>> show-it 23.45 ; float! is NOT part of this typeset!
*** Script error: show-it does not allow float! for its x argument
*** Where: show-it
; -------------------------------------------------------------------------
; Try to make a typeset from the existing typeset! number!
; -------------------------------------------------------------------------
red>> t!: make typeset![number!]
*** Script error: invalid argument: number!
*** Where: make
red>>
top alphanumeric-index category-index
The word typeset? returns true if its argument is a typeset!,Examples
otherwise false.
The argument can be of any type
; -------------------------------------------------------------------------
; integer! is a type, not a typeset!
; -------------------------------------------------------------------------
red>> typeset? integer!
== false
; -------------------------------------------------------------------------
; number! is a typeset!
; -------------------------------------------------------------------------
red>> typeset? number!
== true
red>> ? number!
number! is a typeset! of value: make typeset! [integer! float! percent!]
; -------------------------------------------------------------------------
; Test a self made typeset!
; -------------------------------------------------------------------------
red>> my-type!: make typeset! [pair! tuple! integer!]
== make typeset! [integer! pair! tuple!]
red>> typeset? my-type!
== true
top alphanumeric-index category-index
The word union returns the union of 2 data sets.Examples
Duplicate entries are only included once.
A new series! is produced, and the 2 arguments are unchanged.
Both series arguments must be of the same datatype, which can be:
block! hash! string! bitset! typeset!
The word union is one of several set-type functions.
Refinements:
/case - use case-sensitive comparison
/skip - treat the series as fixed size records.
Specify the size as an integer!
; -------------------------------------------------------------------------
; Union of 2 blocks
; -------------------------------------------------------------------------
red>> union [1 2 99 4] [1 2 3 4]
== [1 2 99 4 3]
; -------------------------------------------------------------------------
; Union of 2 strings
; -------------------------------------------------------------------------
red>> union "abcd" "Ab"
== "abcd"
; /case makes union case-sensitive
red>> union/case "abcd" "Ab"
== "abcdA"
red>> union/case ["a" "b"] [ "A"]
== ["a" "b" "A"]
top alphanumeric-index category-index
The word unique takes a series! as its argument and removesExamples
all duplicates.
Refinements:
/skip - groups of items are made unique
; -------------------------------------------------------------------------
; Leave only the unique values
; -------------------------------------------------------------------------
red>> unique [1 2 3 4 2 5 4 1 3 2]
== [1 2 3 4 5]
; -------------------------------------------------------------------------
; Make groups of 2 items unique
; -------------------------------------------------------------------------
red>> unique/skip [1 2 3 4 1 2 3 4 5 6 7 8 5 6] 2
== [1 2 3 4 5 6 7 8]
; -------------------------------------------------------------------------
; Unique characters in a string!
; -------------------------------------------------------------------------
red>> unique "abcbcdcdedefefg"
== "abcdefg"
; -------------------------------------------------------------------------
; Unique groups of 3 characters
; -------------------------------------------------------------------------
red>> unique/skip "abcdefabcghijklmnoghi" 3
== "abcdefghijklmno"
top alphanumeric-index category-index
The word unless evaluates the block! if the condition is NOT true,Examples
returning the value of the block; otherwise returns none.
The arguments are a condition, and a block!.
Using unless is equivalent to using if not.
age: 18
unless (age < 18) or (age > 100) [print "Within Range"]
Within range
if not( (age < 18) or (age > 100)) [print "Within Range"]
Within range
; equivalent if (for previous unless example)
if (age >= 18) and (age <= 100) [print "Within Range"]
Within range
; same with condition rewritten
top alphanumeric-index category-index
Unsets the value of a word in its current context.
Its single argument can be a word! or block! of words.
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
This type comes into existence when value slots in memory just contain random
garbage. It is possible to use it when specifying function argument types,
but doing so reduces Red's error detection.
There are no examples currently.
top alphanumeric-index category-index
Returns true if its argument is of the unset! type, otherwise false.Examples
The value? word is also useful in this area.
Its single argument can be of any type.
; -------------------------------------------------------------------------
; unset? and value?
; -------------------------------------------------------------------------
; print does not return a value
red>> unset? print 22
22
== true
; value? does not work with unset!
red>> value? print 22
22
*** Script Error: value? does not allow unset! for its value argument
*** Where: value?
top alphanumeric-index category-index
The word until evaluates its block! argument until the block!Examples
returns a true value.
It has one argument, a block!.
n: 5
until [
print n
n: n - 1
n < 0
]
5
4
3
2
1
0
== true
top alphanumeric-index category-index
The word uppercase converts a string of characters to upper-case.Examples
The single argument can be a string! or a char!.
Refinements:
/part - limits to a given length or position.
NOTE: this is a destructive operation.
When applying it to a variable, that variable will be changed!
See also lowercase.
; -------------------------------------------------------------------------
; Try uppercase on char and string
; -------------------------------------------------------------------------
red>> uppercase #"a"
#"A"
red>> uppercase "ABC def"
"ABC DEF"
; -------------------------------------------------------------------------
; Use /part to specify first 2 chars only
; -------------------------------------------------------------------------
red>> uppercase/part "abcd def" 2
ABcd def"
top alphanumeric-index category-index
The datatype! url! (Uniform Resource Locator) lets usExamples
access web pages, images, files, ftp, email, and other protocols.
It is a series! similar to a string, and values take the pattern:
protocol://path
For example:
http://www.red-lang.org/p/about.html
A url! can be used with a variety of words, such as do, load,
read, write and save.
; -------------------------------------------------------------------------
; Create a URL, read the file
; -------------------------------------------------------------------------
red>> web-file: http://www.red-lang.org/p/about.html
== http://www.red-lang.org/p/about.html
red>> type? web-file
== url!
red>> read web-file
; Output suppressed
red>> print read web-file
; Output suppressed
top alphanumeric-index category-index
Returns true if its argument is a url! type, otherwise false.Examples
It does not do a complete check of the URL for current protocols.
Arguments
Its single argument can be of any type.
; -------------------------------------------------------------------------
; Try url? on various types
; -------------------------------------------------------------------------
;string type, not a url
red>> url? "http://www.google.com/"
== false
;a url
red>> url? http://www.google.com/
== true
;a non-existent protocol, still a url
red>> url? xxx://a.b.c
== true
top alphanumeric-index category-index
The word value? returns true if the argument is a Red word!Examples
that has been set; otherwise returns false.
It has 1 argument, a word!, which is passed as a literal
or as the result of other operations.
a: 33
== 33
value? 'a
== true
a: none
== none
value? 'a
== true
value? 'fish
== false
value? second [a fish]
== false
value? fish
*** Script error: fish has no value
*** Where: value?
; NOTE the error message;
; we must prevent evaluation of the word by using ' or [ ].
top alphanumeric-index category-index
Returns the list of values of a value that supports reflection.Examples
Arguments
A value of a type that supports reflection.
; -------------------------------------------------------------------------
; values-of, with map and object
; -------------------------------------------------------------------------
;with a map
red>> m: make map![a 1 b 2]
== #(
a: 1
b: 2
)
red>> values-of m
== [1 2]
;with an object
red>> place: make object! [ x-pos: 100 y-pos: 200 ]
red>> values-of place
== [100 200]
top alphanumeric-index category-index
The datatype! vector! represents a series! type.Examples
A vector! is a high-performance series! of items.
The items in a vector! must all have the same type (unlike a block!).
The allowable item types are:
integer! float! char! percent!
Vectors of string! are not allowed.
To create a vector, we use make, as in:
v-ages: make vector! [80 18 65]
At least one value must be given, to allow to determine the type.
; -------------------------------------------------------------------------
; First create a vector! of integer!
; -------------------------------------------------------------------------
red>> v-ages: make vector! [80 18 65]
== make vector! [80 18 65]
red>> print v-ages
80 18 65
red>> print mold v-ages
make vector! [80 18 65]
; -------------------------------------------------------------------------
; Add a new item to that vector!
; -------------------------------------------------------------------------
red>> append v-ages 99
== make vector! [80 18 65 99]
red>> print v-ages
80 18 65 99
; -------------------------------------------------------------------------
; Obtain the length of it
; -------------------------------------------------------------------------
red>> print length? v-ages
4
; -------------------------------------------------------------------------
; Extract an item using path notation
; -------------------------------------------------------------------------
red>> print v-ages/2
18
; -------------------------------------------------------------------------
; Return sub series! starting with found item
; -------------------------------------------------------------------------
red>> find v-ages 18
== make vector! [18 65 99]
; -------------------------------------------------------------------------
; Loop through a vector!
; -------------------------------------------------------------------------
red>> grades: make vector![ #"A" #"B" #"F"]
== make vector! [#"A" #"B" #"F"]
red>> foreach grade grades [print grade]
A
B
F
top alphanumeric-index category-index
The word vector? returns true if its argument is a vector!, otherwise false.Examples
Its single argument can be of any type.
; -------------------------------------------------------------------------
; Create a vector! and a block!, then explore vector?
; -------------------------------------------------------------------------
red>> v-ages: make vector! [80 18 65]
== make vector! [80 18 65]
red>> vector? v-ages
== true
red>> a-block: [ 80 18 85]
== [80 18 85]
red>> vector? a-block
== false
top alphanumeric-index category-index
Waits for a duration in seconds. or specified time!. It can also wait for ports.Examples
Arguments
The single argument can be a number!, block!, time!, none!.
Refinements
/all - Returns all events in a block.
; -------------------------------------------------------------------------
; simple wait for 20 seconds
; -------------------------------------------------------------------------
red>> now/time
== 13:34:56
red>> wait 20 ;wait happens here
== none
red>> now/time
== 13:35:21
top alphanumeric-index category-index
The word what displays brief information about every Red function,Examples
one per line.
Has no arguments or refinements.
red>> what
to : Converts to a specified datatype
not : Returns the negation (logical complement) of a value
remove : Returns the series at the same index after removing a value
while : Evaluates body as long as condition is true
any : Evaluates, returning at the first that is true
copy : Returns a copy of a non-scalar value
insert : Inserts value(s) at series index; returns series head
if : If condition is true, evaluate block; else return NONE
... a lot more lines are displayed ... omitted here
top alphanumeric-index category-index
The word what-dir returns the active directory path as a file! value.Examples
It has no arguments.
Irrespective of the operating system, it uses / in file paths.
Literal file paths must start with the % sign:
So, e.g. c:\windows\system32 must be represented as %/c/windows/system32
a: what-dir
== %/C/rebol/
type? a
== file!
top alphanumeric-index category-index
The word while has two arguments:Examples
- a condition block!
- a body block!
As long as the condition block! returns true, the body block!
will be evaluated.
NOTE that in Red, a block! can contain several evaluations; only the value
resulting from the final evaluation influences the while condition.
The word break can be used to break out of the body block!.
In the case where several conditions control the loop continuation, you might
require short-circuit evaluation of the condition using all.
When working with series!, you should investigate existing functions like
first, rather than writing complex loops.
count: 1
while [count <= 5] [
print count
count: count + 1
]
print "Done"
print count
1
2
3
4
5
Done
== 6
while [all [ n < 10 still-ok] ] [
body ; which might alter n and/or still-ok
]
; Either condition going false will terminate the loop.
; The extra [ ] are because the condition must be inside a block.
top alphanumeric-index category-index
Return true if the point is within the rectangle bounds, otherwise false.Examples
Arguments
point - a pair! - XY position of point. Origin is at upper left corner.
offset -a pair! - Offset of area, i.e. upper left corner of a rectangle.
size - a pair! - Size of area, i.e. width, height of area.
; -------------------------------------------------------------------------
; Explore possibilities for within?, with 8x2 area, corner at 5x10
; -------------------------------------------------------------------------
;5x5 outside
red>> within? 5x5 5x10 8x2
== false
;12x11 inside
red>> within? 12x11 5x10 8x2
== true
;5x10 on edge, considered inside
red>> within? 5x10 5x10 8x2
== true
top alphanumeric-index category-index
A word! type is a symbolic value that can be used like a variable, an
identifier that refers to a value in a given context.
See word?"
top alphanumeric-index category-index
The word word? returns true if its argument is a word! type,Examples
otherwise false.
Its single argument can be of any type.
; -------------------------------------------------------------------------
; Try word? with various types
; -------------------------------------------------------------------------
red>> word? "abc"
== false
red>> word? 123
== false
red>> word? abc
*** Script Error: abc has no value
*** Where: word?
'a word
red>> word? 'abc
== true
red>> word? first [abc]
== true
red>> x: 3
== 3
red>> word? x
== false
red>> word? :x
== false
red>> word? first [x]
== true
top alphanumeric-index category-index
Returns the list of words of a value that supports reflection.Examples
Arguments
A value of a type that supports reflection.
; -------------------------------------------------------------------------
; words-of, with map and object
; -------------------------------------------------------------------------
;with map
red>> m: make map![a 1 b 2]
red>> words-of m
== [a b]
;with object
red>> place: make object! [x-pos: 100 y-pos: 200 ]
red>> words-of place
== [x-pos y-pos]
top alphanumeric-index category-index
Writes data to a file!, url!, or other port.Examples
Arguments
1. destination: a file! or url!
2. data to write: any type.
Refinements
/binary - preserves contents exactly. Use for images, etc.
/lines - write each value in a block as a separate line.
/info - no details available currently
/append - write data at end of file.
/part - partial write a given number of units.
We provide a length, as a number!.
/seek - write at a specific position. We provide a position,
as a number!.
/allow - specifies protection attributes. We provide access
details in a block!.
/as - write with the specified encoding, default is 'UTF-8.
We provide the encoding as a word!.
; -------------------------------------------------------------------------
; Use write. Note the preceding % for literal file names.
; -------------------------------------------------------------------------
;write 1 line to local file testfile.txt
red>> write %testfile.txt "ABCDEFG"
;read it back
red>> read %testfile.txt
== "ABCDEFG"
;/seek refinement
red>> write/seek %testfile.txt "XXX" 2
;read it back
red>> read %testfile.txt
== "ABXXXFG"
; /lines refinement
red>> write/lines %testfile.txt ["a line" "another line"]
;read it back, use print to avoid console formatting
red>> print read %testfile.txt
a line
another line
top alphanumeric-index category-index
USAGE:
WRITE-CLIPBOARD data
DESCRIPTION:
Write content to the system clipboard.
WRITE-CLIPBOARD is a routine! value.
ARGUMENTS:
data [string!]
; -------------------------------------------------------------------------
; write-clipboard
; -------------------------------------------------------------------------
>> write-clipboard "Hello!!"
== true
>> read-clipboard
== "Hello!!"
top alphanumeric-index category-index
USAGE:
WRITE-STDOUT str
DESCRIPTION:
Write data to STDOUT.
WRITE-STDOUT is a routine! value.
ARGUMENTS:
str [string!]
(Awaiting additional documentation by red-by-example team.)
top alphanumeric-index category-index
The word xor returns the exclusive "or" of its two arguments.Examples
The type of both arguments can be:
logic! integer! char! bitset! typeset! pair! tuple! vector!
xor provides a sensible interpretation of "true when different"
for types and combinations of types.
The result type is determined by the type of the first argument.
Certain combinations of types are disallowed.
3 xor 2
== 1
3x3 xor 2x2 ;pairs
== 1x1
3x0 xor 3x1
== 0x1
#"A" xor 2 ;character, number
== #"C"
2 xor #"A"
== 67
#"A" xor 2
== #"C"
true xor false
== true
true xor true
== false
3 xor 2x1
*** Script error: xor does not allow pair for its value2 argument
*** Where: xor
123 xor true
*** Script error: logic type is not allowed here
*** Where: xor
top alphanumeric-index category-index
Returns the exclusive OR of its 2 arguments.Examples
It is the functional equivalent of the xor infix operator.
; -------------------------------------------------------------------------
; Explore some types
; -------------------------------------------------------------------------
red>> xor~ 3 3
== 0
red>> xor~ 1 3
== 2
red>> xor~ #"A" 1
== #"@"
red>> xor~ #"D" 1
== #"E"
top alphanumeric-index category-index
The value yes can be used as the boolean value true.Examples
red>> yes
== true
red>> not yes
== false
top alphanumeric-index category-index
The word zero? returns true if its argument is zero;Examples
otherwise false.
Has one argument: a number! or a pair!.
zero? 3
== false
zero? 3.1
== false
zero? 0
== true
zero? 2x0
== false
zero? 0x0
== true
top alphanumeric-index category-index