Thread overview | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
November 23, 2017 reduce condition nesting | ||||
---|---|---|---|---|
| ||||
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 Re: reduce condition nesting | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrey | 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 Re: reduce condition nesting | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrey | 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 Re: reduce condition nesting | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrea Fontana | 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 Re: reduce condition nesting | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrey | 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 Re: reduce condition nesting | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | 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 Re: reduce condition nesting | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrea Fontana | 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 Re: reduce condition nesting | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrea Fontana | 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 Re: reduce condition nesting | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrea Fontana | 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 Re: reduce condition nesting | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrea Fontana | 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
|
Copyright © 1999-2021 by the D Language Foundation