Jump to page: 1 2
Thread overview
reduce condition nesting
Nov 23, 2017
Andrey
Nov 23, 2017
Nicholas Wilson
Nov 23, 2017
Andrea Fontana
Nov 23, 2017
Andrey
Nov 23, 2017
Adam D. Ruppe
Nov 23, 2017
Andrea Fontana
Nov 23, 2017
drug
Nov 23, 2017
Temtaime
Nov 23, 2017
Michael
Nov 25, 2017
Ali Çehreli
Nov 25, 2017
Adam D. Ruppe
Nov 25, 2017
Ali Çehreli
Nov 26, 2017
Adam D. Ruppe
November 23, 2017
Hello, is there way to reduce this condition:
> if (c1) {
>     foo();
> } else {
>     if (c2) {
>         bar();
>     } else {
>         if (c3) {
>         ...
>         }
>     }
> }

for instance in kotlin it can be replace with this:
> when {
>     c1 -> foo(),
>     c2 -> bar(),
>     c3 -> ...
>     else -> someDefault()
> }

November 23, 2017
On Thursday, 23 November 2017 at 05:19:27 UTC, Andrey wrote:
> Hello, is there way to reduce this condition:
>> if (c1) {
>>     foo();
>> } else {
>>     if (c2) {
>>         bar();
>>     } else {
>>         if (c3) {
>>         ...
>>         }
>>     }
>> }
>
> for instance in kotlin it can be replace with this:
>> when {
>>     c1 -> foo(),
>>     c2 -> bar(),
>>     c3 -> ...
>>     else -> someDefault()
>> }

do
{
    if (c1) { foo(); break;}
    if (c2) { bar(); break;}
    if (c3) { baz(); break;}
    someDefault();
} while (false);
November 23, 2017
On Thursday, 23 November 2017 at 05:19:27 UTC, Andrey wrote:
> Hello, is there way to reduce this condition:
>> if (c1) {
>>     foo();
>> } else {
>>     if (c2) {
>>         bar();
>>     } else {
>>         if (c3) {
>>         ...
>>         }
>>     }
>> }
>
> for instance in kotlin it can be replace with this:
>> when {
>>     c1 -> foo(),
>>     c2 -> bar(),
>>     c3 -> ...
>>     else -> someDefault()
>> }

if (c1) foo()
else if (c2) bar();
else if (c3) ...
else someDefault();

?

November 23, 2017
On Thursday, 23 November 2017 at 08:27:54 UTC, Andrea Fontana wrote:
> On Thursday, 23 November 2017 at 05:19:27 UTC, Andrey wrote:
>> Hello, is there way to reduce this condition:
>>> if (c1) {
>>>     foo();
>>> } else {
>>>     if (c2) {
>>>         bar();
>>>     } else {
>>>         if (c3) {
>>>         ...
>>>         }
>>>     }
>>> }
>>
>> for instance in kotlin it can be replace with this:
>>> when {
>>>     c1 -> foo(),
>>>     c2 -> bar(),
>>>     c3 -> ...
>>>     else -> someDefault()
>>> }
>
> if (c1) foo()
> else if (c2) bar();
> else if (c3) ...
> else someDefault();
>
> ?

haha, yes you are right, sorry for stupid question, I recently began to study Kotlin and noticed than `when` is a great feature )
November 23, 2017
On Thursday, 23 November 2017 at 05:19:27 UTC, Andrey wrote:
> for instance in kotlin it can be replace with this:
>> when {
>>     c1 -> foo(),
>>     c2 -> bar(),
>>     c3 -> ...
>>     else -> someDefault()
>> }

The `switch` statement covers some of these cases too.
November 23, 2017
On Thursday, 23 November 2017 at 13:47:37 UTC, Adam D. Ruppe wrote:
> On Thursday, 23 November 2017 at 05:19:27 UTC, Andrey wrote:
>> for instance in kotlin it can be replace with this:
>>> when {
>>>     c1 -> foo(),
>>>     c2 -> bar(),
>>>     c3 -> ...
>>>     else -> someDefault()
>>> }
>
> The `switch` statement covers some of these cases too.

Anyway you can create something like this:
https://run.dlang.io/is/7pbVXT

November 23, 2017
23.11.2017 17:16, Andrea Fontana пишет:
> On Thursday, 23 November 2017 at 13:47:37 UTC, Adam D. Ruppe wrote:
>> On Thursday, 23 November 2017 at 05:19:27 UTC, Andrey wrote:
>>> for instance in kotlin it can be replace with this:
>>>> when {
>>>>     c1 -> foo(),
>>>>     c2 -> bar(),
>>>>     c3 -> ...
>>>>     else -> someDefault()
>>>> }
>>
>> The `switch` statement covers some of these cases too.
> 
> Anyway you can create something like this:
> https://run.dlang.io/is/7pbVXT
> 
I really like Dlang very much
November 23, 2017
On Thursday, 23 November 2017 at 14:16:25 UTC, Andrea Fontana wrote:
> On Thursday, 23 November 2017 at 13:47:37 UTC, Adam D. Ruppe wrote:
>> On Thursday, 23 November 2017 at 05:19:27 UTC, Andrey wrote:
>>> for instance in kotlin it can be replace with this:
>>>> when {
>>>>     c1 -> foo(),
>>>>     c2 -> bar(),
>>>>     c3 -> ...
>>>>     else -> someDefault()
>>>> }
>>
>> The `switch` statement covers some of these cases too.
>
> Anyway you can create something like this:
> https://run.dlang.io/is/7pbVXT

Syntax #4

 // Syntax #4
    when
	(
        c1, { writeln("first"); 	},
        c2, { writeln("second"); },
        { writeln("default"); }	
    );

:)
November 23, 2017
On Thursday, 23 November 2017 at 14:16:25 UTC, Andrea Fontana wrote:
> On Thursday, 23 November 2017 at 13:47:37 UTC, Adam D. Ruppe wrote:
>> On Thursday, 23 November 2017 at 05:19:27 UTC, Andrey wrote:
>>> for instance in kotlin it can be replace with this:
>>>> when {
>>>>     c1 -> foo(),
>>>>     c2 -> bar(),
>>>>     c3 -> ...
>>>>     else -> someDefault()
>>>> }
>>
>> The `switch` statement covers some of these cases too.
>
> Anyway you can create something like this:
> https://run.dlang.io/is/7pbVXT

That's pretty cool!
November 25, 2017
On 11/23/2017 06:16 AM, Andrea Fontana wrote:
> On Thursday, 23 November 2017 at 13:47:37 UTC, Adam D. Ruppe wrote:
>> On Thursday, 23 November 2017 at 05:19:27 UTC, Andrey wrote:
>>> for instance in kotlin it can be replace with this:
>>>> when {
>>>>     c1 -> foo(),
>>>>     c2 -> bar(),
>>>>     c3 -> ...
>>>>     else -> someDefault()
>>>> }
>>
>> The `switch` statement covers some of these cases too.
> 
> Anyway you can create something like this:
> https://run.dlang.io/is/7pbVXT
> 

I tried to implement the following but gave up because I could not ensure short circuit behaviour.

    when(
        c1.then(foo()),
        c2.then(bar()),
        otherwise(zar())
    );

Possible?

Ali
« First   ‹ Prev
1 2