Thread overview
Is there any generic iteration function that stops at first match?
Mar 05, 2021
Jack
Mar 05, 2021
H. S. Teoh
Mar 05, 2021
Jack
Mar 05, 2021
mipri
Mar 05, 2021
Jack
Mar 06, 2021
Jesse Phillips
March 05, 2021
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
March 04, 2021
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

-- 
There is no gravity. The earth sucks.
March 05, 2021
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
March 05, 2021
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
March 05, 2021
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
March 05, 2021
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'.
March 06, 2021
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.