Thread overview | |||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
January 22, 2010 boolean over multiple variables | ||||
---|---|---|---|---|
| ||||
This may be is a very basic question, but is there a way to let me omit a repeating variable when doing multiple boolean operations? if ( var == a || var == b || var == c || var == d) if ( var == (a || b || c || d) ) |
January 22, 2010 Re: boolean over multiple variables | ||||
---|---|---|---|---|
| ||||
Posted in reply to strtr | Hello Strtr,
> This may be is a very basic question, but is there a way to let me
> omit a repeating variable when doing multiple boolean operations?
>
> if ( var == a || var == b || var == c || var == d) if ( var == (a || b
> || c || d) )
>
bool B;
switch(var) { case a,b,c,d: B = true; break; default B = false; break; }
if(B)
or you can put the then/else parts right in the switch
|
January 22, 2010 Re: boolean over multiple variables | ||||
---|---|---|---|---|
| ||||
Posted in reply to BCS | BCS Wrote:
> Hello Strtr,
>
> > This may be is a very basic question, but is there a way to let me omit a repeating variable when doing multiple boolean operations?
> >
> > if ( var == a || var == b || var == c || var == d) if ( var == (a || b
> > || c || d) )
> >
>
> bool B;
> switch(var) { case a,b,c,d: B = true; break; default B = false; break; }
> if(B)
>
> or you can put the then/else parts right in the switch
>
Not really what I had in mind, but works yes.
|
January 22, 2010 Re: boolean over multiple variables | ||||
---|---|---|---|---|
| ||||
Posted in reply to strtr | On Fri, 22 Jan 2010 22:55:45 +0100, strtr <strtr@spam.com> wrote: > This may be is a very basic question, but is there a way to let me omit a repeating variable when doing multiple boolean operations? > > if ( var == a || var == b || var == c || var == d) > if ( var == (a || b || c || d) ) bool anySame( T, U... )( T arg1, U args ) { foreach ( arg; args ) { if ( arg1 == arg ) { return true; } } return false; } bool allSame( T, U... )( T arg1, U args ) { foreach ( arg; args ) { if ( arg1 != arg ) { return false; } } return true; } Not tested, but they should work: if ( anySame( var, a, b, c, d ) ) { } if ( allSame( var, a, b, c, d ) ) { } -- Simen |
January 22, 2010 Re: boolean over multiple variables | ||||
---|---|---|---|---|
| ||||
Posted in reply to Simen kjaeraas | Simen kjaeraas Wrote: > > Not tested, but they should work: > > if ( anySame( var, a, b, c, d ) ) { > } > > if ( allSame( var, a, b, c, d ) ) { > } > A lot prettier. I thought there would be a generic (basic) solution to this which I just didn't know about but maybe I actually do know the basics by now :) > -- > Simen |
January 25, 2010 Re: boolean over multiple variables | ||||
---|---|---|---|---|
| ||||
Posted in reply to strtr | On 01/23/2010 12:29 AM, strtr wrote:
> Simen kjaeraas Wrote:
>
>>
>> Not tested, but they should work:
>>
>> if ( anySame( var, a, b, c, d ) ) {
>> }
>>
>> if ( allSame( var, a, b, c, d ) ) {
>> }
>>
>
> A lot prettier.
> I thought there would be a generic (basic) solution to this which I just didn't know about but maybe I actually do know the basics by now :)
>> --
>> Simen
>
If we get opIn_r for arrays, you can do
if (var in [a, b, c, d]) {
}
Which I find a lot prettier.
|
January 25, 2010 Re: boolean over multiple variables | ||||
---|---|---|---|---|
| ||||
Posted in reply to Pelle Månsson | On Mon, 25 Jan 2010 09:59:42 +0100, Pelle Månsson <pelle.mansson@gmail.com> wrote: > On 01/23/2010 12:29 AM, strtr wrote: >> Simen kjaeraas Wrote: >> >>> >>> Not tested, but they should work: >>> >>> if ( anySame( var, a, b, c, d ) ) { >>> } >>> >>> if ( allSame( var, a, b, c, d ) ) { >>> } >>> >> >> A lot prettier. >> I thought there would be a generic (basic) solution to this which I just didn't know about but maybe I actually do know the basics by now :) >>> -- >>> Simen >> > If we get opIn_r for arrays, you can do > > if (var in [a, b, c, d]) { > } > > Which I find a lot prettier. Is this good enough? struct CheckIf( T ) { T payload; bool opIn( T[] rhs ) { foreach ( e; rhs ) { if ( e == payload ) { return true; } } return false; } } CheckIf!( T ) checkIf( T )( T rhs ) { return CheckIf!( T )( rhs ); } if ( checkIf( true ) in [ false, true, false ] ) { } -- Simen |
January 25, 2010 Re: boolean over multiple variables | ||||
---|---|---|---|---|
| ||||
Posted in reply to Simen kjaeraas | Simen kjaeraas Wrote:
> On Mon, 25 Jan 2010 09:59:42 +0100, Pelle MÃ¥nsson <pelle.mansson@gmail.com> wrote:
>
> > On 01/23/2010 12:29 AM, strtr wrote:
> >> Simen kjaeraas Wrote:
> >>
> >>>
> >>> Not tested, but they should work:
> >>>
> >>> if ( anySame( var, a, b, c, d ) ) {
> >>> }
> >>>
> >>> if ( allSame( var, a, b, c, d ) ) {
> >>> }
> >>>
> >>
> >> A lot prettier.
> >> I thought there would be a generic (basic) solution to this which I
> >> just didn't know about but maybe I actually do know the basics by now :)
> >>> --
> >>> Simen
> >>
> > If we get opIn_r for arrays, you can do
> >
> > if (var in [a, b, c, d]) {
> > }
> >
> > Which I find a lot prettier.
>
> Is this good enough?
>
> struct CheckIf( T ) {
> T payload;
> bool opIn( T[] rhs ) {
> foreach ( e; rhs ) {
> if ( e == payload ) {
> return true;
> }
> }
> return false;
> }
> }
>
> CheckIf!( T ) checkIf( T )( T rhs ) {
> return CheckIf!( T )( rhs );
> }
>
> if ( checkIf( true ) in [ false, true, false ] ) {
> }
>
> --
> Simen
Sometimes code just makes me smile :)
|
January 25, 2010 Re: boolean over multiple variables | ||||
---|---|---|---|---|
| ||||
Posted in reply to Simen kjaeraas | On 01/25/2010 10:28 AM, Simen kjaeraas wrote:
> On Mon, 25 Jan 2010 09:59:42 +0100, Pelle Månsson
> <pelle.mansson@gmail.com> wrote:
>
>> On 01/23/2010 12:29 AM, strtr wrote:
>>> Simen kjaeraas Wrote:
>>>
>>>>
>>>> Not tested, but they should work:
>>>>
>>>> if ( anySame( var, a, b, c, d ) ) {
>>>> }
>>>>
>>>> if ( allSame( var, a, b, c, d ) ) {
>>>> }
>>>>
>>>
>>> A lot prettier.
>>> I thought there would be a generic (basic) solution to this which I
>>> just didn't know about but maybe I actually do know the basics by now :)
>>>> --
>>>> Simen
>>>
>> If we get opIn_r for arrays, you can do
>>
>> if (var in [a, b, c, d]) {
>> }
>>
>> Which I find a lot prettier.
>
> Is this good enough?
>
> struct CheckIf( T ) {
> T payload;
> bool opIn( T[] rhs ) {
> foreach ( e; rhs ) {
> if ( e == payload ) {
> return true;
> }
> }
> return false;
> }
> }
>
> CheckIf!( T ) checkIf( T )( T rhs ) {
> return CheckIf!( T )( rhs );
> }
>
> if ( checkIf( true ) in [ false, true, false ] ) {
> }
>
Interesting solution! Very clever!
I still think opIn_r should be defined for arrays, though. :)
|
January 25, 2010 Re: boolean over multiple variables | ||||
---|---|---|---|---|
| ||||
Posted in reply to Pelle Månsson | Pelle Månsson <pelle.mansson@gmail.com> wrote: > Interesting solution! Very clever! Thank you. > I still think opIn_r should be defined for arrays, though. :) Yeah, but currently, 'a in b' means '(∃b[a])', that is, 'is a a valid index in b'. This means having 'a in b' mean '(∃i)( b[i] = a )', that is, 'is there such an i that b[i] == a' would introduce inconsistencies. While I do agree it would, I do see reasons for including such a thing to the language, especially seeing how often just that question arises, and how rarely the first meaning is actually useful. -- Simen |
Copyright © 1999-2021 by the D Language Foundation