Cash or credit problem
If you walk into a store and ask "Cash or Credit?" they can just say yes. This does not say anything about you, since you specified the operator OR
.if(cash || credit)
With humans, it is possible that they can answer "Both" to this question or "Only {cash | credit]". Is there a way (or operator) to make the a operator return parts of an TRUE
expression? For instance:
boolean cash = true;
boolean credit = true;
boolean check = false;
if(cash || credit || check)
{
// In here you would have an array with cash and credit in it because both of those are true
}
I would like to point out that this is not the problem I am trying to solve. This is what I was thinking and wondered if it is possible. I can't think of a practical application I would have for this.
a source to share
In C #, you can do something very similar to this with an enum with the Flags attribute set.
[Flags]
enum MethodOfPayment
{
None = 0,
Cash = 1,
Credit = 2,
Check = 4
}
Usage example:
void Run()
{
MethodOfPayment m = MethodOfPayment.Cash | MethodOfPayment.Credit;
if (m != MethodOfPayment.None)
{
// You can now test m to see which values are selected.
// If you really want the values in an array, you can do this:
MethodOfPayment[] selected = getSelectedValues(m).ToArray();
// selected now contains { Cash, Credit }
}
}
// Helper method to get the selected values from the enum.
IEnumerable<MethodOfPayment> getSelectedValues(MethodOfPayment m)
{
foreach (MethodOfPayment x in Enum.GetValues(typeof(MethodOfPayment)))
{
if ((m & x) != MethodOfPayment.None)
yield return x;
}
}
a source to share
In Scala, you can write the following using the match operator
def OneOrTheOther( _1:Boolean, _2:Boolean ) = {
(_1, _2) match{
case True, False => //do stuff
case False, True => //do stuff
case True, True => //do stuff
case False, False =>//do stuff
}
}
I like expressions of conformity.
a source to share
I'm guessing this is a somewhat artificial scenario - it's not what you're trying to answer, so I'm going to make the assumption that there is some value in some other case.
In languages with functional constructs, you can "select" items from a collection that match a condition.
In Ruby:
payment={cash=>true, credit=>true, check=>false}
methods_used=payment.select{ |key,value| value==true}.map { |key,value| key}
gives [: cash ,: credit]
In C # 3:
var payment = new Dictionary<string, bool>
{{"cash", true}, {"credit",true}, {"check", false}};
var items=payment.Where(x => x.Value == true).Select(x => x.Key);
Console.WriteLine(String.Join(",",items.ToArray()));
In most languages I can think of, you cannot do what immediately after binding the variables without adding the variables to the collection or dictionary. There may be some exception, but I cannot think of one of them on my head.
A closer approximation could be pattern matching in languages like Haskell, F #, and OCaml, but I still can't seem to find a way to make it look the way you hint.
In Boo, you can change the compiler pipeline to replace the semantics of the if statement with something that will give you what you want. I would probably use an alternative keyword, but you could basically extract all the subexpressions and add the variable names to the scope of the block, or add yes to one collection and nos to another and add the named variable by convention. To do this usefully, you might have to break the short-circuit evaluation convention, which will annoy most people used for a modern programming language.
a source to share
I think what you are aiming for (and correct me if I'm wrong because I'm not 100% sure) is this:
if (operandA || operandB || operandC || (operandD && operandE))
{
//in here we have access to an environment-created array $TRUE_OPERANDS which stores the operands
//that were TRUE in the most recently evaluated if-statement.
//Maybe it contains the names of operands. suppose that A, C and E were TRUE
//then $TRUE_OPERANDS = {'operandA', 'operandC', 'operandE'}
//...but E was AND'd with D and D was false, so should E still be returned?
//Are E and D considered separate operands or are the considered as a single "E && D" operand?
//...and how would we implement nested if-statements?
//Would there be only one global $TRUE_OPERANDS or would there be one
//for the scope of every if-statement?
}
I don't know of any language that can do this (at least not in the way shown here). Like many others, bitmasks and enums are often used to solve this problem. Some other people have posted examples in Ruby, Python, and C # that might be close to what you want.
a source to share
You can use a list comprehension if your language supports it (example in Python):
list = [True, False, True, True]
print [x for x in list if x] # print only the True values
Alternatively, bitmasks are often used for tasks like this (as they are generally cheap and exist in most languages):
CASH = 1
CREDIT = 2
ELSE = 4
myOption = CASH & ELSE
It all depends on what exactly you are trying to do.
a source to share
From the comments, you seem to be asking: Will any language automatically create this collection for any if-statement that is evaluated?
The answer is no. At least not a single modern language (C, C #, Java, Haskell, almost nothing). The reason is a short circuit. Imagine this if statement:
if (a() || b() || c() || d()) {
}
That is, you call a function to get each true value. If a()
true, then any modern language will not evaluate any other functions, as they will not change the truth value for the entire statement. Currently the code relies on this short-circuiting, so changing the language to evaluate all of it or the operands is likely to break a lot of code.
As for the special case where you have a set of heads and only variables in the test case ... which is really a special case and it is best to use some of the other solutions suggested here if you want to get a set from what is true, not just whether any of them are true.
a source to share
This is often done using flags and binary logic:
http://msdn.microsoft.com/en-us/library/system.flagsattribute.aspx http://msdn.microsoft.com/en-us/library/cc138362.aspx
a source to share