Lists: Difference between revisions
(Created page with "= Lists = The '''Lists''' module provides comprehensive list manipulation and processing functions for the SCM programming language. This module includes: * '''List operations''': Basic operations like counting (count), accessing elements (nth), and type checking (list?) * '''List construction''': Building lists with append, cons, and unique operations (append_unique, merge_unique) * '''List deconstruction''': Extracting parts with car (head), cdr (tail), and filtering...") |
No edit summary |
||
| Line 13: | Line 13: | ||
← Back to [[Full SCM API documentation]] | ← Back to [[Full SCM API documentation]] | ||
== list == | |||
constructs a list from its arguments | |||
'''Allowed number of parameters:''' 0–10000 | |||
<span id="parameters"></span> | |||
=== Parameters === | |||
* '''items''' (<code>any</code>): items to put into the list ''(variadic)'' | |||
<span id="returns"></span> | |||
=== Returns === | |||
<code>list</code> | |||
== count == | == count == | ||
| Line 20: | Line 37: | ||
'''Allowed number of parameters:''' 1–1 | '''Allowed number of parameters:''' 1–1 | ||
<span id="parameters-1"></span> | |||
=== Parameters === | |||
* '''list''' (<code>list</code>): base list | * '''list''' (<code>list</code>): base list | ||
<span id="returns-1"></span> | |||
=== Returns === | |||
<code>int</code> | |||
== nth == | == nth == | ||
| Line 31: | Line 53: | ||
'''Allowed number of parameters:''' 2–2 | '''Allowed number of parameters:''' 2–2 | ||
<span id="parameters-2"></span> | |||
=== Parameters === | |||
* '''list''' (<code>list</code>): base list | * '''list''' (<code>list</code>): base list | ||
* '''index''' (<code>number</code>): index beginning from 0 | * '''index''' (<code>number</code>): index beginning from 0 | ||
'''Returns:''' <code> | <span id="returns-2"></span> | ||
=== Returns === | |||
<code>any</code> | |||
== slice == | |||
extract a sublist from start (inclusive) to end (exclusive). (slice list start end) returns elements list[start..end). | |||
'''Allowed number of parameters:''' 3–3 | |||
<span id="parameters-3"></span> | |||
=== Parameters === | |||
* '''list''' (<code>list</code>): base list | |||
* '''start''' (<code>number</code>): start index (inclusive) | |||
* '''end''' (<code>number</code>): end index (exclusive) | |||
<span id="returns-3"></span> | |||
=== Returns === | |||
<code>list</code> | |||
== reverse == | |||
returns a new list with elements in reversed order. | |||
'''Allowed number of parameters:''' 1–1 | |||
<span id="parameters-4"></span> | |||
=== Parameters === | |||
* '''list''' (<code>list</code>): list to reverse | |||
<span id="returns-4"></span> | |||
=== Returns === | |||
<code>list</code> | |||
== append == | == append == | ||
| Line 41: | Line 102: | ||
appends items to a list and return the extended list. The original list stays unharmed. | appends items to a list and return the extended list. The original list stays unharmed. | ||
'''Allowed number of parameters:''' | '''Allowed number of parameters:''' 1–10000 | ||
<span id="parameters-5"></span> | |||
=== Parameters === | |||
* '''list''' (<code>list</code>): base list | * '''list''' (<code>list</code>): base list | ||
* '''item...''' (<code>any</code>): items to add | * '''item...''' (<code>any</code>): items to add ''(variadic)'' | ||
<span id="returns-5"></span> | |||
=== Returns === | |||
<code>list</code> | |||
== append_unique == | == append_unique == | ||
| Line 53: | Line 119: | ||
appends items to a list but only if they are new. The original list stays unharmed. | appends items to a list but only if they are new. The original list stays unharmed. | ||
'''Allowed number of parameters:''' | '''Allowed number of parameters:''' 1–10000 | ||
<span id="parameters-6"></span> | |||
=== Parameters === | |||
* '''list''' (<code>list</code>): base list | * '''list''' (<code>list</code>): base list | ||
* '''item...''' (<code>any</code>): items to add | * '''item...''' (<code>any</code>): items to add ''(variadic)'' | ||
<span id="returns-6"></span> | |||
=== Returns === | |||
<code>list</code> | |||
== cons == | == cons == | ||
| Line 67: | Line 138: | ||
'''Allowed number of parameters:''' 2–2 | '''Allowed number of parameters:''' 2–2 | ||
<span id="parameters-7"></span> | |||
=== Parameters === | |||
* '''car''' (<code>any</code>): new head element | * '''car''' (<code>any</code>): new head element | ||
* '''cdr''' (<code>list</code>): tail that is appended after car | * '''cdr''' (<code>list</code>): tail that is appended after car | ||
<span id="returns-7"></span> | |||
=== Returns === | |||
<code>list</code> | |||
== car == | == car == | ||
| Line 79: | Line 155: | ||
'''Allowed number of parameters:''' 1–1 | '''Allowed number of parameters:''' 1–1 | ||
<span id="parameters-8"></span> | |||
=== Parameters === | |||
* '''list''' (<code>list</code>): list | * '''list''' (<code>list</code>): list | ||
<span id="returns-8"></span> | |||
=== Returns === | |||
<code>any</code> | |||
== cdr == | == cdr == | ||
extracts the tail of a list | extracts the tail of a list The tail of a list is a list with all items except the head. | ||
'''Allowed number of parameters:''' 1–1 | |||
<span id="parameters-9"></span> | |||
=== Parameters === | |||
* '''list''' (<code>list</code>): list | |||
<span id="returns-9"></span> | |||
=== Returns === | |||
<code>list</code> | |||
== cadr == | |||
extracts the second element of a list. Equivalent to (car (cdr x)). | |||
'''Allowed number of parameters:''' 1–1 | '''Allowed number of parameters:''' 1–1 | ||
<span id="parameters-10"></span> | |||
=== Parameters === | |||
* '''list''' (<code>list</code>): list | * '''list''' (<code>list</code>): list | ||
<span id="returns-10"></span> | |||
=== Returns === | |||
<code>any</code> | |||
== zip == | == zip == | ||
| Line 99: | Line 201: | ||
swaps the dimension of a list of lists. If one parameter is given, it is a list of lists that is flattened. If multiple parameters are given, they are treated as the components that will be zipped into the sub list | swaps the dimension of a list of lists. If one parameter is given, it is a list of lists that is flattened. If multiple parameters are given, they are treated as the components that will be zipped into the sub list | ||
'''Allowed number of parameters:''' | '''Allowed number of parameters:''' 0–10000 | ||
<span id="parameters-11"></span> | |||
=== Parameters === | |||
* '''list''' (<code>any</code>): list of lists of items ''(variadic)'' | |||
<span id="returns-11"></span> | |||
=== Returns === | |||
<code>list</code> | |||
== merge == | == merge == | ||
| Line 110: | Line 217: | ||
flattens a list of lists into a list containing all the subitems. If one parameter is given, it is a list of lists that is flattened. If multiple parameters are given, they are treated as lists that will be merged into one | flattens a list of lists into a list containing all the subitems. If one parameter is given, it is a list of lists that is flattened. If multiple parameters are given, they are treated as lists that will be merged into one | ||
'''Allowed number of parameters:''' | '''Allowed number of parameters:''' 0–10000 | ||
<span id="parameters-12"></span> | |||
=== Parameters === | |||
* '''list''' (<code>any</code>): list of lists of items ''(variadic)'' | |||
<span id="returns-12"></span> | |||
=== Returns === | |||
<code>list</code> | |||
== merge_unique == | == merge_unique == | ||
| Line 121: | Line 233: | ||
flattens a list of lists into a list containing all the subitems. Duplicates are filtered out. | flattens a list of lists into a list containing all the subitems. Duplicates are filtered out. | ||
'''Allowed number of parameters:''' | '''Allowed number of parameters:''' 0–10000 | ||
<span id="parameters-13"></span> | |||
=== Parameters === | |||
* '''list''' (<code>list</code>): list of lists of items ''(variadic)'' | |||
* '''list''' (<code>list</code>): list of lists of items | |||
<span id="returns-13"></span> | |||
=== Returns === | |||
<code>list</code> | |||
<span id="has"></span> | |||
== has? == | == has? == | ||
| Line 134: | Line 252: | ||
'''Allowed number of parameters:''' 2–2 | '''Allowed number of parameters:''' 2–2 | ||
<span id="parameters-14"></span> | |||
=== Parameters === | |||
* '''haystack''' (<code>list</code>): list to search in | * '''haystack''' (<code>list</code>): list to search in | ||
* '''needle''' (<code>any</code>): item to search for | * '''needle''' (<code>any</code>): item to search for | ||
<span id="returns-14"></span> | |||
=== Returns === | |||
<code>bool</code> | |||
== filter == | == filter == | ||
| Line 146: | Line 269: | ||
'''Allowed number of parameters:''' 2–2 | '''Allowed number of parameters:''' 2–2 | ||
<span id="parameters-15"></span> | |||
=== Parameters === | |||
* '''list''' (<code>list</code>): list that has to be filtered | * '''list''' (<code>list</code>): list that has to be filtered | ||
* '''condition''' (<code>func</code>): filter condition func( | * '''condition''' (<code>func(item:any) -> bool</code>): filter condition func(item)->bool | ||
<span id="returns-15"></span> | |||
=== Returns === | |||
<code>list</code> | |||
== find == | |||
returns the first list element that passes the condition function, or nil/default if none matches | |||
'''Allowed number of parameters:''' 2–3 | |||
<span id="parameters-16"></span> | |||
=== Parameters === | |||
''' | * '''list''' (<code>list</code>): list to search | ||
* '''condition''' (<code>func(item:any) -> bool</code>): predicate func(any)->bool that is applied until the first match | |||
* '''default''' (<code>any</code>): optional default value if nothing matches ''(optional)'' | |||
<span id="returns-16"></span> | |||
=== Returns === | |||
<code>any</code> | |||
== map == | == map == | ||
| Line 158: | Line 304: | ||
'''Allowed number of parameters:''' 2–2 | '''Allowed number of parameters:''' 2–2 | ||
<span id="parameters-17"></span> | |||
=== Parameters === | |||
* '''list''' (<code>list</code>): list that has to be mapped | * '''list''' (<code>list</code>): list that has to be mapped | ||
* '''map''' (<code>func</code>): map function func(any)- | * '''map''' (<code>func(item:any) -> any</code>): map function func(any)->any that is applied to each item | ||
<span id="returns-17"></span> | |||
=== Returns === | |||
<code>list</code> | |||
== parallel_map == | |||
like map, but applies fn to each element in parallel using a worker pool limited to runtime.NumCPU() | |||
'''Allowed number of parameters:''' 2–2 | |||
<span id="parameters-18"></span> | |||
=== Parameters === | |||
* '''list''' (<code>list</code>): list to map over in parallel | |||
* '''fn''' (<code>func(item:any) -> any</code>): function applied to each element | |||
<span id="returns-18"></span> | |||
=== Returns === | |||
<code>list</code> | |||
== parallel_map_mut == | |||
like parallel_map, but signals the optimizer that fn may have side effects | |||
'''Allowed number of parameters:''' 2–2 | |||
<span id="parameters-19"></span> | |||
=== Parameters === | |||
* '''list''' (<code>list</code>): list to map over in parallel | |||
* '''fn''' (<code>func(item:any) -> any</code>): function with side effects applied to each element | |||
<span id="returns-19"></span> | |||
=== Returns === | |||
<code>list</code> | |||
<span id="mapindex"></span> | |||
== mapIndex == | == mapIndex == | ||
| Line 170: | Line 356: | ||
'''Allowed number of parameters:''' 2–2 | '''Allowed number of parameters:''' 2–2 | ||
<span id="parameters-20"></span> | |||
=== Parameters === | |||
* '''list''' (<code>list</code>): list that has to be mapped | * '''list''' (<code>list</code>): list that has to be mapped | ||
* '''map''' (<code>func</code>): map function func(i, any)- | * '''map''' (<code>func(index:int, item:any) -> any</code>): map function func(i, any)->any that is applied to each item | ||
<span id="returns-20"></span> | |||
=== Returns === | |||
<code>list</code> | |||
== reduce == | == reduce == | ||
| Line 182: | Line 373: | ||
'''Allowed number of parameters:''' 2–3 | '''Allowed number of parameters:''' 2–3 | ||
<span id="parameters-21"></span> | |||
=== Parameters === | |||
* '''list''' (<code>list</code>): list that has to be reduced | * '''list''' (<code>list</code>): list that has to be reduced | ||
* '''reduce''' (<code>func</code>): reduce function func(any any)- | * '''reduce''' (<code>func(acc:any, item:any) -> any</code>): reduce function func(any any)->any where the first parameter is the accumulator, the second is a list item | ||
* '''neutral''' (<code>any</code>): (optional) initial value of the accumulator, defaults to nil | * '''neutral''' (<code>any</code>): (optional) initial value of the accumulator, defaults to nil ''(optional)'' | ||
<span id="returns-21"></span> | |||
=== Returns === | |||
<code>any</code> | |||
== produce == | == produce == | ||
| Line 195: | Line 391: | ||
'''Allowed number of parameters:''' 3–3 | '''Allowed number of parameters:''' 3–3 | ||
<span id="parameters-22"></span> | |||
=== Parameters === | |||
* '''startstate''' (<code>any</code>): start state to begin with | * '''startstate''' (<code>any</code>): start state to begin with | ||
* '''condition''' (<code>func</code>): func that returns true whether the state will be inserted into the result or the loop is stopped | * '''condition''' (<code>func(state:any) -> bool</code>): func that returns true whether the state will be inserted into the result or the loop is stopped | ||
* '''iterator''' (<code>func</code>): func that produces the next state | * '''iterator''' (<code>func(state:any) -> any</code>): func that produces the next state | ||
<span id="returns-22"></span> | |||
=== Returns === | |||
<code>list</code> | |||
<span id="producen"></span> | |||
== produceN == | == produceN == | ||
returns a list with numbers from 0..n-1 | returns a list with numbers from 0..n-1, optionally mapped through a function | ||
'''Allowed number of parameters:''' 1–2 | |||
<span id="parameters-23"></span> | |||
=== Parameters === | |||
* '''n''' (<code>number</code>): number of elements to produce | |||
* '''fn''' (<code>func(index:int) -> any</code>): (optional) map function applied to each index ''(optional)'' | |||
<span id="returns-23"></span> | |||
=== Returns === | |||
<code>list</code> | |||
<span id="paralleln"></span> | |||
== parallelN == | |||
returns a list with numbers from 0..n-1 mapped in parallel through a function | |||
'''Allowed number of parameters:''' 2–2 | |||
<span id="parameters-24"></span> | |||
=== Parameters === | |||
* '''n''' (<code>number</code>): number of elements to produce | * '''n''' (<code>number</code>): number of elements to produce | ||
* '''fn''' (<code>func(index:int) -> any</code>): map function applied to each index in parallel | |||
<span id="returns-24"></span> | |||
=== Returns === | |||
<code>list</code> | |||
<span id="list-1"></span> | |||
== list? == | == list? == | ||
| Line 219: | Line 446: | ||
'''Allowed number of parameters:''' 1–1 | '''Allowed number of parameters:''' 1–1 | ||
<span id="parameters-25"></span> | |||
=== Parameters === | |||
* '''value''' (<code>any</code>): value to check | * '''value''' (<code>any</code>): value to check | ||
<span id="returns-25"></span> | |||
=== Returns === | |||
<code>bool</code> | |||
<span id="contains"></span> | |||
== contains? == | == contains? == | ||
| Line 230: | Line 463: | ||
'''Allowed number of parameters:''' 2–2 | '''Allowed number of parameters:''' 2–2 | ||
<span id="parameters-26"></span> | |||
=== Parameters === | |||
* '''list''' (<code>list</code>): list to check | * '''list''' (<code>list</code>): list to check | ||
* '''value''' (<code>any</code>): value to check | * '''value''' (<code>any</code>): value to check | ||
''' | <span id="returns-26"></span> | ||
=== Returns === | |||
<code>bool</code> | |||
== sql_in == | |||
tests SQL IN-list membership and returns nil when NULL makes the result UNKNOWN | |||
'''Allowed number of parameters:''' 2–2 | |||
<span id="parameters-27"></span> | |||
=== Parameters === | |||
* '''values''' (<code>list</code>): SQL IN-list values | |||
* '''value''' (<code>any</code>): value to find | |||
<span id="returns-27"></span> | |||
=== Returns === | |||
<code>bool</code> | |||
Revision as of 08:15, 27 August 2026
Lists
The Lists module provides comprehensive list manipulation and processing functions for the SCM programming language. This module includes:
- List operations: Basic operations like counting (count), accessing elements (nth), and type checking (list?)
- List construction: Building lists with append, cons, and unique operations (append_unique, merge_unique)
- List deconstruction: Extracting parts with car (head), cdr (tail), and filtering operations
- Functional programming: Higher-order functions like map, filter, reduce, and produce for advanced list processing
- List utilities: Searching (has?, contains?), merging, zipping, and flattening operations
- List generation: Creating sequences and ranges with produce and produceN functions
These functions provide the essential tools for working with lists as the primary data structure in functional programming with SCM.
← Back to Full SCM API documentation
list
constructs a list from its arguments
Allowed number of parameters: 0–10000
Parameters
- items (
any): items to put into the list (variadic)
Returns
list
count
counts the number of elements in the list
Allowed number of parameters: 1–1
Parameters
- list (
list): base list
Returns
int
nth
get the nth item of a list
Allowed number of parameters: 2–2
Parameters
- list (
list): base list - index (
number): index beginning from 0
Returns
any
slice
extract a sublist from start (inclusive) to end (exclusive). (slice list start end) returns elements list[start..end).
Allowed number of parameters: 3–3
Parameters
- list (
list): base list - start (
number): start index (inclusive) - end (
number): end index (exclusive)
Returns
list
reverse
returns a new list with elements in reversed order.
Allowed number of parameters: 1–1
Parameters
- list (
list): list to reverse
Returns
list
append
appends items to a list and return the extended list. The original list stays unharmed.
Allowed number of parameters: 1–10000
Parameters
- list (
list): base list - item... (
any): items to add (variadic)
Returns
list
append_unique
appends items to a list but only if they are new. The original list stays unharmed.
Allowed number of parameters: 1–10000
Parameters
- list (
list): base list - item... (
any): items to add (variadic)
Returns
list
cons
constructs a list from a head and a tail list
Allowed number of parameters: 2–2
Parameters
- car (
any): new head element - cdr (
list): tail that is appended after car
Returns
list
car
extracts the head of a list
Allowed number of parameters: 1–1
Parameters
- list (
list): list
Returns
any
cdr
extracts the tail of a list The tail of a list is a list with all items except the head.
Allowed number of parameters: 1–1
Parameters
- list (
list): list
Returns
list
cadr
extracts the second element of a list. Equivalent to (car (cdr x)).
Allowed number of parameters: 1–1
Parameters
- list (
list): list
Returns
any
zip
swaps the dimension of a list of lists. If one parameter is given, it is a list of lists that is flattened. If multiple parameters are given, they are treated as the components that will be zipped into the sub list
Allowed number of parameters: 0–10000
Parameters
- list (
any): list of lists of items (variadic)
Returns
list
merge
flattens a list of lists into a list containing all the subitems. If one parameter is given, it is a list of lists that is flattened. If multiple parameters are given, they are treated as lists that will be merged into one
Allowed number of parameters: 0–10000
Parameters
- list (
any): list of lists of items (variadic)
Returns
list
merge_unique
flattens a list of lists into a list containing all the subitems. Duplicates are filtered out.
Allowed number of parameters: 0–10000
Parameters
- list (
list): list of lists of items (variadic)
Returns
list
has?
checks if a list has a certain item (equal?)
Allowed number of parameters: 2–2
Parameters
- haystack (
list): list to search in - needle (
any): item to search for
Returns
bool
filter
returns a list that only contains elements that pass the filter function
Allowed number of parameters: 2–2
Parameters
- list (
list): list that has to be filtered - condition (
func(item:any) -> bool): filter condition func(item)->bool
Returns
list
find
returns the first list element that passes the condition function, or nil/default if none matches
Allowed number of parameters: 2–3
Parameters
- list (
list): list to search - condition (
func(item:any) -> bool): predicate func(any)->bool that is applied until the first match - default (
any): optional default value if nothing matches (optional)
Returns
any
map
returns a list that contains the results of a map function that is applied to the list
Allowed number of parameters: 2–2
Parameters
- list (
list): list that has to be mapped - map (
func(item:any) -> any): map function func(any)->any that is applied to each item
Returns
list
parallel_map
like map, but applies fn to each element in parallel using a worker pool limited to runtime.NumCPU()
Allowed number of parameters: 2–2
Parameters
- list (
list): list to map over in parallel - fn (
func(item:any) -> any): function applied to each element
Returns
list
parallel_map_mut
like parallel_map, but signals the optimizer that fn may have side effects
Allowed number of parameters: 2–2
Parameters
- list (
list): list to map over in parallel - fn (
func(item:any) -> any): function with side effects applied to each element
Returns
list
mapIndex
returns a list that contains the results of a map function that is applied to the list
Allowed number of parameters: 2–2
Parameters
- list (
list): list that has to be mapped - map (
func(index:int, item:any) -> any): map function func(i, any)->any that is applied to each item
Returns
list
reduce
returns a list that contains the result of a map function
Allowed number of parameters: 2–3
Parameters
- list (
list): list that has to be reduced - reduce (
func(acc:any, item:any) -> any): reduce function func(any any)->any where the first parameter is the accumulator, the second is a list item - neutral (
any): (optional) initial value of the accumulator, defaults to nil (optional)
Returns
any
produce
returns a list that contains produced items - it works like for(state = startstate, condition(state), state = iterator(state)) {yield state}
Allowed number of parameters: 3–3
Parameters
- startstate (
any): start state to begin with - condition (
func(state:any) -> bool): func that returns true whether the state will be inserted into the result or the loop is stopped - iterator (
func(state:any) -> any): func that produces the next state
Returns
list
produceN
returns a list with numbers from 0..n-1, optionally mapped through a function
Allowed number of parameters: 1–2
Parameters
- n (
number): number of elements to produce - fn (
func(index:int) -> any): (optional) map function applied to each index (optional)
Returns
list
parallelN
returns a list with numbers from 0..n-1 mapped in parallel through a function
Allowed number of parameters: 2–2
Parameters
- n (
number): number of elements to produce - fn (
func(index:int) -> any): map function applied to each index in parallel
Returns
list
list?
checks if a value is a list
Allowed number of parameters: 1–1
Parameters
- value (
any): value to check
Returns
bool
contains?
checks if a value is in a list; uses the equal?? operator
Allowed number of parameters: 2–2
Parameters
- list (
list): list to check - value (
any): value to check
Returns
bool
sql_in
tests SQL IN-list membership and returns nil when NULL makes the result UNKNOWN
Allowed number of parameters: 2–2
Parameters
- values (
list): SQL IN-list values - value (
any): value to find
Returns
bool