Obsecure problem 1

pascal111 judas.the.messiah.111 at gmail.com
Sun Jul 31 15:01:21 UTC 2022


On Sunday, 31 July 2022 at 07:43:06 UTC, Salih Dincer wrote:
> 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
>   }
> ```
>

I don't know how "assert" works, if you explained it I'll be able 
to get the idea of your suggestion to apply the appropriate 
changes on my code.

> But if it's because of typos, it's ok. Lets continue...
>
I think "typos" are mistakes of typing? I'm not sure.
>
> 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.
>
> I think you are right, I'll add a default argument in 
> "d_strtok".
>
> **[bonus]** And using selective imports where necessary is also 
> a nice feature of D.
>
Beginners are reading my library, so I have some limitations to 
provide 'em understood code, so if I'll apply a new D syntax, 
I'll explain it in a video before providing the beginners the 
code with the new syntax.
>
> SDB at 79




More information about the Digitalmars-d-learn mailing list