Return explicitly "empty" MapResult (fail-fast)
kdevel
kdevel at vogtner.de
Sat Jan 31 15:36:09 UTC 2026
On Friday, 30 January 2026 at 00:34:31 UTC, Ali Çehreli wrote:
> On 1/29/26 4:28 PM, kdevel wrote:
>> On Thursday, 29 January 2026 at 20:44:17 UTC, Ali Çehreli
>> wrote:
>>>> On 1/28/26 12:37 AM, kdevel wrote:
>>>> On Tuesday, 27 January 2026 at 02:05:45 UTC, Ali Çehreli
>>>> wrote:
>> [...]
>>>>> auto example_func(bool fail)
>>>>> {
>>>>> alias filterFunc = a => a != "expensive";
>>>>> alias mapFunc = a => tuple!("value", "numLetters")(a,
>>>>> a.length);
>>>>>
>>>>> auto makeRange(R)(R r =
>>>>> typeof(someExpensiveOperation()).init) {
>>>>> return r
>>>>> .filter!filterFunc
>>>>> .map!mapFunc;
>>>>> }
>>>>>
>>>>> if (fail)
>>>>> {
>>>>> return makeRange();
>>>>> }
>>>>>
>>>>> auto list = someExpensiveOperation();
>>>>>
>>>>> return makeRange(list);
>>>>> }
> >
> > Your function is equivalent to
>
> No because you don't apply filterFunc or mapFunc below.
>
> > auto example_func (bool fail)
> > {
> > alias filterFunc = a => a != "expensive";
> > alias mapFunc = a => tuple!("value", "numLetters")(a,
> a.length);
> >
> > auto list = fail
> > ? typeof(someExpensiveOperation()).init
> > : someExpensiveOperation();
> >
> > return list
> > .filter!filterFunc
> > .map!mapFunc;
> > }
> >
If you read the code again you'll spot filterFunc and mapFunc in
the
return statement. And if you count the occurrences of
"filterFunc",
"someExpensiveOperation", and "mapFunc" in the code fragement it
yields
two for each of them.
But the code is less complex than code with a lambda/nested
function while sharing the same repetitiveness and the same
result.
>> but it has only two levels of indentation and no nested
>> function
>> (template). Alas it shares the duplication of
>> "someExpensiveOperation()".
>
> The problem is, one has to repeat filterFunc and mapFunc as
> well. And that's why a nested function solves the repetition
> issue elegantly for me.
I don't understand the connection between the last two sentences.
Please, can you rephrase?
More information about the Digitalmars-d-learn
mailing list