digitalmars.D.learn - reduce condition nesting
- Andrey (2/19) Nov 22 2017
- Nicholas Wilson (8/27) Nov 22 2017 do
- Andrea Fontana (6/25) Nov 23 2017 if (c1) foo()
- Andrey (4/30) Nov 23 2017 haha, yes you are right, sorry for stupid question, I recently
- Adam D. Ruppe (2/9) Nov 23 2017 The `switch` statement covers some of these cases too.
- Andrea Fontana (4/13) Nov 23 2017 Anyway you can create something like this:
- drug (2/17) Nov 23 2017 I really like Dlang very much
- Temtaime (11/25) Nov 23 2017 Syntax #4
- Michael (3/17) Nov 23 2017 That's pretty cool!
- =?UTF-8?Q?Ali_=c3=87ehreli?= (10/25) Nov 25 2017 I tried to implement the following but gave up because I could not
- Adam D. Ruppe (40/48) Nov 25 2017 Bones: "Perhaps the professor can use your computer."
- =?UTF-8?Q?Ali_=c3=87ehreli?= (7/35) Nov 25 2017 Cool! So, D is great even without templates. ;)
- Adam D. Ruppe (4/7) Nov 25 2017 Yeah, I'm tempted to say that is a bug... I doubt anyone has
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
On Thursday, 23 November 2017 at 05:19:27 UTC, Andrey wrote:Hello, is there way to reduce this condition:do { if (c1) { foo(); break;} if (c2) { bar(); break;} if (c3) { baz(); break;} someDefault(); } while (false);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
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) ... else someDefault(); ?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 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:haha, yes you are right, sorry for stupid question, I recently began to study Kotlin and noticed than `when` is a great feature )Hello, is there way to reduce this condition:if (c1) foo() else if (c2) bar(); else if (c3) ... else someDefault(); ?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 23 2017
On Thursday, 23 November 2017 at 05:19:27 UTC, Andrey wrote:for instance in kotlin it can be replace with this:The `switch` statement covers some of these cases too.when { c1 -> foo(), c2 -> bar(), c3 -> ... else -> someDefault() }
Nov 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:Anyway you can create something like this: https://run.dlang.io/is/7pbVXTfor instance in kotlin it can be replace with this:The `switch` statement covers some of these cases too.when { c1 -> foo(), c2 -> bar(), c3 -> ... else -> someDefault() }
Nov 23 2017
23.11.2017 17:16, Andrea Fontana пишет:On Thursday, 23 November 2017 at 13:47:37 UTC, Adam D. Ruppe wrote:I really like Dlang very muchOn Thursday, 23 November 2017 at 05:19:27 UTC, Andrey wrote:Anyway you can create something like this: https://run.dlang.io/is/7pbVXTfor instance in kotlin it can be replace with this:The `switch` statement covers some of these cases too.when { c1 -> foo(), c2 -> bar(), c3 -> ... else -> someDefault() }
Nov 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:when ( c1, { writeln("first"); }, c2, { writeln("second"); }, { writeln("default"); } ); :)On Thursday, 23 November 2017 at 05:19:27 UTC, Andrey wrote:Anyway you can create something like this: https://run.dlang.io/is/7pbVXTfor instance in kotlin it can be replace with this:The `switch` statement covers some of these cases too.when { c1 -> foo(), c2 -> bar(), c3 -> ... else -> someDefault() }
Nov 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:That's pretty cool!On Thursday, 23 November 2017 at 05:19:27 UTC, Andrey wrote:Anyway you can create something like this: https://run.dlang.io/is/7pbVXTfor instance in kotlin it can be replace with this:The `switch` statement covers some of these cases too.when { c1 -> foo(), c2 -> bar(), c3 -> ... else -> someDefault() }
Nov 23 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: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? AliOn Thursday, 23 November 2017 at 05:19:27 UTC, Andrey wrote:Anyway you can create something like this: https://run.dlang.io/is/7pbVXTfor instance in kotlin it can be replace with this:The `switch` statement covers some of these cases too.when { c1 -> foo(), c2 -> bar(), c3 -> ... else -> someDefault() }
Nov 25 2017
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
On 11/25/2017 02:05 PM, Adam D. Ruppe wrote:On Saturday, 25 November 2017 at 21:42:29 UTC, Ali Çehreli wrote:Cool! So, D is great even without templates. ;) Despite 'lazy', apparently my failed attempt had eager arguments: void when(lazy bool[] args...) { // ... } AliI 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; } }
Nov 25 2017
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