omdl  v0.9.8
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/pgid_path_pstem_pg.amu)
34 *******************************************************************************/
35 
36 //----------------------------------------------------------------------------//
37 // validation.
38 //----------------------------------------------------------------------------//
39 
40 /***************************************************************************//**
41  \amu_include (include/amu/validate_log_th.amu)
42  \amu_include (include/amu/validate_log_td.amu)
43  \amu_include (include/amu/validate_results.amu)
44 *******************************************************************************/
45 
46 //----------------------------------------------------------------------------//
47 // group.
48 //----------------------------------------------------------------------------//
49 
50 /***************************************************************************//**
51  \amu_include (include/amu/group_in_parent_start.amu)
52  \amu_include (include/amu/includes_required.amu)
53 
54  \details
55 
56  \amu_include (include/amu/validate_summary.amu)
57 *******************************************************************************/
58 
59 //----------------------------------------------------------------------------//
60 
61 //! Return given value, if defined, or a secondary value, if primary is not defined.
62 /***************************************************************************//**
63  \param v <value> A primary value.
64  \param d <value> A secondary value.
65 
66  \returns <value> \p v when it is defined and \p d otherwise.
67 
68  The value \p d is returned only when \p v is equal to \b undef.
69 *******************************************************************************/
70 function defined_or
71 (
72  v,
73  d
74 ) = is_undef(v) ? d : v;
75 
76 //! Return given value, if it is Boolean, else return a secondary value, if not.
77 /***************************************************************************//**
78  \param v <value> A primary value.
79  \param d <value> A secondary value.
80 
81  \returns <value> \p v when it is Boolean and \p d otherwise.
82 *******************************************************************************/
83 function boolean_or
84 (
85  v,
86  d
87 ) = is_bool(v) ? v : d;
88 
89 //! Return given value, if it is a number, else return a secondary value, if not.
90 /***************************************************************************//**
91  \param v <value> A primary value.
92  \param d <value> A secondary value.
93 
94  \returns <value> \p v when it is a number and \p d otherwise.
95 *******************************************************************************/
96 function number_or
97 (
98  v,
99  d
100 ) = is_num(v) ? v : d;
101 
102 //! Return given value, if it is a string, else return a secondary value, if not.
103 /***************************************************************************//**
104  \param v <value> A primary value.
105  \param d <value> A secondary value.
106 
107  \returns <value> \p v when it is a string and \p d otherwise.
108 *******************************************************************************/
109 function string_or
110 (
111  v,
112  d
113 ) = is_string(v) ? v : d;
114 
115 //! Return given value, if it is a list, else return a secondary value, if not.
116 /***************************************************************************//**
117  \param v <value> A primary value.
118  \param d <value> A secondary value.
119 
120  \returns <value> \p v when it is a list and \p d otherwise.
121 *******************************************************************************/
122 function list_or
123 (
124  v,
125  d
126 ) = is_list(v) ? v : d;
127 
128 //! Return a circular index position.
129 /***************************************************************************//**
130  \param i <integer> A integer position.
131  \param l <integer> The range length.
132  \param f <integer> The starting index position.
133 
134  \returns <integer> The index position mapped into a circular list
135  within the range <tt>[f : l+f-1]</tt>.
136 *******************************************************************************/
137 function index_c
138 (
139  i,
140  l,
141  f = 0
142 ) = f + (((i % l) + l) % l);
143 
144 //! @}
145 //! @}
146 
147 //----------------------------------------------------------------------------//
148 // openscad-amu auxiliary scripts
149 //----------------------------------------------------------------------------//
150 
151 /*
152 BEGIN_SCOPE validate;
153  BEGIN_OPENSCAD;
154  include <omdl-base.scad>;
155  include <common/validation.scad>;
156 
157  function fmt( id, td, ev, v1, v2, v3 ) = map_validate_fmt(id, td, ev, v1, v2, v3);
158  function v1( db, id ) = map_validate_get_v1(db, id);
159  function v2( db, id ) = map_validate_get_v2(db, id);
160 
161  map_test_defined_or =
162  [
163  fmt("t01", "Undefined", 1, undef, 1),
164  fmt("t02", "A small value", eps, eps, 2),
165  fmt("t03", "Infinity", number_inf, number_inf, 3),
166  fmt("t04", "Max number", number_max, number_max, 4),
167  fmt("t05", "Undefined list", [undef], [undef], 5),
168  fmt("t06", "Short range", [0:9], [0:9], 6),
169  fmt("t07", "Empty string", empty_str, empty_str, 7),
170  fmt("t08", "Empty list", empty_lst, empty_lst, 8)
171  ];
172 
173  db = map_validate_init( map_test_defined_or, "defined_or" );
174  map_validate_start( db );
175 
176  for ( id = map_validate_get_ids( db ) )
177  map_validate( db, id, 2, defined_or ( v1(db, id), v2(db, id) ) );
178 
179  // skip test for:
180  // * boolean_or()
181  // * number_or()
182  // * string_or()
183  // * list_or()
184 
185  // end_include
186  END_OPENSCAD;
187 
188  BEGIN_MFSCRIPT;
189  include --path "${INCLUDE_PATH}" {var_init,var_gen_term}.mfs;
190  include --path "${INCLUDE_PATH}" scr_make_mf.mfs;
191  END_MFSCRIPT;
192 END_SCOPE;
193 */
194 
195 //----------------------------------------------------------------------------//
196 // end of file
197 //----------------------------------------------------------------------------//
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.