omdl  v1.0
OpenSCAD Mechanical Design Library
scalar_operate.scad
Go to the documentation of this file.
1 //! Scalar data type operations.
2 /***************************************************************************//**
3  \file
4  \author Roy Allen Sutton
5  \date 2015-2025
6 
7  \copyright
8 
9  This file is part of [omdl] (https://github.com/royasutton/omdl),
10  an OpenSCAD mechanical design library.
11 
12  The \em omdl is free software; you can redistribute it and/or modify
13  it under the terms of the [GNU Lesser General Public License]
14  (http://www.gnu.org/licenses/lgpl.html) as published by the Free
15  Software Foundation; either version 2.1 of the License, or (at
16  your option) any later version.
17 
18  The \em omdl is distributed in the hope that it will be useful,
19  but WITHOUT ANY WARRANTY; without even the implied warranty of
20  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21  Lesser General Public License for more details.
22 
23  You should have received a copy of the GNU Lesser General Public
24  License along with the \em omdl; if not, write to the Free Software
25  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26  02110-1301, USA; or see <http://www.gnu.org/licenses/>.
27 
28  \details
29 
30  \amu_define group_name (Scalar Operations)
31  \amu_define group_brief (Operations for scalar data types.)
32 
33  \amu_include (include/amu/doxyg_init_pd_gds_ipg.amu)
34 *******************************************************************************/
35 
36 // auto-tests (add to test results page)
37 /***************************************************************************//**
38  \amu_include (include/amu/validate_log.amu)
39  \amu_include (include/amu/validate_results.amu)
40 *******************************************************************************/
41 
42 // group(s) begin (test summary and includes-required)
43 /***************************************************************************//**
44  \amu_include (include/amu/doxyg_define_in_parent_open.amu)
45  \amu_include (include/amu/validate_summary.amu)
46  \amu_include (include/amu/includes_required.amu)
47 *******************************************************************************/
48 
49 // member-wide reference definitions
50 /***************************************************************************//**
51  \amu_define group_references
52  (
53  )
54 *******************************************************************************/
55 
56 // member-wide documentation and conventions
57 /***************************************************************************//**
58  \addtogroup \amu_eval(${group})
59  \details
60  \anchor \amu_eval(${group})_conventions
61  \par Conventions
62 
63  - The primary parameter is \p v (the value to operate on).
64  - The fallback / default parameter is always \p d.
65  - defined_or() treats only \b undef as 'undefined'; \b false, 0, and
66  \p empty_lst are considered defined and are returned unchanged.
67  - index_c() maps any integer \p i (including negative values) into the
68  circular range [f : f+l-1]. Negative \p i wraps from the end: index_c(-1, l)
69  returns the last valid index.
70  - The \p f parameter provides an optional non-zero base offset; it
71  defaults to 0 for zero-based indexing.
72 *******************************************************************************/
73 
74 //----------------------------------------------------------------------------//
75 // members
76 //----------------------------------------------------------------------------//
77 
78 //! Return given value, if defined, or a secondary value, if primary is not defined.
79 /***************************************************************************//**
80  \param v <value> A primary value.
81  \param d <value> A secondary value.
82 
83  \returns <value> \p v when it is defined and \p d otherwise.
84 
85  The value \p d is returned only when \p v is equal to \b undef.
86 *******************************************************************************/
87 function defined_or
88 (
89  v,
90  d
91 ) = is_undef(v) ? d : v;
92 
93 //! Return given value, if it is Boolean, else return a secondary value, if not.
94 /***************************************************************************//**
95  \param v <value> A primary value.
96  \param d <value> A secondary value.
97 
98  \returns <value> \p v when it is Boolean and \p d otherwise.
99 *******************************************************************************/
100 function boolean_or
101 (
102  v,
103  d
104 ) = is_bool(v) ? v : d;
105 
106 //! Return given value, if it is a number, else return a secondary value, if not.
107 /***************************************************************************//**
108  \param v <value> A primary value.
109  \param d <value> A secondary value.
110 
111  \returns <value> \p v when it is a number and \p d otherwise.
112 *******************************************************************************/
113 function number_or
114 (
115  v,
116  d
117 ) = is_num(v) ? v : d;
118 
119 //! Return given value, if it is a string, else return a secondary value, if not.
120 /***************************************************************************//**
121  \param v <value> A primary value.
122  \param d <value> A secondary value.
123 
124  \returns <value> \p v when it is a string and \p d otherwise.
125 *******************************************************************************/
126 function string_or
127 (
128  v,
129  d
130 ) = is_string(v) ? v : d;
131 
132 //! Return given value, if it is a list, else return a secondary value, if not.
133 /***************************************************************************//**
134  \param v <value> A primary value.
135  \param d <value> A secondary value.
136 
137  \returns <value> \p v when it is a list and \p d otherwise.
138 *******************************************************************************/
139 function list_or
140 (
141  v,
142  d
143 ) = is_list(v) ? v : d;
144 
145 //! Return a circular index position.
146 /***************************************************************************//**
147  \param i <integer> A integer position.
148  \param l <integer> The range length.
149  \param f <integer> The starting index position.
150 
151  \returns <integer> The index position mapped into a circular list
152  within the range <tt>[f : l+f-1]</tt>.
153 
154  \details
155 
156  Negative values of \p i wrap from the end of the range backwards.
157  For example, with \p l=5 and \p f=0, an index of \c -1 maps to
158  position \c 4, \c -2 maps to \c 3, and so on.
159 *******************************************************************************/
160 function index_c
161 (
162  i,
163  l,
164  f = 0
165 ) = f + (((i % l) + l) % l);
166 
167 //! @}
168 //! @}
169 
170 //----------------------------------------------------------------------------//
171 // openscad-amu auxiliary scripts
172 //----------------------------------------------------------------------------//
173 
174 /*
175 BEGIN_SCOPE validate;
176  BEGIN_OPENSCAD;
177  include <omdl-base.scad>;
178  include <common/validation.scad>;
179 
180  function fmt( id, td, ev, v1, v2, v3 ) = map_validate_fmt(id, td, ev, v1, v2, v3);
181  function v1( db, id ) = map_validate_get_v1(db, id);
182  function v2( db, id ) = map_validate_get_v2(db, id);
183 
184  map_test_defined_or =
185  [
186  fmt("t01", "Undefined", 1, undef, 1),
187  fmt("t02", "A small value", eps, eps, 2),
188  fmt("t03", "Infinity", number_inf, number_inf, 3),
189  fmt("t04", "Max number", number_max, number_max, 4),
190  fmt("t05", "Undefined list", [undef], [undef], 5),
191  fmt("t06", "Short range", [0:9], [0:9], 6),
192  fmt("t07", "Empty string", empty_str, empty_str, 7),
193  fmt("t08", "Empty list", empty_lst, empty_lst, 8)
194  ];
195 
196  db = map_validate_init( map_test_defined_or, "defined_or" );
197  map_validate_start( db );
198 
199  for ( id = map_validate_get_ids( db ) )
200  map_validate( db, id, 2, defined_or ( v1(db, id), v2(db, id) ) );
201 
202  // skip test for:
203  // * boolean_or()
204  // * number_or()
205  // * string_or()
206  // * list_or()
207 
208  // end_include
209  END_OPENSCAD;
210 
211  BEGIN_MFSCRIPT;
212  include --path "${INCLUDE_PATH}" {var_init,var_gen_term}.mfs;
213  include --path "${INCLUDE_PATH}" scr_make_mf.mfs;
214  END_MFSCRIPT;
215 END_SCOPE;
216 */
217 
218 //----------------------------------------------------------------------------//
219 // end of file
220 //----------------------------------------------------------------------------//
function boolean_or(v, d)
Return given value, if it is Boolean, else return a secondary value, if not.
function list_or(v, d)
Return given value, if it is a list, else return a secondary value, if not.
function string_or(v, d)
Return given value, if it is a string, else return a secondary value, if not.
function number_or(v, d)
Return given value, if it is a number, else return a secondary value, if not.
function index_c(i, l, f=0)
Return a circular index position.
function defined_or(v, d)
Return given value, if defined, or a secondary value, if primary is not defined.