On Monday, 25 April 2022 at 16:11:47 UTC, rassoc wrote:

```d
import std;

void main() {
    struct Foo { string s; }
    Foo[] arr = ["abc", "def", "ghi"].map!Foo.array;
    arr.writeln; // => [Foo("abc"), Foo("def"), Foo("ghi")]
}
```

Thank you...

Very very nice and simple but not extensible!

Because it cannot be used with other possibilities such as ```chunks()``` and ```take()```. Also it cannot be customized with ```toString()```. I guess even when ```this()``` constructor is added, ```map()``` explodes! So it not to explode, it is necessary to move away from simplicity:

```d
import std.algorithm;
import std.range, std.stdio;

void main() {
  struct Foo {
    string s; /*
    string s;
    string toString() {
      return s;
    }//*/
  }
  auto arr1 = ["abc", "def", "ghi"]
              .map!Foo.array; /*
              .map!(a => Foo(a))
              .array;//*/

  typeof(arr1).stringof.writeln(": ", arr1);

  struct Bar {
    string s;
    //*
    this(R)(R result) {
      import std.conv : to;
      this.s = result.to!string;
    }//*/

    string toString() {
      return s;
    }
  }
  auto arr2 = "abcdefghi"
             .chunks(3)
             .map!(a => Bar(a))
             .array;

  typeof(arr2).stringof.writeln(": ", arr2);

} /* OUTPUT:
Foo[]: [Foo("abc"), Foo("def"), Foo("ghi")]
Bar[]: [abc, def, ghi]
*/
```
SDB@79

Reply via email to