omdl  v1.0
OpenSCAD Mechanical Design Library
Iterable Operations

Operations for iterable data types. More...

+ Collaboration diagram for Iterable Operations:

Files

file  iterable_operate.scad
 Iterable data type operations.
 

Functions and/or Modules

function defined_e_or (v, i, d)
 Returns an element from an iterable if it exists, or a default value if not. More...
 
function defined_eon_or (v, i, d)
 Returns the list element or scalar numeric value if defined, otherwise returns the default value. More...
 
function defined_eonb_or (v, i, d)
 Returns the list element or scalar numeric or boolean value if defined, otherwise returns the default value. More...
 
function defined_fle_or (v, i, d)
 Return the first defined specified element in a list of lists, otherwise returns the given default value. More...
 
function defined_ei_or (v, i, d, n)
 Returns an element from an iterable if it exists and is a iterable, otherwise returns a default value. More...
 
function find_all (mv, v)
 Find all occurrences of a match value in an iterable value. More...
 
function find (mv, v, c=1, i, i1=0, i2)
 Find the occurrences of a match value in an iterable value. More...
 
function count (mv, v, s=true, i)
 Count all occurrences of a match value in an iterable value. More...
 
function exists (mv, v, s=true, i)
 Check for the existence of a match value in an iterable value. More...
 
function exists_e (i, v)
 Test if an element exists at a specified index of an iterable value. More...
 
function first (v)
 Return the first element of an iterable value. More...
 
function second (v)
 Return the second element of an iterable value. More...
 
function third (v)
 Return the third element of an iterable value. More...
 
function last (v)
 Return the last element of an iterable value. More...
 
function middle (v)
 Return the middle element of an iterable value. More...
 
function first2 (v)
 Return a list containing the first two elements of an iterable value. More...
 
function first3 (v)
 Return a list containing the first three elements of an iterable value. More...
 
function last2 (v)
 Return a list containing the last two elements of an iterable value. More...
 
function last3 (v)
 Return a list containing the last three elements of an iterable value. More...
 
function firstn (v, n=1)
 Return a list containing the first n elements of an iterable value. More...
 
function lastn (v, n=1)
 Return a list containing the last n elements of an iterable value. More...
 
function headn (v, n=1)
 Return a list containing all but the last n elements of an iterable value. More...
 
function tailn (v, n=1)
 Return a list containing all but the first n elements of an iterable value. More...
 
function reverse (v)
 Reverse the elements of an iterable value. More...
 
function shift_cd (v, n=0, r=true, d=false)
 Circularly shift the elements of an iterable, with an optional element drop. More...
 
function shift_ci (v, n=0, r=true, i)
 Shift-in a value to the elements of a given iterable value. More...
 
function select_r (v, i)
 Select a range of elements from an iterable value. More...
 
function sequence_ns (v, n=1, s=1, w=false)
 Return a list of all n-element sequential-subsets of an iterable value. More...
 
function combine_ns (v, n)
 Return all n-sized set combinations of iterable value. More...
 
function permute_ns (v, n)
 Return all ordered n-sized set permutations of iterable value. More...
 
function permute_ns_r (v, n)
 Return all ordered n-size set permutations of iterable value with repetition allowed. More...
 
function append_e (nv, v, r=true, j=true, l=true)
 Append a value to each element of an iterable value. More...
 
function append_v (nv, v, n=0, r=false)
 Append a value to the end or beginning of an iterable value. More...
 
function insert (nv, v, i=0, mv, mi=0, s=true, si)
 Insert a new value into an iterable value. More...
 
function delete_first (v, mv, mc=1)
 Delete the first occurrence(s) of a matched value from an iterable value. More...
 
function delete_each (v, mv)
 Delete each match value from an iterable value. More...
 
function delete (v, i, mv, mc=1, s=true, si)
 Delete elements from an iterable value. More...
 
function replace_first (v, mv, nv, mc=1)
 Replace the first occurrence(s) of a matched value in iterable value. More...
 
function strip (v, mv=empty_lst)
 Strip all matching values from an iterable value. More...
 
function mask (v, m, r=false, o=0, u=undef, z=0)
 Apply a binary mask to an interable value. More...
 
function unique (v)
 Return a list of the unique elements of an iterable value. More...
 
function common (v1, v2, e=empty_lst)
 Return a list of the common elements of two iterable values. More...
 
function not_common (v1, v2)
 Return a list of the elements not present in both iterable values. More...
 

Usage Details

Operations for iterable data types.

Validation Summary

filegroupscriptresultsno testskippedpassedfailedwarning
datatypes/iterable_operate.scadIterable OperationsScriptResults0033000

No Failures

See complete validation results.

Requires:
include <omdl-base.scad>;

Conventions

Function and/or Module Documentation

◆ defined_e_or()

function defined_e_or ( ,
,
 
)

Returns an element from an iterable if it exists, or a default value if not.

Parameters
v<iterable> An iterable value.
i<integer> An element index.
d<value> A default value.
Returns
(1)

v[i] if it is defined, or d otherwise.

◆ defined_eon_or()

function defined_eon_or ( ,
,
 
)

Returns the list element or scalar numeric value if defined, otherwise returns the default value.

Parameters
v<value> A value.
i<integer> An element index.
d<value> A default value.
Returns
(1) <number> v if it is a scalar numeric value; (2)

v[i] if it is defined, otherwise the default value d.

+ Here is the caller graph for this function:

◆ defined_eonb_or()

function defined_eonb_or ( ,
,
 
)

Returns the list element or scalar numeric or boolean value if defined, otherwise returns the default value.

Parameters
v<value> A value.
i<integer> An element index.
d<value> A default value.
Returns
(1)

v if it is a scalar numeric or boolean value; (2)

v[i] if it is defined, otherwise the default value d.

◆ defined_fle_or()

function defined_fle_or ( ,
,
 
)

Return the first defined specified element in a list of lists, otherwise returns the given default value.

Parameters
v<value> A list of n elements lists.
i<integer> The specified element index.
d<value> A default value.
Returns
(1) <value> for the given v, a list of n lists, return the first element v[j][i] that is defined where j = [0 : n], otherwise the default value d.
+ Here is the caller graph for this function:

◆ defined_ei_or()

function defined_ei_or ( ,
,
,
 
)

Returns an element from an iterable if it exists and is a iterable, otherwise returns a default value.

Parameters
v<value> A value.
i<integer> An element index.
d<value> A default value.
n<integer> The optional expected element length.
Returns
(1) v[i], a <list-n> if the element is iterable of length n (or <list> if n is unspecified), (2) or the default value d if the element is not defined.
+ Here is the caller graph for this function:

◆ find_all()

function find_all ( mv  ,
 
)

Find all occurrences of a match value in an iterable value.

Parameters
mv<value> A match value.
v<iterable> An iterable value.
Returns
(1) <list> A list of indexes where elements match mv. (2) Returns empty_lst when no element of v matches mv or when v is not iterable.

◆ find()

function find ( mv  ,
,
= 1,
,
i1  = 0,
i2   
)

Find the occurrences of a match value in an iterable value.

Parameters
mv<value> A match value.
v<iterable> An iterable value.
c<integer> A match count. For (c >= 1), return the first c matches. For (c <= 0), return all matches.
i<integer> The secondary element index to match for when the elements of v are themselves iterable elements.
i1<integer> The element index of v where the search begins.
i2<integer> The element index of v where the search ends.
Returns
(1) <list> A list of indexes where elements match mv. (2) Returns empty_lst when no element of v matches mv or when v is not iterable.

The use-cases for find() and search() are summarized in the following tables.

Find:

mv / v string list of scalars list of iterables
scalar (a) (b) see note 1
string (c) (b) see note 1
list of scalars (b) see note 1
list of iterables (b) see note 1

Search:

mv / v string list of scalars list of iterables
scalar (a) (b)
string (d) invalid (e) see note 2
list of scalars (f) (g)
list of iterables (g)

Key:

  • (a) Identify each element of v that equals mv.
  • (b) Identify each element of v where mv equals the element at the specified column index, i, of each iterable value in v.
  • (c) If, and only if, mv is a single character, identify each character in v that equals mv.
  • (d) For each character of mv, identify where it exists in v. empty_lst is returned for each character of mv absent from v.
  • (e) For each character of mv, identify where it exists in v either as a numeric value or as a character at the specified column index, i. empty_lst is returned for each character of mv absent from v.
  • (f) For each scalar of mv, identify where it exists in v. empty_lst is returned for each scalar of mv absent from v.
  • (g) For each element of mv, identify where it equals the element at the specified column index, i, of each iterable value in v. empty_lst is returned for each element of mv absent from v in the specified column index.
Note
1: When i is specified, that element column is compared. Otherwise, the entire element is compared. Functions find() and search() behave differently in this regard.
2: Invalid use combination when any element of v is a string. However, an element that is a list of one or more strings is valid. In which case, only the first character of each string element is considered.

◆ count()

function count ( mv  ,
,
= true,
 
)

Count all occurrences of a match value in an iterable value.

Parameters
mv<value> A match value.
v<iterable> An iterable value.
s<boolean> Element matching search method.
i<integer> The secondary element index to match for when the elements of v are themselves iterable elements.
Returns
(1) <integer> The number of times mv occurs in the list.

When s == true, search() is used to match elements. When s == false, find() is used.

+ Here is the caller graph for this function:

◆ exists()

function exists ( mv  ,
,
= true,
 
)

Check for the existence of a match value in an iterable value.

Parameters
mv<value> A match value.
v<iterable> An iterable value.
s<boolean> Element matching search method.
i<integer> The secondary element index to match for when the elements of v are themselves iterable elements.
Returns
(1) <boolean> true when mv exists in the list and false otherwise.

When s == true, search() is used to match elements. When s == false, find() is used.

◆ exists_e()

function exists_e ( ,
 
)

Test if an element exists at a specified index of an iterable value.

Parameters
i<integer> An element index.
v<iterable> An iterable value.
Returns
(1) <boolean> true when the element v[i], exists and false otherwise. (2) Returns undef when i is not an number.
Note
This functions does not consider the value of the element at the index position, but rather if an element exists at the index position i.

◆ first()

function first ( )

Return the first element of an iterable value.

Parameters
v<iterable> An iterable value.
Returns
(1) <value> The first element of v. (2) Returns undef when v is not defined, is not iterable, or is empty.
Note
Value may also be a range to obtain its start value.
+ Here is the caller graph for this function:

◆ second()

function second ( )

Return the second element of an iterable value.

Parameters
v<iterable> An iterable value.
Returns
(1) <value> The second element of v. (2) Returns undef when v is not defined, is not iterable, or is empty.
Note
Value may also be a range to obtain its step value.
+ Here is the caller graph for this function:

◆ third()

function third ( )

Return the third element of an iterable value.

Parameters
v<iterable> An iterable value.
Returns
(1) <value> The third element of v. (2) Returns undef when v is not defined, is not iterable, or is empty.
Note
Value may also be a range to obtain its end value.
+ Here is the caller graph for this function:

◆ last()

function last ( )

Return the last element of an iterable value.

Parameters
v<iterable> An iterable value.
Returns
(1) <value> The last element of v. (2) Returns undef when v is not defined, is not iterable, or is empty.
+ Here is the caller graph for this function:

◆ middle()

function middle ( )

Return the middle element of an iterable value.

Parameters
v<iterable> An iterable value.
Returns
(1) <value> The middle element of v. (2) Returns undef when v is not defined, is not iterable, or is empty.

◆ first2()

function first2 ( )

Return a list containing the first two elements of an iterable value.

Parameters
v<iterable> An iterable value.
Returns
(1) <list> A list containing the first two elements of v. (2) Returns undef when v is not defined, is not iterable, is empty or has less than two elements
Note
Value may also be a range.

◆ first3()

function first3 ( )

Return a list containing the first three elements of an iterable value.

Parameters
v<iterable> An iterable value.
Returns
(1) <list> A list containing the first three elements of v. (2) Returns undef when v is not defined, is not iterable, is empty, or has less than three elements.
Note
Value may also be a range.

◆ last2()

function last2 ( )

Return a list containing the last two elements of an iterable value.

Parameters
v<iterable> An iterable value.
Returns
(1) <list> A list containing the last two elements of v. (2) Returns undef when v is not defined, is not iterable, is empty, or has less than two elements.

◆ last3()

function last3 ( )

Return a list containing the last three elements of an iterable value.

Parameters
v<iterable> An iterable value.
Returns
(1) <list> A list containing the last three elements of v. (2) Returns undef when v is not defined, is not iterable, is empty, or has less than three elements.

◆ firstn()

function firstn ( ,
= 1 
)

Return a list containing the first n elements of an iterable value.

Parameters
v<iterable> An iterable value.
n<integer> The element count.
Returns
(1) <list> A list containing the first n elements of v. (2) Returns undef when v is not defined, is not iterable, or is empty.

When n is greater than the length of the iterable v, the list will stop at the last element of v.

+ Here is the caller graph for this function:

◆ lastn()

function lastn ( ,
= 1 
)

Return a list containing the last n elements of an iterable value.

Parameters
v<iterable> An iterable value.
n<integer> The element count.
Returns
(1) <list> A list containing the last n elements of v. (2) Returns undef when v is not defined, is not iterable, or is empty.

When n is greater than the length of the iterable v, the list will start at the first element of v.

+ Here is the caller graph for this function:

◆ headn()

function headn ( ,
= 1 
)

Return a list containing all but the last n elements of an iterable value.

Parameters
v<iterable> An iterable value.
n<integer> The element count.
Returns
(1) <list> A list containing all but the last n elements of v. (2) Returns empty_lst when v contains fewer than n elements. (3) Returns undef when v is not defined, is not iterable, or is empty.
+ Here is the caller graph for this function:

◆ tailn()

function tailn ( ,
= 1 
)

Return a list containing all but the first n elements of an iterable value.

Parameters
v<iterable> An iterable value.
n<integer> The element count.
Returns
(1) <list> A list containing all but the first n elements of v. (2) Returns empty_lst when v contains fewer than n elements. (3) Returns undef when v is not defined, is not iterable, or is empty.
+ Here is the caller graph for this function:

◆ reverse()

function reverse ( )

Reverse the elements of an iterable value.

Parameters
v<iterable> An iterable value.
Returns
(1) <list> A list containing the elements of v in reversed order. (2) Returns empty_lst when v is empty. (3) Returns undef when v is not defined or is not iterable.
+ Here is the caller graph for this function:

◆ shift_cd()

function shift_cd ( ,
= 0,
= true,
= false 
)

Circularly shift the elements of an iterable, with an optional element drop.

Parameters
v<iterable> An iterable value.
n<integer> The element shift count.
r<boolean> Shift the elements to the right (or left).
d<boolean> Drop elements outside of iterable value shift window.
Returns
(1) <list> A list containing the elements of v shifted by n elements. (2) Returns undef when v is not defined or is not iterable.

The shift count n may be positive or negative.

+ Here is the caller graph for this function:

◆ shift_ci()

function shift_ci ( ,
= 0,
= true,
 
)

Shift-in a value to the elements of a given iterable value.

Parameters
v<iterable> The iterable value.
n<integer> The element shift count.
r<boolean> Shift the elements to the right (or left).
i<value> The value to shit-in on the left or right.
Returns
(1) <list> A list containing the elements of v shifted by n elements with the value i inserted on the left or right side of the given iterable value. (2) Returns undef when v is not defined or is not iterable.

The shift count n may be positive or negative.

◆ select_r()

function select_r ( ,
 
)

Select a range of elements from an iterable value.

Parameters
v<iterable> An iterable value.
i<range | list | integer> The index selection.
Returns
(1) <list> A list containing the selected elements. (2) Returns undef when v is not defined, is not iterable, or when i does not map to an element of v. (3) Returns empty_lst when v is empty.

◆ sequence_ns()

function sequence_ns ( ,
= 1,
= 1,
= false 
)

Return a list of all n-element sequential-subsets of an iterable value.

Parameters
v<iterable> An iterable value.
n<integer> The number of elements for each subset.
s<integer> The iteration step size.
w<boolean> Use wrap-at-end circular subset selection.
Returns
(1) <list-list> A list of all n-element sequential subsets of v skipping s elements of v between each subset selection. (2) Returns empty_lst when v is empty, is not defined or is not iterable.

Example

v = [1, 2, 3, 4];
sequence_ns( v, 3, 1, false ); // [ [1,2,3], [2,3,4] ]
sequence_ns( v, 3, 1, true ); // [ [1,2,3], [2,3,4], [3,4,1], [4,1,2] ]
function sequence_ns(v, n=1, s=1, w=false)
Return a list of all n-element sequential-subsets of an iterable value.
+ Here is the caller graph for this function:

◆ combine_ns()

function combine_ns ( ,
 
)

Return all n-sized set combinations of iterable value.

Parameters
v<iterable> An iterable value.
n<integer> The set size size.
Returns
(1) <list-list> A list of all n-element combinations of v. (2) Returns empty_lst when v is not iterable.

◆ permute_ns()

function permute_ns ( ,
 
)

Return all ordered n-sized set permutations of iterable value.

Parameters
v<iterable> An iterable value.
n<integer> The set size size.
Returns
(1) <list-list> A list all ordered n-length permutations of v without repetition of elements. (2) Returns empty_lst when v is not iterable.

◆ permute_ns_r()

function permute_ns_r ( ,
 
)

Return all ordered n-size set permutations of iterable value with repetition allowed.

Parameters
v<iterable> An iterable value.
n<integer> The set size size.
Returns
(1) <list-list> A list all ordered n-length permutations of v with element repetition allowed. (2) Returns empty_lst when v is not iterable.

◆ append_e()

function append_e ( nv  ,
,
= true,
= true,
= true 
)

Append a value to each element of an iterable value.

Parameters
nv<value> A new value to append.
v<iterable> An iterable value.
r<boolean> Reduce list element before appending.
j<boolean> Join each appendage as a separate list.
l<boolean> Append new value to last element.
Returns
(1) <list> A list with nv appended to each element of v. (2) Returns undef when v is not defined or is not iterable.

Appending with r == true causes each element of nv to be appended to the elements of each iterable value. When r == false, each element of nv is appended to the iterable value itself. To append a list of elements together as a list to v, enclose the elements of nv with a second set of brackets.

Example

v1=[["a"], ["b"], ["c"], ["d"]];
v2=[1, 2, 3];
echo( append_e( v2, v1 ) );
echo( append_e( v2, v1, r=false ) );
echo( append_e( v2, v1, j=false, l=false ) );
function append_e(nv, v, r=true, j=true, l=true)
Append a value to each element of an iterable value.

Result

ECHO: [["a", 1, 2, 3], ["b", 1, 2, 3], ["c", 1, 2, 3], ["d", 1, 2, 3]]
ECHO: [[["a"], 1, 2, 3], [["b"], 1, 2, 3], [["c"], 1, 2, 3], [["d"], 1, 2, 3]]
ECHO: ["a", 1, 2, 3, "b", 1, 2, 3, "c", 1, 2, 3, "d"]

◆ append_v()

function append_v ( nv  ,
,
= 0,
= false 
)

Append a value to the end or beginning of an iterable value.

Parameters
nv<value> The value to append.
v<iterable> An iterable value.
n<integer> The element shift count.
r<boolean> Shift the elements to the right (or left).
Returns
(1) <list> A list containing the elements of v shifted by n elements with the value a appended the given iterable value as specified. (2) Returns undef when v is not defined or is not iterable.

The shift count n may be positive or negative.

◆ insert()

function insert ( nv  ,
,
= 0,
mv  ,
mi  = 0,
= true,
si   
)

Insert a new value into an iterable value.

Parameters
nv<value> A new value to insert.
v<iterable> An iterable value.
i<integer> The index insert position.
mv<list | string | value> Match value candidates.
mi<integer> The matched selection index.
s<boolean> Element matching search method.
si<integer> The search element index when matching.
Returns
(1) <list> The list with nv inserted into v at the specified position. (2) Returns undef when no value of mv exists in v, when (mi + 1) exceeds the matched element count, when i does not map to an element of v, or when v is not defined or is not iterable.

When s == true, search() is used to match elements. When s == false, find() is used.

The insert position can be specified by an index, an element match value, or list of potential match values. When multiple matches exists, mi indicates the insert position. When more than one insert position criteria is specified, the order of precedence is: (1) mv then (2) i. To insert a list of elements together as a list to v, enclose the elements of nv with a second set of brackets.

◆ delete_first()

function delete_first ( ,
mv  ,
mc  = 1 
)

Delete the first occurrence(s) of a matched value from an iterable value.

Parameters
v<iterable> The iterable value.
mv<value> The match value.
mc<integer> A match count. For (mc >= 1), remove the first mc matches. For (mc = 0), remove all matches.
Returns
(1) <list> The list with the first mc occurrences of the match value removed. (2) Returns undef when v is not defined, or is not iterable.

◆ delete_each()

function delete_each ( ,
mv   
)

Delete each match value from an iterable value.

Parameters
v<iterable> An iterable value.
mv<iterable> The list of match values.
Returns
(1) <list> The list with all occurrence of the match value removed.

◆ delete()

function delete ( ,
,
mv  ,
mc  = 1,
= true,
si   
)

Delete elements from an iterable value.

Parameters
v<iterable> An iterable value.
i<range | list | integer> Deletion Indexes.
mv<list | string | value> Match value candidates.
mc<integer> A match count. For (mc >= 1), remove the first mc matches. For (mc <= 0), remove all matches.
s<boolean> Element matching search method.
si<integer> The element column index when matching.
Returns
(1) <list> The list with all specified elements removed. (2) Returns undef when i does not map to an element of v, when v is not defined, or is not iterable.

When s == true, search() is used to match elements. When s == false, find() is used.

The elements to delete can be specified by an index position, a list of index positions, an index range, an element match value, or a list of element match values (when using search()). When mv is a list of match values, all values of mv that exists in v are candidates for deletion. For each matching candidate, mc indicates the quantity to remove. When more than one deletion criteria is specified, the order of precedence is: (1) mv then (2) i.

◆ replace_first()

function replace_first ( ,
mv  ,
nv  ,
mc  = 1 
)

Replace the first occurrence(s) of a matched value in iterable value.

Parameters
v<iterable> The iterable value.
mv<value> The match value.
nv<value> The new value.
mc<integer> A match count. For (mc >= 1), replaces the first mc matches. For (mc = 0), replaces all matches.
Returns
(1) <list> The list with the first \mc occurrences of the match value replaced by nv. (2) Returns undef when v is not defined, or is not iterable.

◆ strip()

function strip ( ,
mv  = empty_lst 
)

Strip all matching values from an iterable value.

Parameters
v<iterable> An iterable value.
mv<value> A match value.
Returns
(1) <list> The list 'v' with all elements equal mv removed. (2) Returns undef when v is not defined or is not iterable.

◆ mask()

function mask ( ,
,
= false,
= 0,
= undef,
= 0 
)

Apply a binary mask to an interable value.

Parameters
v<iterable> An iterable value.
m<iterable> A binary mask list (or string) of 0 or 1.
r<boolean> Right align the mask to v value.
o<integer> A positive or negative mask offset.
u<value> The value assigned to elements of the mask that does not exists or are undefined in v.
z<value> The value assigned to masked elements.
Returns
(1) <value> A list containing the masked values of v. (2) Returns v as a list when m is not defined. (3) Returns undef when m is not iterable or contains values other than zero or one.

This mask may be specified as a list or string and is composed of ones and zeros. One indicates that an element value of v is passed and a zero indicates that a value of v is to be replaced with z.

◆ unique()

function unique ( )

Return a list of the unique elements of an iterable value.

Parameters
v<iterable> An iterable value.
Returns
(1) <list> A list of unique elements with order preserved. (2) Returns undef when v is not defined or is not iterable.
Warning
Any and all list elements of v that have the value of undef are ignored and is not considered to be a unique.

◆ common()

function common ( v1  ,
v2  ,
= empty_lst 
)

Return a list of the common elements of two iterable values.

Parameters
v1<iterable> The first iterable value.
v2<iterable> The second iterable value.
e<iterable> The common elements list (used for recursion value tracking or may be initialed by user).
Returns
(1) <list> The list of elements common to both iterable values as list intersection with a one-to-one correspondence among the elements. (2) Returns empty_lst when either v1 or v2 is empty.

◆ not_common()

function not_common ( v1  ,
v2   
)

Return a list of the elements not present in both iterable values.

Parameters
v1<iterable> The first iterable value.
v2<iterable> The second iterable value.
Returns
(1) <list> The list of elements that do not exists in both iterable values as a list difference with a one-to-one correspondence among the elements. (2) Returns empty_lst when v1 and v2 are identical, or when both are empty.