On Friday, 5 March 2021 at 05:32:27 UTC, Jack wrote:
On Friday, 5 March 2021 at 02:43:36 UTC, H. S. Teoh wrote:
On Fri, Mar 05, 2021 at 02:13:39AM +0000, Jack via Digitalmars-d-learn wrote:
something like filter[1] but that stops at first match? are there any native functions for this in D or I have to write one? just making sure to not reinvent the wheel
[...]

Why not just .front?  E.g.:

        int[] data = [ 1,2,3,4,5 ];
        auto r = data.filter!(v => v % 2 == 0);
        assert(r.front == 2);


T

it loops over the entire array then returns, I'd like to stop as soon as the predicate return true

  void main() {
      import std.stdio, std.algorithm, std.range;
      int[] data = iota(5).map!"a+1".array;
auto r = data.filter!(function (v) { writeln(v); return v % 2 == 0; });
      assert(r.front == 2);
  }

output:

  1
  2

'r' is an iterator. To force it to loop over the entire array that
would need a .array like I'm using for 'data'.

Reply via email to