July 06, 2021

On Tuesday, 6 July 2021 at 15:48:35 UTC, rassoc wrote:

>

You can also do:

import std;
void main()
{
    // https://dlang.org/phobos/std_ascii.html#.lowercase
    "Book.".filter!(c => lowercase.canFind(c))
           .each!(c => writeln(c, " found"));

    // Output:
    // o found
	// o found
	// k found
}

I really don't like reading arrow functions.
It takes some more effort to understand, follow and get used to them.
I'd like something simple and effortless looking and feeling that would make code readable similarly to an english sentence.

Such as this, but as noted, this does not work as intended/assumed by me:

if (letter.findAmong(alphabet)){
         write("found");
     }
July 07, 2021

I think nested foreach loops are more readable.

import std;
void main()
{
    alias alphabet = letters;
    char[26] letters = ['a','b', 'c', 'd', 'e',
                        'f', 'g', 'h', 'i', 'j',
                        'k', 'l', 'm', 'n', 'o',
                        'p', 'q', 'r', 's', 't',
                        'u', 'v', 'w', 'x', 'y', 'z'];

    string wordExample = "Book.";
	foreach (letter; wordExample){
        foreach (alphabetLetter; letters){
            if (letter == alphabetLetter) {
            	writeln(letter);
            }
        }
    	
    }
						
}
July 07, 2021

On Wednesday, 7 July 2021 at 12:22:11 UTC, BoQsc wrote:

>

I think nested foreach loops are more readable.

import std;
void main()
{
    alias alphabet = letters;
    char[26] letters = ['a','b', 'c', 'd', 'e',
                        'f', 'g', 'h', 'i', 'j',
                        'k', 'l', 'm', 'n', 'o',
                        'p', 'q', 'r', 's', 't',
                        'u', 'v', 'w', 'x', 'y', 'z'];

    string wordExample = "Book.";
	foreach (letter; wordExample){
        foreach (alphabetLetter; letters){
            if (letter == alphabetLetter) {
            	writeln(letter);
            }
        }
    	
    }
						
}

Use whatever fits your style best or what you are most familar with. But be aware that there's a concise alternative:

import std;
void main()
{
    import std.ascii; // because there's also std.uni.isLower
    "Book.".filter!isLower.each!writeln;	
}

or alternatively with fully-qualified naming:

import std;
void main()
{
   "Book.".filter!(std.ascii.isLower).each!writeln;						
}
1 2
Next ›   Last »