-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresolveTruthinessChain.m
60 lines (55 loc) · 1.44 KB
/
resolveTruthinessChain.m
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
function out = resolveTruthinessChain(varargin)
% Evaluate parameters as a truthy value if exec. of arguments else false
%
%
% | op | prev | curr | out |
% | --- | ---- | ---- | ----- |
% | and | T1 | T2 | T2 |
% | and | T1 | F2 | false |
% | and | F1 | T2 | false |
% | and | F1 | F2 | false |
% | or | T1 | T2 | T1 |
% | or | T1 | F2 | T1 |
% | or | F1 | T2 | T2 |
% | or | F1 | F2 | false |
%
% check if even
if mod(nargin, 2) == 0
error( ...
"ArgumentError: " ...
+ "The number of input arguments must be odd.");
end
if nargin == 1
out = varargin{1};
return
end
% NOTE: (`prev` `op` `curr`)
% FIXME: priority of operaters
prev = varargin{1};
for i = 3:2:nargin
op = varargin{i - 1};
curr = varargin{i};
if isequal(op, @and)
if isTruthy(prev) && isTruthy(curr)
prev = curr;
else
prev = false;
end
elseif isequal(op, @or)
if isTruthy(prev)
continue
elseif isTruthy(curr)
prev = curr;
else
prev = false;
end
else
error( ...
"UnsupportedOperatorError: " ...
+ "`%s` is not supported. Use `and` or `or`.", ...
func2str(op));
end
end
out = prev;
end
%%