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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
|
classdef richc
% Rich nested comparison syntax, Python like.
%
% A chain of cmp operators comparions is flattened lazily. Exemples of
% chains: 1 < x < y >= 2 1 < 2 < x
% This is a linear chain. distant arguments are never compared directly,
% only adjacent operand are compared (left to right reduce)
%
% * Typical Matlab syntaxes
% if richc: 1< 2 >1.5 -> cast to bool via self.boolean
% container(richc: 1< 2 >1.5) -> cast to bool via self.subsindex
% richc: 1< 2 >1.5 :richc -> cast to bool via closing colon (:) instance
% richc: 1< 2 >1.5 -> NOT casted to bool
% richc: 1< 2 >1.5 & (true|False) -> cast to bool via external logical operators &|
%
% The colon <:> allows beginning a rich comp with a light syntax, thanks to
% its higher priority
%
% * Warning, NEVER do this
% richc: (1< 2 >1.5) -> parentheses force matlab basic dummy evaluation of
% (1< 2 >1.5)
%
% * Impl.
% - The ternary column seems to have lower priority than the binary :
% workaround: use a closing instance with another colon
% TODO: add boolean operator in a lazy way (current impl. forces evaluation)
% but this requires harder evaluation due to priorities
properties
datas= {};
operators= {};
not_= false;
% CLSNAME= mfilename;
is_left_op= false;
end
methods
function self= colon(self,b,varargin)
% Used for light syntax richc: x<y<z, colon has higher priority
% Used as an autoassign constructor
% the last c in colon could be used for recasting (to bool?) after
% all comparisons are done, but the syntaxe wont allow it wo ()
% NO: even with (), richc:(expr):cast => expr is evaluated before
% :, hence in the builtin stupid way.
if isa(b,mfilename); % closing colon
b.is_left_op= true;
tmp= self; self= b; b= tmp;
end
assert(isempty(self.datas))
self.datas{1}= b;
end
%% ----------- Forcing evaluation
function results= evalme(self)
% Forces evaluation of the boolean value of self
assert(~isempty(self.operators),...
'You probably have used the wrong parentheses richc:(...). Remove them please')
results= true;
if isa(self.datas{end},mfilename)
% Treats a closing colon instance
assert(self.datas{end}.is_left_op);
self.datas{end}= self.datas{end}.datas{1};
end
for k=1:length(self.operators)
op= self.operators{k};
if isequaln(op,@not)
% Implemented with self.not_, this changes the priority of
% NOT to lowest !
else
results= results & op(self.datas{k},self.datas{k+1});
end
if not(any(results)); break; end
% TODO[optim]: restreindre les cmp ultérieurs aux éléments qui sont
% toujours true ?
end
if self.not_; results= not(results);end
end
function results= subsindex(self,A)
% forces the evaluation
% Careful if A overloads subsref
results= find(self.evalme()) -1; % zero based integers !
end
function results= logical(self,A)
% if/while casting
results= self.evalme();
end
function results= double(self,A)
results= self.evalme();
end
%% ----------- lazy cmp operators
function self= check_closing_colon(self)
% Allows RC: .. :RC to evaluate automatically
if isa(self.datas{end},mfilename)
self= self.evalme();
end
end
function self= le(self,b)
self.datas{end+1}= b;
self.operators{end+1} = @le;
self= self.check_closing_colon();
end
function self= ge(self,b)
self.datas{end+1}= b;
self.operators{end+1} = @ge;
self= self.check_closing_colon();
end
function self= lt(self,b)
self.datas{end+1}= b;
self.operators{end+1} = @lt;
self= self.check_closing_colon();
end
function self= gt(self,b)
self.datas{end+1}= b;
self.operators{end+1} = @gt;
self= self.check_closing_colon();
end
function self= eq(self,b)
self.datas{end+1}= b;
self.operators{end+1} = @eq;
self= self.check_closing_colon();
end
function self= ne(self,b)
self.datas{end+1}= b;
self.operators{end+1} = @ne;
self= self.check_closing_colon();
end
function self= not(self)
% remain lazy, priority changed to lowest
self.not_= ~(self.not_);
end
%% ----------- external boolean operators
% Forces evaluation
% TODO: lazy operators, but this would complicate evaluation with priorities
function z= any(self,varargin)
z= any(self.evalme(),varargin{:});
end
function z= all(self,varargin)
z= all(self.evalme(),varargin{:});
end
% DONE: weird behaviour on (richc: 1<2) & (richc: 2<3)
% self was changed to boolean after self.evalme(), in THIS stack !!
% Maybe handle class wouldn't
% FIXED: b.evalme() fixes it
function z= and(self,b)
if isa(b,mfilename); b= b.evalme(); end
z= and(self.evalme(),b);
end
function z= or(self,b)
if isa(b,mfilename); b= b.evalme(); end
z= or(self.evalme(),b);
end
% function horzcat()
%
% end
% function vertcat()
%
% end
end
%%
end
%%
function DEMO
% demo datas
v= 1:10;
vv= 10*rand(1,10);
vv= sort(vv);
w= rand(1,10)<0.5;
%-- Nested comparisons syntax. the <:> allows a light syntax
richc: 3<= v < 7 % remains an instance, not evaluated yet
% A constructor could use heavier syntaxe, not done yet
% richc(3) <= v< 7
%-- logical casting (=> dim 1)
if richc : 1<2<3; disp('ok');end
if richc : 3>2<4; disp('ok');end
if richc : 1<2>3; disp('ok');end
%-- Indexing (via subsindex)
% this is dangerous if the indexed class overloads subsref
v(richc: 3<= v< 7)
v(richc: 3< v<= vv <= 8)
v(richc: 3< v > vv )
v(richc: 3<= v< 8 < 5+vv)
%-- External boolean operator
richc: 3<= v< 7 & true % casts to logical
% no simpler way to do it since unary op have higher priority ....
% SOLUTION; closing colon
richc: 3<= v< 7 | v>=9
any(richc: 3< v< 7)
all(richc: 3< v< 7 | v<=10)
v(richc: w== 1)
v(richc: w~= 1)
% + indexing
v(richc: 3<= v< 7 & v<6)
v(~richc: 3<= v< 7 & v<6) % unary not is lazy, with lowest priority!
v(richc: 3<= v< 7 | v>=9)
% other indexing (cell, matrices)
m= reshape(1:100,[10 10]);
m(richc: 2<= v< 7 ,:)
m(:, richc: 2<= v< 7)
m(richc: 2<= v< 7)
c= num2cell(m);
c(richc: 2<= v< 7,:)
c(richc: 2<= v< 7)
c{richc: 2<= v< 7}
find(richc: 2<= v< 7) %fails
find(richc: 2<= v< 7:richc)
% (richc: 2<= v< 7).find()
% rguments to the closing instance could denote subsindexing !
% la simple présence de cette ligne fait bugger la classe
% !!!!!!!!!!!!!!!!!!!!!!!!!!
% richc: 2<= v< 7:richc(1:3,:) % malheuresement, : et end ne sont pas des arguments valide !
%
end
function SOLUTION_TO_SYNTAXE_ISSUES
% DONE: use a left richc colon operand to close the evaluation and cast to
% bool
richc: 3<v<7: richc
richc: 3<= v < 7 > 1 : richc
richc: 1+2<= 1*v-0 < 7^1 > 1 : richc
end
function OTHER_INTERACTIONS
0+ richc:3<v<7: richc % bad priority, require overload +
0+ (richc:3<v<7: richc) % work around
[richc:3<v<7: richc, richc:3<v<7: richc] % works, cat priority is lower
cat(1,richc:3<v<7:richc, richc:3<v<7:richc) % works, cat priority is lower
cat(1,richc:3<v<7, richc:3<v<7) % struct array concatenation
[richc:3<v<7, richc:3<v<7] % struct array concatenation
evalme([richc:3<v<7, richc:3<v<7]) % fails
end
function TODO
richc:3<v<7:richc('find')
richc:3<v<7:richc('subsindex') % mais pas de : et end :(
end
function BUGGED_SYNTAXES
% Never do this
richc: (3<= v < 7) % supid classic evaluation of (3<= v < 7)
% TODO: we could force some operator to occur before final evaluation to avoid
% this user error
% * tres bizarre
% <and> => self.evalme(), qui a changé self en sortant !!!
% Maybe a handle class could fix it ?
% FIXED by converting other operand to bool !
(richc: 1<2) & (richc: 2<3)
richc: 1<2 & richc: 2<3 % priorities ok !
(richc: 3<= v< 7) & (richc: 3<= v< 7)
%% BUGGED ternary colon syntaxes
% what can we do of the 3rd colon arg ?
% tries to cast once all comparisons are done, but...
richc:3<v<7:@logical % fails, priority issue (ternary : is lower than binary : ???)
richc: (3< v< 7) : @logical % wrong, silent error, (3< v< 7) is evaluated before
(richc: 3< v< 7) : @logical % this works, but error prone syntax with tedious ()!
richc: @logical: 3<v< 7 % syntaxe ok, mais inutile. on ne le sait pas assez tard pour convertir
end |
Partager