forked from davidson16807/relativity.scad
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogic.scad
More file actions
155 lines (130 loc) · 4.06 KB
/
logic.scad
File metadata and controls
155 lines (130 loc) · 4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/// logic operations
include <vectors.scad>
/// enable testing global variable
_TEST_ENABLED_ = true; // _TEST_ENABLED_
/// returns true if the given input is of type string
function is_not_string( unknown ) =
is_undef( unknown ) || ! is_string( unknown );
/// returns true if the given input is of type list
function is_not_list( input ) =
is_undef( input ) || ! is_list( input );
/**
return true if the given string is empty
but it must exist or we return undef
*/
function str_is_empty(string) =
is_not_string( string ) ? undef : string == "" ;
/**
return true when given string is undefined, OR
is empty, meaning the null string ( "" )
or undef if the input is not a string
*/
function str_is_undef_or_empty(string) =
is_not_string( string ) ?
undef
: is_undef(string) || string == "" ;
/**
return true when given a null string ( "" ) or
a string that has only space characters ( " " )
other types of whitespace are counted as non-space
characters
*/
function str_is_null_or_allspaces(string) =
len( search( " ", string, 0 )[0] ) == len( string );
/**
Empty items of any type count as false so all of these
evaluate as false:
false, 0, -0, "", [], and undef
Non-empty items of all types evaluate as true, so all of
these
* "false" is a string
* [0], [ [] ], and [false]
* the operation of 0/0
All arithmetic, logical, and relational operations
involving undef values evaluate as undef.
An exception is the relational operation undef==undef,
which is true. Other relational operators like < and >
return false when passed illegal arguments.
Note that numeric operations may also return 'nan'
(not-a-number) to indicate an illegal argument.
To illustrate, 0/false is undef, but 0/0 is 'nan'.
Although undef is a language value, 'nan' is not.
The value nan is the only OpenSCAD value that is not
equal to any other value, including itself.
Although you can test if a variable 'x' has the
undefined value using 'x == undef', you can't use
'x == 0/0' to test if x is Not A Number.
Instead, you must use 'x != x' to test if x is nan.
*/
/*
return true when any of the elements of the
given vector are true.
The elements of the vector will be evaluated
as boolean values according to the rules for
the different types of the language.
*/
function any(booleans, index=0) =
vec_is_empty( booleans) ?
undef
: index > len(booleans)?
false
: booleans[index]? true :
any(booleans, index+1)
;
/*
return true when all of the elements of the
given vector evaluate as true.
The elements of the vector will be evaluated
as boolean values according to the rules for
the different types of the lanuage.
*/
function all(booleans, index=0) =
vec_is_empty( booleans ) ?
undef
: index >= len(booleans) ?
true
: ! booleans[index]?
false
: all(booleans, index+1)
;
$logic_all=false;
function all_special(booleans, index=0) =
vec_is_empty( booleans ) ?
undef
: index >= len(booleans) ?
true
: for(b=booleans)
$logic_all = $logic_all && b
;
if( _TEST_ENABLED_ ) {
echo( "\n\ttesting logic" );
echo( "\tis_not_string" );
assert( ! is_not_string( "xyz" ) );
assert( ! is_not_string( "" ) );
assert( ! is_not_string( " " ) );
assert( is_not_string( [] ) );
assert( is_not_string( 42 ) );
assert( is_not_string( undef ) );
echo( "\tstr_is_empty" );
assert( ! str_is_empty( " " ) );
assert( str_is_empty( "" ) );
assert( ! str_is_empty( "xyz" ) );
assert( is_undef( str_is_empty( [] ) ) );
echo( "\tstr_is_undef_or_empty" );
assert( ! str_is_undef_or_empty( " " ) );
assert( str_is_undef_or_empty( "" ) );
assert( is_undef( str_is_undef_or_empty( undef ) ) );
assert( ! str_is_undef_or_empty( "xyz" ) );
assert( is_undef( str_is_undef_or_empty( [] ) ) );
assert( is_undef( str_is_undef_or_empty( 42 ) ) );
echo( "\tall" );
assert( ! all( [false,false,false] ) );
assert( all( [true,true,true] ) );
assert( ! all( [true,false,true] ) );
assert( is_undef( all( [] ) ) );
echo( "\tany" );
assert( ! any( [false,false,false] ) );
assert( any( [true,true,true] ) );
assert( any( [true,false,true] ) );
assert( is_undef( any( [] ) ) );
}