2.9 KiB
Core Functions
call
call BLOCK [ARGS...]
Invokes block, passing in the arguments. This is as if block was invoked as a command. This may not always be necessary unless these are need to call BLOCK with exactly 0 arguments.
echo
echo [ARGS...]
Displays the string representation of ARGS to stdout followed by a new line.
foreach
foreach SEQ BLOCK
Iterates BLOCK over every element of the sequence.
The values pass to BLOCK will depend on the type of SEQ. If SEQ is a list, BLOCK receives the element value. If SEQ is a hash, BLOCK receives both the key and value of each element.
BLOCK can call break
and continue
which will exit out of the loop, or jump to the start of the next iteration
respectively.
The return value of foreach
will be the result of the last iteration, unless break
is called with a value.
foreach [1 2 3] { |e|
echo "Element = $e"
}
foreach [a:"one" b:"two"] { |k v|
echo "Key $k = $v"
}
keys
keys HASH
Returns the keys of the passed in hash as a list. The order of keys are non-deterministic. If HASH is not a hash, then nil will be returned.
len
len SEQ
Returns the length of SEQ. If SEQ is a list or hash, SEQ will be the number of elements. If SEQ is a string, SEQ will be the string's length. All other values will return a length of 0.
map
map SEQ BLOCK
Returns a new list of elements mapped from SEQ according to the result of BLOCK. SEQ can be any listable data structure, however the result will always be a concrete list.
map [1 2 3] { |x| str $x | len }
proc
proc [NAME] BLOCK
Defines a new function optionally with the given name. When called without NAME, this will define a new
lambda which can be invoked using call
.
When NAME is set, this function defining a function a name will always declare it at the top-level scope.
reduce
reduce SEQ [INIT] BLOCK
Returns the result of reducing the elements of SEQ with the passed in block.
BLOCK will receive at least two argument, with the current value of the accumulator always being the last argument. If SEQ is a list, the arguments will be |element accumulator|, and if SEQ is a hash, the arguments will be |key value accumulator|.
The block result will be set as the value of the accumulator for the next iteration. Once all elements are process
the accumulator value will be returned as the result of reduce
.
If INIT is not set, and SEQ is a list, the accumulator will be set to the first value and BLOCK will be called from the second element, if any. If SEQ is a hash, then the accumulator will be set to nil.
set
set NAME VALUE
Sets the value of variable NAME to VALUE. Any variable with NAME will be checked within the scope first, including any parent scopes, before a new variable is defined. Any new variables will only be defined with the current scope.