digitalmars.D.learn - Is there any generic iteration function that stops at first match?
- Jack (4/4) Mar 04 2021 something like filter[1] but that stops at first match? are there
- H. S. Teoh (9/12) Mar 04 2021 [...]
- Jack (3/14) Mar 04 2021 it loops over the entire array then returns, I'd like to stop as
- mipri (13/31) Mar 04 2021 void main() {
- Steven Schveighoffer (3/7) Mar 04 2021 std.algorithm.searching.until
- Jack (3/12) Mar 04 2021 thanks, totally overlooked this searching section
- Jesse Phillips (12/16) Mar 05 2021 std.algorithm.searching.find
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
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
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:it loops over the entire array then returns, I'd like to stop as soon as the predicate return truesomething 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
Mar 04 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: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'.On Fri, Mar 05, 2021 at 02:13:39AM +0000, Jack via Digitalmars-d-learn wrote:it loops over the entire array then returns, I'd like to stop as soon as the predicate return truesomething 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
Mar 04 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#filterstd.algorithm.searching.until -Steve
Mar 04 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:thanks, totally overlooked this searching sectionsomething 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#filterstd.algorithm.searching.until -Steve
Mar 04 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#filterstd.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