From 80e56ec1a502998d0dbcbb2101509defa18c3e3e Mon Sep 17 00:00:00 2001 From: Leon Mika Date: Sun, 19 Jan 2025 09:33:09 +1100 Subject: [PATCH] Documented some more core functions --- _docs/core.md | 57 +++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 44 insertions(+), 13 deletions(-) diff --git a/_docs/core.md b/_docs/core.md index 27b4ed4..21e1d65 100644 --- a/_docs/core.md +++ b/_docs/core.md @@ -23,13 +23,13 @@ Displays the string representation of ARGS to stdout followed by a new line. ### foreach ``` -foreach SEQ BLOCK +foreach COL 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. +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. @@ -46,6 +46,14 @@ foreach [a:"one" b:"two"] { |k v| } ``` +## index + +``` +index COL [INDEX...] +``` + +Returns the index value at INDEX of COL. COL can be either a list or a hash. + ### keys ``` @@ -58,20 +66,20 @@ If HASH is not a hash, then nil will be returned. ### len ``` -len SEQ +len COL ``` -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 +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 SEQ BLOCK +map COL BLOCK ``` -Returns a new list of elements mapped from SEQ according to the result of BLOCK. SEQ can be any listable data +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. ``` @@ -92,20 +100,43 @@ When NAME is set, this function defining a function a name will always declare i ### reduce ``` -reduce SEQ [INIT] BLOCK +reduce COL [INIT] BLOCK ``` -Returns the result of reducing the elements of SEQ with the passed in 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 SEQ is a list, the arguments will be _|element accumulator|_, and if SEQ is a hash, the arguments will be +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 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. +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