On Saturday, 26 March 2016 at 22:11:53 UTC, Nordlöw wrote:
On Saturday, 26 October 2013 at 15:41:32 UTC, Andrei
Alexandrescu wrote:
While messing with std.allocator I explored the type below. I
ended up not using it, but was surprised that implementing it
was quite nontrivial. Should we add it to stdlib?
I can think of many variants of for this. What about
{ yes, // 1 chance
no, // 0 chance
likely, // > 1/2 chance
unlikely, // < 1/2 chance
unknown // any chance
}
?
Partial implementation at
https://github.com/nordlow/justd/blob/master/fuzzy.d#L15
:)
If we're going down that route, might as well use state tables.
With CTFE + templates, you could possibly do something like this:
immutable State[] StateOrTable = ParseStateTable!q{
| yes | no | likely | unlikely | unknown
------------------------------------------------------
yes | yes | yes | yes | yes | yes
no | yes | no | likely | unlikely | unknown
likely | yes | likely | likely | likely | likely
unlikely| yes | unlikely | likely | unlikely | unknown
unknown | yes | unknown | likely | unknwon | unknown
};
State opBinary(string op)(State other)
if(op == "||") {
return StateOrTable[this.value*NumStates+other.value];
}
Though I see issues with having a generic n-state value template
and also rewriting `a != b` to `!(a == b)`; I suspect that there
may be some class of values where the two are not equivalent.