www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - reduce condition nesting

reply Andrey <andrey kabylin.ru> writes:
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()
 }
Nov 22 2017
next sibling parent Nicholas Wilson <iamthewilsonator hotmail.com> writes:
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);
Nov 22 2017
prev sibling next sibling parent reply Andrea Fontana <nospam example.com> writes:
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(); ?
Nov 23 2017
parent Andrey <andrey kabylin.ru> writes:
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 )
Nov 23 2017
prev sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
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.
Nov 23 2017
parent reply Andrea Fontana <nospam example.com> writes:
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
Nov 23 2017
next sibling parent drug <drug2004 bk.ru> writes:
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
Nov 23 2017
prev sibling next sibling parent Temtaime <temtaime gmail.com> writes:
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
when ( c1, { writeln("first"); }, c2, { writeln("second"); }, { writeln("default"); } ); :)
Nov 23 2017
prev sibling next sibling parent Michael <michael toohuman.io> writes:
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!
Nov 23 2017
prev sibling parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
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
Nov 25 2017
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Saturday, 25 November 2017 at 21:42:29 UTC, Ali Çehreli wrote:
 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?
Bones: "Perhaps the professor can use your computer." https://dlang.org/spec/function.html#lazy_variadic_functions Dr. Nichols: "Lazy variadic functions?!" Scotty: "That's the ticket, laddie." --- import std.stdio; void when(bool delegate()[] foo...) { foreach(i; foo) { if(i()) return; } } bool then(bool c, lazy void what) { if(c) what(); return c; } bool otherwise(lazy void what) { what; return true; } void foo() { writeln("foo evaled"); } void bar() { writeln("bar evaled"); } void zar() { writeln("zar evaled"); } void main() { bool c1 = false; bool c2 = false; when( c1.then(foo()), c2.then(bar()), otherwise(zar()) ); } ---
Nov 25 2017
parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 11/25/2017 02:05 PM, Adam D. Ruppe wrote:
 On Saturday, 25 November 2017 at 21:42:29 UTC, Ali Çehreli wrote:
 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?
Bones: "Perhaps the professor can use your computer." https://dlang.org/spec/function.html#lazy_variadic_functions Dr. Nichols: "Lazy variadic functions?!" Scotty: "That's the ticket, laddie." --- import std.stdio; void when(bool delegate()[] foo...) {     foreach(i; foo) {         if(i())             return;     } }
Cool! So, D is great even without templates. ;) Despite 'lazy', apparently my failed attempt had eager arguments: void when(lazy bool[] args...) { // ... } Ali
Nov 25 2017
parent Adam D. Ruppe <destructionator gmail.com> writes:
On Saturday, 25 November 2017 at 22:45:03 UTC, Ali Çehreli wrote:
 Despite 'lazy', apparently my failed attempt had eager 
 arguments:

 void when(lazy bool[] args...) {
Yeah, I'm tempted to say that is a bug... I doubt anyone has combined lazy with T[]... like this before - especially since the language has that other syntax to cover this case.
Nov 25 2017