On Sunday, 31 July 2022 at 00:58:47 UTC, pascal111 wrote:
Another version of the program:

https://github.com/pascal111-fra/D/blob/main/proj04.d

I have a few more suggestions for you; Among them the first is on the following sample:

```d
  auto sentence_x = "she has six oxen";
  auto noNeedTrim = sentence_x.d_strtok(" ");

  foreach(token; noNeedTrim)
  {
    //token = token.strrtrim;
    if(token.canFind("s"))
    {
      assert(token.length == 3);
    } else token.writeln; // oxen
  }
```

But if it's because of typos, it's ok. Lets continue...

Why are you using const for strings? After all, they are protected by immutable. Moreover, since you do not use refs, copies are taken in every function and also you have created extra copy for results.

```d
enum str = "five hundred twelve";
auto test = str.strltirim;
assert(test == "five hundred twelve");

string strltrim(const string ch)
{
    string ch_cpy=ch;

    while(ch_cpy.strleft(1)==" ")
        ch_cpy=ch_cpy.strdel(0,1);

    return ch_cpy;
}

```

There are also nicer ways to delete spaces in a text. Have you ever tried using the default arguments and auto?

```d

//string[] d_strtok(const string ch, const string delim)
auto d_strtok(string ch, string delim = " ")//*/
{
    import std.functional : not; // [bonus]
    string[] tokens = ch.
    splitter!(c => delim.canFind(c)).
    filter!(not!empty).array;

    return tokens;
}
```

Functions may optionally define default arguments. This avoids the tedious work of declaring redundant overloads.

**[bonus]** And using selective imports where necessary is also a nice feature of D.

SDB@79

Reply via email to