Started working on more package docs
This commit is contained in:
parent
f51c3ce891
commit
84702267df
9 changed files with 91 additions and 12 deletions
151
_docs/mod/core.md
Normal file
151
_docs/mod/core.md
Normal file
|
|
@ -0,0 +1,151 @@
|
|||
---
|
||||
---
|
||||
|
||||
# Core Builtins
|
||||
|
||||
### 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 COL BLOCK
|
||||
```
|
||||
|
||||
Iterates BLOCK over every element of the sequence.
|
||||
|
||||
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.
|
||||
|
||||
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.
|
||||
|
||||
This is implemented as a macro but can be used in a pipeline.
|
||||
|
||||
```
|
||||
foreach [1 2 3] { |e|
|
||||
echo "Element = $e"
|
||||
}
|
||||
|
||||
[a:"one" b:"two"] | foreach { |k v|
|
||||
echo "Key $k = $v"
|
||||
}
|
||||
```
|
||||
|
||||
## index
|
||||
|
||||
```
|
||||
index COL [INDEX...]
|
||||
```
|
||||
|
||||
Returns the index value at INDEX of COL. COL can be either a list or a hash.
|
||||
|
||||
### 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 COL
|
||||
```
|
||||
|
||||
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
|
||||
return a length of 0.
|
||||
|
||||
### map
|
||||
|
||||
```
|
||||
map COL BLOCK
|
||||
```
|
||||
|
||||
Returns a new list of elements mapped from COL according to the result of BLOCK. COL 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 COL [INIT] BLOCK
|
||||
```
|
||||
|
||||
Returns the result of reducing the elements of COL 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 COL is a list, the arguments will be _|element accumulator|_, and if COL 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 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
|
||||
}
|
||||
```
|
||||
|
||||
### 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.
|
||||
|
||||
29
_docs/mod/csv.md
Normal file
29
_docs/mod/csv.md
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
# CSV Module
|
||||
|
||||
Functions for operating over CSV data.
|
||||
|
||||
### each-record
|
||||
|
||||
```
|
||||
csv:each-record FILE BLOCK
|
||||
```
|
||||
|
||||
Opens the CSV file at FILE and calls BLOCK on each record. It is expected that this
|
||||
CSV file has a header which appears as the first row. This command will read and
|
||||
index the header, then start calling BLOCK from the row directly below the header.
|
||||
|
||||
BLOCK is called with the arguments _|row header|_ where:
|
||||
|
||||
- _row_ contains the fields of the current row as a list.
|
||||
- _header_ contains a hash mapping a header name to a field index.
|
||||
|
||||
The return value will be nil.
|
||||
|
||||
```
|
||||
csv:each-record "winds.csv" { |row hdr|
|
||||
set name $row.($hdr.name)
|
||||
set bearing $row.($hdr.bearing)
|
||||
|
||||
echo "Wind $name has bearing $bearing"
|
||||
}
|
||||
```
|
||||
11
_docs/mod/fs.md
Normal file
11
_docs/mod/fs.md
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
# FS Module
|
||||
|
||||
Functions for accessing the file system.
|
||||
|
||||
### lines
|
||||
|
||||
```
|
||||
fs:lines FILE
|
||||
```
|
||||
|
||||
Returns a list containing the scanned lines of FILE.
|
||||
8
_docs/mod/index.md
Normal file
8
_docs/mod/index.md
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
# Modules
|
||||
|
||||
Modules of the standard library:
|
||||
|
||||
- [core](/mod/core): Core builtins
|
||||
- [csv](/mod/csv): Functions for operating over CSV data.
|
||||
- [fs](/mod/fs): File system functions
|
||||
- [os](/mod/os): Operating system functions
|
||||
17
_docs/mod/os.md
Normal file
17
_docs/mod/os.md
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
# OS Module
|
||||
|
||||
Functions for accessing the operating system.
|
||||
|
||||
### env
|
||||
|
||||
```
|
||||
os:env NAME [DEFAULT]
|
||||
```
|
||||
|
||||
Returns the value of the environment variable NAME. If no environment with NAME
|
||||
is defined, then DEFAULT will be returned, if specified. Otherwise, nil will be
|
||||
returned.
|
||||
|
||||
```
|
||||
echo "User's home directory is: " (os:env "HOME")
|
||||
```
|
||||
Loading…
Add table
Add a link
Reference in a new issue