Is there any generic iteration function that stops at first match?

2021-03-04 Thread Jack via Digitalmars-d-learn
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



[1]: https://devdocs.io/d/std_algorithm_iteration#filter


Re: Is there any generic iteration function that stops at first match?

2021-03-04 Thread H. S. Teoh via Digitalmars-d-learn
On Fri, Mar 05, 2021 at 02:13:39AM +, 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

-- 
There is no gravity. The earth sucks.


Re: Is there any generic iteration function that stops at first match?

2021-03-04 Thread Steven Schveighoffer via Digitalmars-d-learn

On Friday, 5 March 2021 at 02:13:39 UTC, Jack 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



[1]: https://devdocs.io/d/std_algorithm_iteration#filter


std.algorithm.searching.until

-Steve


Re: Is there any generic iteration function that stops at first match?

2021-03-04 Thread Jack via Digitalmars-d-learn

On Friday, 5 March 2021 at 02:43:36 UTC, H. S. Teoh wrote:
On Fri, Mar 05, 2021 at 02:13:39AM +, 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


Re: Is there any generic iteration function that stops at first match?

2021-03-04 Thread mipri via Digitalmars-d-learn

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 +, 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'.


Re: Is there any generic iteration function that stops at first match?

2021-03-04 Thread Jack via Digitalmars-d-learn
On Friday, 5 March 2021 at 04:22:23 UTC, Steven Schveighoffer 
wrote:

On Friday, 5 March 2021 at 02:13:39 UTC, Jack 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



[1]: https://devdocs.io/d/std_algorithm_iteration#filter


std.algorithm.searching.until

-Steve


thanks, totally overlooked this searching section


Re: Is there any generic iteration function that stops at first match?

2021-03-05 Thread Jesse Phillips via Digitalmars-d-learn

On Friday, 5 March 2021 at 02:13:39 UTC, Jack 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



[1]: https://devdocs.io/d/std_algorithm_iteration#filter


std.algorithm.searching.find

To summarize.

* filter.front
* find.front
* until
* find

The first two provide you data at the first match, `until` 
produces a range exclusive of the first match.


If you just use find it will produce a range that starts at the 
first match, unlike filter the range includes everything and not 
just the matching data.