On Thursday, 9 April 2015 at 12:57:26 UTC, Jack Applegame wrote:
I quite often have to write similar designs:

-----
import std.stdio;

void main() {

        auto a = [ 1, 2, 3, 4, 5 ];

        foreach (e; a) {
                if (e == 4) {
                        writeln("Yes");
                        return;
                }
        }
        
        writeln("No");
}
-----

But is not it easier to write :)

import std.stdio;

void main() {

        auto a = [ 1, 2, 3, 4, 5 ];

        foreach (e; a) {
                if (e == 4) {
                        return writeln("Yes");
                }
        }
        
        writeln("No");
}

import std.stdio;
import std.algorithm;
import std.array;

void main() {
  auto a = [ 1, 2, 3, 4, 5 ];
  writeln(a.find(4).empty ? "No" : "Yes");
}

import std.stdio;

void main() {

        foreach (...) {
                foreach (...) {
                        ...
                        if (...) {
                                ...
                                return writeln("Yes");
                        }
                        ...
                }
                ...
        }

        ...
        
        writeln("No");
}

No design can be completely arbitrary:

-----
import std.stdio;

void main() {

        foreach (...) {
                foreach (...) {
                        ...
                        if (...) {
                                ...
                                return writeln("Yes");
                        }
                        ...
                }
                ...
        }

        ...
        
        writeln("No");
}

Reply via email to