www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - partial with struct

reply Salih Dincer <salihdb hotmail.com> writes:
Hi,

I just do it directly with the S struct, why can't I filter with 
partial? (#line 1)

```d
import std.stdio, std.functional : partial;

void main()
{
   alias foo = partial!(S.filter, nums);
   foo('t').writeln; // okay
   #line 1
   //foo!(true)('t').writeln; //compile error
   S.filter!true(nums, 'T').writeln; // alternative
}

struct S
{
   import std.algorithm : F = filter;
   static auto filter(bool isEnd = false)(string[] str, char chr)
   {
     static if (isEnd)
     {
       alias pred = s => s[$-1] == chr;
       
     } else {

       alias pred = s => s[0] == chr;
     }
     return str.F!pred;
   }
}
```

SDB 79
Dec 10 2024
parent Salih Dincer <salihdb hotmail.com> writes:
On Tuesday, 10 December 2024 at 19:43:58 UTC, Salih Dincer wrote:
 ```d
   auto nums = [
          "zero", "one", "two", "six", "ten",
          "Twenty", "Thirty"
   ];
   alias foo = partial!(S.filter, nums);
   foo('t').writeln; // okay
   #line 1
   //foo!(true)('t').writeln; //compile error
 ```
I figured it out, but it's not sense! Because as the number of overload functions increases, side effects will occur. Moreover, it is very ugly: ```d alias foo(bool B = false) = partial!(S.filter!B, nums); foo!true('t').writeln; // ["Twenty", "Thirty"] ``` SDB 79
Dec 10 2024