all with find

Pangram
Pangram in D
module pangram;

import std.algorithm.searching : all, find;
import std.range : empty;
import std.uni : toLower;

private immutable abc = "abcdefghijklmnopqrstuvwxyz";

@safe pure bool isPangram(immutable string text)
{
    auto textLowered = text.toLower;
    return abc.all!((ltr) => !textLowered.find(ltr).empty);
}

This approach starts by importing from libraries for what is needed.

An immutable binding to a string of all the English lowercase letters is defined. Although a string type is immutable, meaning it can't be modified in place, the immutable binding ensures that the abc binding can't be assigned to another string.

import std;
void main()
{
    auto ab = "ab";
    // ab[0] = "b"; // compile error: cannot modify `immutable` expression `ab[0]`
    ab = "cd"; // okay to set a mutable binding to another value
    immutable cd = ab; // okay to set an immutable binding to the value of a mutable binding
    // cd = "ef"; //compile error: cannot modify `immutable` expression `cd`
    ab = "gh"; // still okay to set a mutable binding to another value
    writeln(ab);
    writeln(cd);
}

// prints gh
// prints cd

In the above code example, we see that, although the ab binding is not immutable, we still can't change it in place, as all strings are immutable. We can, however, change the ab binding to another value.

We can set the immutable binding of cd to the current value of the mutable ab binding. Once the immutable cd binding is set, we cannot set it to another value.

Although the immutable cd binding was set to the value of ab, ab can still be changed to another value independent of the cd binding. The abbinding is changed to the "gh" value, and the cd binding still has its "cd" value.

The isPangram function is marked @safe to ensure the compiler disallows certain unsafe practices in the function implementation. It is also marked as pure to ensure it does not modify any state external to itself.

A binding is defined for the text input lowercased with the toLower function using Uniform Function Call Syntax.

auto textLowered = text.toLower;
return abc.all!((ltr) => !textLowered.find(ltr).empty);

The all function is then called on the string of English letters. The lambda takes each letter from the English letters and passes it to the find function called on the lowercased input. The lambda uses the logical NOT operator (!) to test the result of calling the find function.

If the find function does not return an empty result, then the letter was found, and find returns true, otherwise it returns false. If all calls to find are true, then all returns true. If any call to find returns false, then all returns false.

Finally, isPangram returns the result of calling all.

11th Dec 2024 · Found it useful?