November 25, 2017 Re: reduce condition nesting | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | 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()) ); } --- |
November 25, 2017 Re: reduce condition nesting | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | 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
|
November 26, 2017 Re: reduce condition nesting | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | 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.
|
Copyright © 1999-2021 by the D Language Foundation