ucl/_docs/core.md

151 lines
3.6 KiB
Markdown
Raw Normal View History

2025-01-18 05:02:35 +00:00
---
---
# 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
```
2025-01-18 22:33:09 +00:00
foreach COL BLOCK
2025-01-18 05:02:35 +00:00
```
Iterates BLOCK over every element of the sequence.
2025-01-18 22:33:09 +00:00
The values pass to BLOCK will depend on the type of COL. If COL is a list, BLOCK receives the element value.
If COL is a hash, BLOCK receives both the key and value of each element.
2025-01-18 05:02:35 +00:00
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"
}
```
2025-01-18 22:33:09 +00:00
## index
```
index COL [INDEX...]
```
Returns the index value at INDEX of COL. COL can be either a list or a hash.
2025-01-18 05:02:35 +00:00
### 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
```
2025-01-18 22:33:09 +00:00
len COL
2025-01-18 05:02:35 +00:00
```
2025-01-18 22:33:09 +00:00
Returns the length of COL. If COL is a list or hash, COL will be the number of
elements. If COL is a string, COL will be the string's length. All other values will
2025-01-18 05:02:35 +00:00
return a length of 0.
### map
```
2025-01-18 22:33:09 +00:00
map COL BLOCK
2025-01-18 05:02:35 +00:00
```
2025-01-18 22:33:09 +00:00
Returns a new list of elements mapped from COL according to the result of BLOCK. COL can be any listable data
2025-01-18 05:02:35 +00:00
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
```
2025-01-18 22:33:09 +00:00
reduce COL [INIT] BLOCK
2025-01-18 05:02:35 +00:00
```
2025-01-18 22:33:09 +00:00
Returns the result of reducing the elements of COL with the passed in block.
2025-01-18 05:02:35 +00:00
BLOCK will receive at least two argument, with the current value of the accumulator always being the last argument.
2025-01-18 22:33:09 +00:00
If COL is a list, the arguments will be _|element accumulator|_, and if COL is a hash, the arguments will be
2025-01-18 05:02:35 +00:00
_|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`.
2025-01-18 22:33:09 +00:00
If INIT is not set, and COL is a list, the accumulator will be set to the first value and BLOCK will be called
from the second element, if any. If COL is a hash, then the accumulator will be set to nil.
## seq
```
seq [FROM] TO [-inc]
```
Returns a listable sequence containing all the integers from 0 to TO, exclusive.
TO can be either be negative or positive. If TO Is positive, then the sequence will
step by +1; if TO is negative, then the sequence will step by -1.
If FROM is specified, then the collection contains all the integers from FROM inclusive, to TO
exclusive. The step will be +1 or -1 if TO is greater than, or less than FROM respectively.
If FROM and TO are equal, or if FROM is unspecified and TO is 0, then the sequence will be empty.
If -inc is specified, then the sequence will step towards TO inclusively.
```
foreach (seq 5) { |i|
echo $i
}
```
2025-01-18 05:02:35 +00:00
### 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.