www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Is there any generic iteration function that stops at first match?

reply Jack <jckj33 gmail.com> writes:
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
Mar 04 2021
next sibling parent reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
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.
Mar 04 2021
parent reply Jack <jckj33 gmail.com> writes:
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
Mar 04 2021
parent mipri <mipri minimaltype.com> writes:
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'.
Mar 04 2021
prev sibling next sibling parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
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
Mar 04 2021
parent Jack <jckj33 gmail.com> writes:
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
Mar 04 2021
prev sibling parent Jesse Phillips <Jesse.K.Phillips+D gmail.com> writes:
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.
Mar 05 2021