Re: reduce condition nesting

2017-11-25 Thread Adam D. Ruppe via Digitalmars-d-learn

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.


Re: reduce condition nesting

2017-11-25 Thread Ali Çehreli via Digitalmars-d-learn

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


Re: reduce condition nesting

2017-11-25 Thread Adam D. Ruppe via Digitalmars-d-learn

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())
);
}
---


Re: reduce condition nesting

2017-11-25 Thread Ali Çehreli via Digitalmars-d-learn

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


Re: reduce condition nesting

2017-11-23 Thread Michael via Digitalmars-d-learn
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!


Re: reduce condition nesting

2017-11-23 Thread Temtaime via Digitalmars-d-learn
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"); }   
);

:)


Re: reduce condition nesting

2017-11-23 Thread drug via Digitalmars-d-learn

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


Re: reduce condition nesting

2017-11-23 Thread Andrea Fontana via Digitalmars-d-learn
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



Re: reduce condition nesting

2017-11-23 Thread Adam D. Ruppe via Digitalmars-d-learn

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.


Re: reduce condition nesting

2017-11-23 Thread Andrey via Digitalmars-d-learn
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 )


Re: reduce condition nesting

2017-11-23 Thread Andrea Fontana via Digitalmars-d-learn

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();

?



Re: reduce condition nesting

2017-11-22 Thread Nicholas Wilson via Digitalmars-d-learn

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);


reduce condition nesting

2017-11-22 Thread Andrey via Digitalmars-d-learn

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()
}