Re: 200-600x slower Dlang performance with nested foreach loop

2021-01-31 Thread a11e99z via Digitalmars-d-learn
On Sunday, 31 January 2021 at 16:50:15 UTC, methonash wrote: What confuses me, at this point, is this: I originally wrote the D code using foreach in this style: foreach( i, ref parentString; strings ) foreach( j, ref childString; strings[ i + 1 .. $ ] ) Essentially, the value of j

Re: 200-600x slower Dlang performance with nested foreach loop

2021-01-31 Thread methonash via Digitalmars-d-learn
On Sunday, 31 January 2021 at 00:53:05 UTC, Steven Schveighoffer wrote: I'd suggest trying it in reverse. If you have the sequence "cba", "ba", "a", then determining "a" is in "ba" is probably cheaper than determining "a" is in "cba". I have user requirements that this application track

Re: 200-600x slower Dlang performance with nested foreach loop

2021-01-30 Thread CraigDillabaugh via Digitalmars-d-learn
typically be an O(n^2) approach. However, due to subsetting the input dataset to unique strings and then sorting in descending length, one might notice that the inner foreach loop does not iterate over all of n, only on the iterator value i+1 through the end of the array. Thus, I believe

Re: 200-600x slower Dlang performance with nested foreach loop

2021-01-30 Thread Steven Schveighoffer via Digitalmars-d-learn
On 1/30/21 6:13 PM, methonash wrote: Greetings all, Many thanks for sharing your collective perspective and advice thus far! It has been very helpful and instructive. I return bearing live data and a minimally complete, compilable, and executable program to experiment with and potentially

Re: 200-600x slower Dlang performance with nested foreach loop

2021-01-30 Thread methonash via Digitalmars-d-learn
Greetings all, Many thanks for sharing your collective perspective and advice thus far! It has been very helpful and instructive. I return bearing live data and a minimally complete, compilable, and executable program to experiment with and potentially optimize. The dataset can be pulled

Re: 200-600x slower Dlang performance with nested foreach loop

2021-01-27 Thread Steven Schveighoffer via Digitalmars-d-learn
On 1/27/21 10:14 AM, Paul Backus wrote: On Wednesday, 27 January 2021 at 15:12:32 UTC, Paul Backus wrote: Maybe it's to avoid invalidating the result of `key in aa` when adding or removing entries? The spec doesn't say anything about it either way [1], but allowing invalidation would make

Re: 200-600x slower Dlang performance with nested foreach loop

2021-01-27 Thread Paul Backus via Digitalmars-d-learn
On Wednesday, 27 January 2021 at 14:15:26 UTC, FeepingCreature wrote: Associative arrays allocate per entry added?! https://github.com/dlang/druntime/blob/master/src/rt/aaA.d#L205 Oh God, associative arrays allocate per entry added! Maybe it's to avoid invalidating the result of `key in aa`

Re: 200-600x slower Dlang performance with nested foreach loop

2021-01-27 Thread Paul Backus via Digitalmars-d-learn
On Wednesday, 27 January 2021 at 15:12:32 UTC, Paul Backus wrote: Maybe it's to avoid invalidating the result of `key in aa` when adding or removing entries? The spec doesn't say anything about it either way [1], but allowing invalidation would make AAs much more difficult to use in @safe

Re: 200-600x slower Dlang performance with nested foreach loop

2021-01-27 Thread FeepingCreature via Digitalmars-d-learn
On Wednesday, 27 January 2021 at 02:14:39 UTC, H. S. Teoh wrote: Yes, definitely try this. This will completely eliminate the overhead of using an AA, which has to allocate memory (at least) once per entry added. Especially since the data has to be sorted eventually anyway, you might as well

Re: 200-600x slower Dlang performance with nested foreach loop

2021-01-26 Thread H. S. Teoh via Digitalmars-d-learn
On Wed, Jan 27, 2021 at 01:28:33AM +, Paul Backus via Digitalmars-d-learn wrote: > On Tuesday, 26 January 2021 at 23:57:43 UTC, methonash wrote: > > > Using AA's may not necessarily improve performance. It depends on > > > what your code does with it. Because AA's require random access > >

Re: 200-600x slower Dlang performance with nested foreach loop

2021-01-26 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 26 January 2021 at 23:57:43 UTC, methonash wrote: Using AA's may not necessarily improve performance. It depends on what your code does with it. Because AA's require random access to memory, it's not friendly to the CPU cache hierarchy, whereas traversing linear arrays is more

Re: 200-600x slower Dlang performance with nested foreach loop

2021-01-26 Thread methonash via Digitalmars-d-learn
how to improve this algorithm. Nice observation, and yes, this would typically be an O(n^2) approach. However, due to subsetting the input dataset to unique strings and then sorting in descending length, one might notice that the inner foreach loop does not iterate over all of n, only

Re: 200-600x slower Dlang performance with nested foreach loop

2021-01-26 Thread mw via Digitalmars-d-learn
On Tuesday, 26 January 2021 at 21:55:47 UTC, mw wrote: On Tuesday, 26 January 2021 at 17:40:36 UTC, methonash wrote: foreach( i, ref pStr; sortedArr ) { foreach( j, ref cStr; sortedArr[ i + 1 .. $ ] ) { if( indexOf( pStr, cStr ) > -1 ) { // ... yourInnerOp

Re: 200-600x slower Dlang performance with nested foreach loop

2021-01-26 Thread mw via Digitalmars-d-learn
On Tuesday, 26 January 2021 at 17:40:36 UTC, methonash wrote: foreach( i, ref pStr; sortedArr ) { foreach( j, ref cStr; sortedArr[ i + 1 .. $ ] ) { if( indexOf( pStr, cStr ) > -1 ) { // ... } } } Before adding the code excerpt above, the Dlang

Re: 200-600x slower Dlang performance with nested foreach loop

2021-01-26 Thread Steven Schveighoffer via Digitalmars-d-learn
On 1/26/21 12:40 PM, methonash wrote: My first attempt to solve this problem space used a small Perl program to perform steps 1 through 3, which would then pipe intermediate output to a small Dlang program handling only step #4 using dynamic arrays (no use of AAs) of ubyte[][] with use of

Re: 200-600x slower Dlang performance with nested foreach loop

2021-01-26 Thread H. S. Teoh via Digitalmars-d-learn
On Tue, Jan 26, 2021 at 06:13:54PM +, methonash via Digitalmars-d-learn wrote: [...] > I cannot post the full source code. Then we are limited in how much we can help you. > Regarding a reduced version reproducing the issue: well, that's > exactly what the nested foreach loop does

Re: 200-600x slower Dlang performance with nested foreach loop

2021-01-26 Thread H. S. Teoh via Digitalmars-d-learn
for is .release, not .array. [...] > With the formally returned array, I then attempted to construct a > double foreach loop to iterate through the sorted array of unique > strings and find substring matches. > > foreach( i, ref pStr; sortedArr ) > { > foreach( j, ref cStr; sortedArr[ i

Re: 200-600x slower Dlang performance with nested foreach loop

2021-01-26 Thread Imperatorn via Digitalmars-d-learn
On Tuesday, 26 January 2021 at 17:40:36 UTC, methonash wrote: Greetings Dlang wizards, I seek knowledge/understanding of a very frustrating phenomenon I've experienced over the past several days. [...] Source please 

Re: 200-600x slower Dlang performance with nested foreach loop

2021-01-26 Thread methonash via Digitalmars-d-learn
, it is quite likely that some part of the code you do not suspect is actually to blame. I cannot post the full source code. Regarding a reduced version reproducing the issue: well, that's exactly what the nested foreach loop does. Without it, the program reaches that point quickly. With the nested

Re: 200-600x slower Dlang performance with nested foreach loop

2021-01-26 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 26 January 2021 at 17:40:36 UTC, methonash wrote: foreach( i, ref pStr; sortedArr ) { foreach( j, ref cStr; sortedArr[ i + 1 .. $ ] ) { if( indexOf( pStr, cStr ) > -1 ) { // ... } } } Before adding the code excerpt above, the Dlang

200-600x slower Dlang performance with nested foreach loop

2021-01-26 Thread methonash via Digitalmars-d-learn
to convert the returned SortedRange into an array of type string[]. This appeared to work, and neither DMD nor LDC threw any warnings/errors for doing this. With the formally returned array, I then attempted to construct a double foreach loop to iterate through the sorted array of unique

Re: How to send ownerTid into a parallel foreach loop?

2020-06-27 Thread Kagamin via Digitalmars-d-learn
On Saturday, 27 June 2020 at 07:51:21 UTC, adnan338 wrote: On Saturday, 27 June 2020 at 07:31:56 UTC, Kagamin wrote: std.concurrency is for noninteractive appications, the approach with gui timer was the correct one. Thank you. That works but my progress bar is sometimes getting stuck

Re: How to send ownerTid into a parallel foreach loop?

2020-06-27 Thread adnan338 via Digitalmars-d-learn
On Saturday, 27 June 2020 at 07:31:56 UTC, Kagamin wrote: std.concurrency is for noninteractive appications, the approach with gui timer was the correct one. Thank you. That works but my progress bar is sometimes getting stuck because of a possible data race. See

Re: How to send ownerTid into a parallel foreach loop?

2020-06-27 Thread Kagamin via Digitalmars-d-learn
std.concurrency is for noninteractive appications, the approach with gui timer was the correct one.

How to send ownerTid into a parallel foreach loop?

2020-06-26 Thread adnan338 via Digitalmars-d-learn
I have a list of files to download, and I want to download them in parallel. At the end of each of those parallel download I want to send the main thread a message from the parallel loop. import std.concurrency, std.parallelism; string[] files = ["a", "b", "c"]; void download(string[] links)

Re: Convert program to 2020: replace foreach loop with map, filter and friends

2020-03-30 Thread Vladimir Panteleev via Digitalmars-d-learn
On Tuesday, 31 March 2020 at 04:00:28 UTC, User wrote: I'd like to convert the following program to 2020 standards (i.e, replace the foreach block with a one-line code). I've tried much and I failed. Here is how I'd do it. Because the program downloads and then reads the local file

Convert program to 2020: replace foreach loop with map, filter and friends

2020-03-30 Thread User via Digitalmars-d-learn
I'd like to convert the following program to 2020 standards (i.e, replace the foreach block with a one-line code). I've tried much and I failed. This is the code that works (1990s style) -- import std; void main() { immutable URL =

Re: Is removing elements of AA in foreach loop safe?

2019-09-04 Thread Ferhat Kurtulmuş via Digitalmars-d-learn
On Wednesday, 4 September 2019 at 06:20:00 UTC, berni wrote: On Tuesday, 3 September 2019 at 20:06:27 UTC, Ferhat Kurtulmuş wrote: I know, it is foreach loop in question. How about using a reverse for loop like: for (size_t i = arr.length ; i-- > 0 ; ){ arr.remove(i); } This wo

Re: Is removing elements of AA in foreach loop safe?

2019-09-04 Thread berni via Digitalmars-d-learn
On Tuesday, 3 September 2019 at 20:06:27 UTC, Ferhat Kurtulmuş wrote: I know, it is foreach loop in question. How about using a reverse for loop like: for (size_t i = arr.length ; i-- > 0 ; ){ arr.remove(i); } This would be good, if it where for slices. But with associative arr

Re: Is removing elements of AA in foreach loop safe?

2019-09-03 Thread Ferhat Kurtulmuş via Digitalmars-d-learn
you agree? Or is there a better way to achieve this? I know, it is foreach loop in question. How about using a reverse for loop like: for (size_t i = arr.length ; i-- > 0 ; ){ arr.remove(i); }

Re: Is removing elements of AA in foreach loop safe?

2019-08-30 Thread Jordan Wilson via Digitalmars-d-learn
On Thursday, 29 August 2019 at 10:11:58 UTC, berni wrote: Iterating of some structure and removing elements thereby is always errorprone and should be avoided. But: In case of AA, I've got the feeling, that it might be safe: foreach (k,v;ways) if (v.empty) ways.remove(k); Do

Re: Is removing elements of AA in foreach loop safe?

2019-08-30 Thread H. S. Teoh via Digitalmars-d-learn
reshold, the AA will > > be resized [1], and its original memory will be freed [2]. > > It could still work, depending on how the foreach loop is implemented. > If the keys were stored away before starting the loop it would work. > But for one thing, it isn't implemented that way and fo

Re: Is removing elements of AA in foreach loop safe?

2019-08-30 Thread berni via Digitalmars-d-learn
the foreach loop is implemented. If the keys were stored away before starting the loop it would work. But for one thing, it isn't implemented that way and for the other, one shouldn't rely on it, because the implementation could change. What I hoped for, was, that the specs enforce somewhere

Re: Is removing elements of AA in foreach loop safe?

2019-08-30 Thread Paul Backus via Digitalmars-d-learn
On Friday, 30 August 2019 at 13:43:54 UTC, XavierAP wrote: On Thursday, 29 August 2019 at 10:11:58 UTC, berni wrote: Iterating of some structure and removing elements thereby is always errorprone and should be avoided. But: In case of AA, I've got the feeling, that it might be safe: foreach

Re: Is removing elements of AA in foreach loop safe?

2019-08-30 Thread XavierAP via Digitalmars-d-learn
On Thursday, 29 August 2019 at 10:11:58 UTC, berni wrote: Do you agree? Or is there a better way to achieve this? An alternative would be to reassign the AAA to the output of std.algorithm.filter()... but assignment between AAs and Ranges isn't so type-direct.

Re: Is removing elements of AA in foreach loop safe?

2019-08-30 Thread XavierAP via Digitalmars-d-learn
On Thursday, 29 August 2019 at 10:11:58 UTC, berni wrote: Iterating of some structure and removing elements thereby is always errorprone and should be avoided. But: In case of AA, I've got the feeling, that it might be safe: foreach (k,v;ways) if (v.empty) ways.remove(k); Do

Re: Is removing elements of AA in foreach loop safe?

2019-08-29 Thread Jonathan M Davis via Digitalmars-d-learn
On Thursday, August 29, 2019 4:11:58 AM MDT berni via Digitalmars-d-learn wrote: > Iterating of some structure and removing elements thereby is > always errorprone and should be avoided. But: In case of AA, I've > > got the feeling, that it might be safe: > > foreach (k,v;ways) > > > > if

Is removing elements of AA in foreach loop safe?

2019-08-29 Thread berni via Digitalmars-d-learn
Iterating of some structure and removing elements thereby is always errorprone and should be avoided. But: In case of AA, I've got the feeling, that it might be safe: foreach (k,v;ways) if (v.empty) ways.remove(k); Do you agree? Or is there a better way to achieve this?

Re: [TWiD] static foreach loop variable

2019-05-28 Thread Nick Treleaven via Digitalmars-d-learn
Ok, thanks for explaining. Nice idea.

Re: [TWiD] static foreach loop variable

2019-05-28 Thread Adam D. Ruppe via Digitalmars-d-learn
On Tuesday, 28 May 2019 at 13:43:45 UTC, Nick Treleaven wrote: Hi, Last week's TWiD had a tip that didn't make sense: http://dpldocs.info/this-week-in-d/Blog.Posted_2019_05_20.html#tip-of-the-week template Locals(int i) { alias Whatever = int; } static foreach(i; [1, 2, 3]) {

[TWiD] static foreach loop variable

2019-05-28 Thread Nick Treleaven via Digitalmars-d-learn
Hi, Last week's TWiD had a tip that didn't make sense: http://dpldocs.info/this-week-in-d/Blog.Posted_2019_05_20.html#tip-of-the-week template Locals(int i) { alias Whatever = int; } static foreach(i; [1, 2, 3]) { Locals!i.Whatever; } The body is just `int;`. Not sure how to reach Adam.

Re: Deprecation: foreach: loop index implicitly converted from size_t to int

2019-01-18 Thread Jonathan M Davis via Digitalmars-d-learn
On Friday, January 18, 2019 8:34:22 AM MST Michael via Digitalmars-d-learn wrote: > On Friday, 18 January 2019 at 13:29:29 UTC, Adam D. Ruppe wrote: > > On Friday, 18 January 2019 at 12:27:17 UTC, Michael wrote: > >> This, to be, looks like quite the explicit conversion, no? > > > > Yeah, I

Re: Deprecation: foreach: loop index implicitly converted from size_t to int

2019-01-18 Thread Michael via Digitalmars-d-learn
On Friday, 18 January 2019 at 13:29:29 UTC, Adam D. Ruppe wrote: On Friday, 18 January 2019 at 12:27:17 UTC, Michael wrote: This, to be, looks like quite the explicit conversion, no? Yeah, I agree. But the language is silly. I just leave the type out of foreach and explicitly cast it inside

Re: Deprecation: foreach: loop index implicitly converted from size_t to int

2019-01-18 Thread Adam D. Ruppe via Digitalmars-d-learn
On Friday, 18 January 2019 at 12:27:17 UTC, Michael wrote: This, to be, looks like quite the explicit conversion, no? Yeah, I agree. But the language is silly. I just leave the type out of foreach and explicitly cast it inside the body.

Re: Deprecation: foreach: loop index implicitly converted from size_t to int

2019-01-18 Thread Nicholas Wilson via Digitalmars-d-learn
On Friday, 18 January 2019 at 12:27:17 UTC, Michael wrote: Hello all, I am getting this deprecation warning when compiling using DMD64 D Compiler v2.084.0 on Linux. I'm a little unsure what the problem is, however, because the code producing these warnings tends to be of the form: foreach

Re: Deprecation: foreach: loop index implicitly converted from size_t to int

2019-01-18 Thread Steven Schveighoffer via Digitalmars-d-learn
On 1/18/19 7:27 AM, Michael wrote: Hello all, I am getting this deprecation warning when compiling using DMD64 D Compiler v2.084.0 on Linux. I'm a little unsure what the problem is, however, because the code producing these warnings tends to be of the form: foreach (int i, ref prop; props)

Deprecation: foreach: loop index implicitly converted from size_t to int

2019-01-18 Thread Michael via Digitalmars-d-learn
Hello all, I am getting this deprecation warning when compiling using DMD64 D Compiler v2.084.0 on Linux. I'm a little unsure what the problem is, however, because the code producing these warnings tends to be of the form: foreach (int i, ref prop; props) This, to be, looks like quite

Re: BigInt foreach loop

2017-08-09 Thread Steven Schveighoffer via Digitalmars-d-learn
On 8/9/17 9:08 PM, Steven Schveighoffer wrote: Right, but this is not a limitation of the API, just the implementation. It could be improved. https://issues.dlang.org/show_bug.cgi?id=17736 Based on H.S. Teoh's comment in the bug report, this actually is invalid. That's a tough requirement.

Re: BigInt foreach loop

2017-08-09 Thread Steven Schveighoffer via Digitalmars-d-learn
;key < limit;++key) { // use x } That's enough to know that the foreach loop does not reuse the space for the iteration variable. That was what I cared about. Note that using BigInt as iteration variable will probably cause a BigInt allocation per iteration, because operations on Big

Re: BigInt foreach loop

2017-08-09 Thread H. S. Teoh via Digitalmars-d-learn
x = key;key < limit;++key) > > { > > // use x > > } > > That's enough to know that the foreach loop does not reuse the space > for the iteration variable. That was what I cared about. Note that using BigInt as iteration variable will probably cause a BigInt allo

Re: BigInt foreach loop

2017-08-09 Thread Q. Schroll via Digitalmars-d-learn
On Friday, 4 August 2017 at 16:40:08 UTC, Stefan Koch wrote: [..] foreach(x;A .. B) it's lowerd to auto limit = B; auto key = A; for(auto x = key;key < limit;++key) { // use x } That's enough to know that the foreach loop does not reuse the space for the iteration variable. That was wha

Re: BigInt foreach loop

2017-08-04 Thread Stefan Koch via Digitalmars-d-learn
On Friday, 4 August 2017 at 13:09:55 UTC, Steven Schveighoffer wrote: Any foreach range statement like this: foreach(var; A .. B) is treated as if you wrote: for(auto var = A; var < B; ++var) So it's pretty much exactly like what you wrote, just the initializer is different but the result

Re: BigInt foreach loop

2017-08-04 Thread Steven Schveighoffer via Digitalmars-d-learn
On 8/4/17 8:49 AM, Q. Schroll wrote: One can do BigInt n = returnsBigInt(); foreach (i; BigInt(0) .. n) { .. } How is this implemented? The code for BigInt is very large and I didn't find it. And is it more efficient than for (BigInt i = 0; i < n; ++i) { .. } Any foreach range

Re: BigInt foreach loop

2017-08-04 Thread Stefan Koch via Digitalmars-d-learn
On Friday, 4 August 2017 at 12:49:30 UTC, Q. Schroll wrote: One can do BigInt n = returnsBigInt(); foreach (i; BigInt(0) .. n) { .. } How is this implemented? The code for BigInt is very large and I didn't find it. And is it more efficient than for (BigInt i = 0; i < n; ++i) { .. }

BigInt foreach loop

2017-08-04 Thread Q. Schroll via Digitalmars-d-learn
One can do BigInt n = returnsBigInt(); foreach (i; BigInt(0) .. n) { .. } How is this implemented? The code for BigInt is very large and I didn't find it. And is it more efficient than for (BigInt i = 0; i < n; ++i) { .. } as incrementing is a costly operation?

Re: 'Access Violation Error' with parallel-foreach loop

2017-03-19 Thread ikod via Digitalmars-d-learn
On Sunday, 19 March 2017 at 00:46:29 UTC, ooyu wrote: On Saturday, 18 March 2017 at 22:27:27 UTC, Vladimir Panteleev wrote: [...] Aha! Thank you. I try again using std.file.write and input ubyte[] data. auto rq = Request(); auto d =

Re: 'Access Violation Error' with parallel-foreach loop

2017-03-18 Thread ooyu via Digitalmars-d-learn
On Saturday, 18 March 2017 at 22:27:27 UTC, Vladimir Panteleev wrote: On Saturday, 18 March 2017 at 16:15:30 UTC, ooyu wrote: I don't understand why got Access Violation error. :-( That looks like https://issues.dlang.org/show_bug.cgi?id=13727, though I thought a fix was included in recent

Re: 'Access Violation Error' with parallel-foreach loop

2017-03-18 Thread ooyu via Digitalmars-d-learn
On Saturday, 18 March 2017 at 20:57:59 UTC, ikod wrote: On Saturday, 18 March 2017 at 16:15:30 UTC, ooyu wrote: When i was uing parallel-foreach loop, got this error: object.Error@(0): Access Violation 0x0056CCB8 in _flushbu

Re: 'Access Violation Error' with parallel-foreach loop

2017-03-18 Thread Vladimir Panteleev via Digitalmars-d-learn
On Saturday, 18 March 2017 at 16:15:30 UTC, ooyu wrote: I don't understand why got Access Violation error. :-( That looks like https://issues.dlang.org/show_bug.cgi?id=13727, though I thought a fix was included in recent DMD Windows versions. Try using std.file.write.

Re: 'Access Violation Error' with parallel-foreach loop

2017-03-18 Thread ikod via Digitalmars-d-learn
On Saturday, 18 March 2017 at 16:15:30 UTC, ooyu wrote: When i was uing parallel-foreach loop, got this error: object.Error@(0): Access Violation 0x0056CCB8 in _flushbu 0x005663C7 in fwrite 0x0041D39F in nothrow @nogc

'Access Violation Error' with parallel-foreach loop

2017-03-18 Thread ooyu via Digitalmars-d-learn
When i was uing parallel-foreach loop, got this error: object.Error@(0): Access Violation 0x0056CCB8 in _flushbu 0x005663C7 in fwrite 0x0041D39F in nothrow @nogc @trusted uint std.stdio.trustedFwrite!(char).trustedFwrite

Re: iota access in foreach loop

2016-06-04 Thread Alex via Digitalmars-d-learn
On Saturday, 4 June 2016 at 18:58:51 UTC, Brad Anderson wrote: On Saturday, 4 June 2016 at 18:55:09 UTC, Brad Anderson wrote: On Saturday, 4 June 2016 at 18:20:26 UTC, Alex wrote: [...] Check out enumerate() in std.range; Ah! thanks! int counter = 5; foreach(i, el;

Re: iota access in foreach loop

2016-06-04 Thread Brad Anderson via Digitalmars-d-learn
On Saturday, 4 June 2016 at 18:55:09 UTC, Brad Anderson wrote: On Saturday, 4 June 2016 at 18:20:26 UTC, Alex wrote: [...] Check out enumerate() in std.range; int counter = 5; foreach(i, el; enumerate(randomCover(iota(counter writeln("index: ", i, " element: ", el);

Re: iota access in foreach loop

2016-06-04 Thread Brad Anderson via Digitalmars-d-learn
On Saturday, 4 June 2016 at 18:20:26 UTC, Alex wrote: Hi all! Could you help me clearify why a iota can't be accessed with two arguments in a foreach loop? following tests show my problem: What does work: int[] ku = [0, 1, 2, 3, 4]; foreach(i, el; ku) writeln("index:

Re: iota access in foreach loop

2016-06-04 Thread Alex via Digitalmars-d-learn
PPS: The error shown is in line with the iota inside foreach: Error: cannot infer argument types, expected 1 argument, not 2

iota access in foreach loop

2016-06-04 Thread Alex via Digitalmars-d-learn
Hi all! Could you help me clearify why a iota can't be accessed with two arguments in a foreach loop? following tests show my problem: What does work: int[] ku = [0, 1, 2, 3, 4]; foreach(i, el; ku) writeln("index: ", i, " element: ", el); What does not w

Why `i` not working on foreach loop if it have byLine option

2015-12-17 Thread Suliman via Digitalmars-d-learn
Next code produce error: foreach(i, line;fileContent.byLine) Error: cannot infer argument types, expected 1 argument, not 2 Why it's do not work?

Re: Why `i` not working on foreach loop if it have byLine option

2015-12-17 Thread cym13 via Digitalmars-d-learn
On Thursday, 17 December 2015 at 14:09:57 UTC, Suliman wrote: Next code produce error: foreach(i, line;fileContent.byLine) Error: cannot infer argument types, expected 1 argument, not 2 Why it's do not work? Because byLine doesn't return an array, use std.range.enumerate :

Re: Why `i` not working on foreach loop if it have byLine option

2015-12-17 Thread Daniel Kozak via Digitalmars-d-learn
V Thu, 17 Dec 2015 14:09:57 + Suliman via Digitalmars-d-learn napsáno: > Next code produce error: > > foreach(i, line;fileContent.byLine) > > Error: cannot infer argument types, expected 1 argument, not 2 > > Why it's do not work?

Re: foreach loop

2015-11-04 Thread ixid via Digitalmars-d-learn
On Tuesday, 3 November 2015 at 15:06:00 UTC, Namal wrote: Can you help me out please. Thx. reduce!((x, y) => x + !y)(0, arr).writeln; This would probably be the preferred way, that uses a lambda function (x, y) => x + !y which adds the inverse of the next array value (y) to the total so far

Re: foreach loop

2015-11-03 Thread Namal via Digitalmars-d-learn
On Tuesday, 3 November 2015 at 14:52:19 UTC, Adam D. Ruppe wrote: On Tuesday, 3 November 2015 at 14:47:14 UTC, Namal wrote: I remember it is possible to get the index for each element in the foreach loop, but I forgot how to do it. Can you help me out please. Thx. for many of them

Re: foreach loop

2015-11-03 Thread wobbles via Digitalmars-d-learn
as: foreach(index, element; array) { } Thank you. I am still struggling with the functional ways of D. Now how could I write this foreach loop the functional way? bool[] arr = [ture, false, ture, ...]; int count; foreach(i;arr){ if(!i) count++; } writeln(count); writefln("Count is: %s&

Re: foreach loop

2015-11-03 Thread wobbles via Digitalmars-d-learn
On Tuesday, 3 November 2015 at 15:06:00 UTC, Namal wrote: On Tuesday, 3 November 2015 at 14:52:19 UTC, Adam D. Ruppe wrote: On Tuesday, 3 November 2015 at 14:47:14 UTC, Namal wrote: I remember it is possible to get the index for each element in the foreach loop, but I forgot how to do it. Can

Re: foreach loop

2015-11-03 Thread Namal via Digitalmars-d-learn
element in the foreach loop, but I forgot how to do it. Can you help me out please. Thx. for many of them it is as simple as: foreach(index, element; array) { } Thank you. I am still struggling with the functional ways of D. Now how could I write this foreach loop the functional way? bool

Re: foreach loop

2015-11-03 Thread Namal via Digitalmars-d-learn
element in the foreach loop, but I forgot how to do it. Can you help me out please. Thx. for many of them it is as simple as: foreach(index, element; array) { } Thank you. I am still struggling with the functional ways of D. Now how could I write this foreach loop the functional way? bool

Re: foreach loop

2015-11-03 Thread Edwin van Leeuwen via Digitalmars-d-learn
On Tuesday, 3 November 2015 at 15:29:31 UTC, Namal wrote: writefln("Count is: %s", arr .filter!(a => a==true) .sum); // Note: std.algorithm.sum is the same as // std.algorithm.reduce!((a,b)=a+b); Shouldn't you be using walkLength instead of sum, since you are counting the left over

Re: foreach loop

2015-11-03 Thread Edwin van Leeuwen via Digitalmars-d-learn
On Tuesday, 3 November 2015 at 16:55:44 UTC, wobbles wrote: On Tuesday, 3 November 2015 at 15:42:16 UTC, Edwin van Leeuwen wrote: On Tuesday, 3 November 2015 at 15:29:31 UTC, Namal wrote: writefln("Count is: %s", arr .filter!(a => a==true) .sum); // Note: std.algorithm.sum is the same as

Re: foreach loop

2015-11-03 Thread wobbles via Digitalmars-d-learn
On Tuesday, 3 November 2015 at 15:42:16 UTC, Edwin van Leeuwen wrote: On Tuesday, 3 November 2015 at 15:29:31 UTC, Namal wrote: writefln("Count is: %s", arr .filter!(a => a==true) .sum); // Note: std.algorithm.sum is the same as // std.algorithm.reduce!((a,b)=a+b); Shouldn't you be

Re: foreach loop

2015-11-03 Thread TheFlyingFiddle via Digitalmars-d-learn
On Tuesday, 3 November 2015 at 15:29:31 UTC, Namal wrote: well I tried this that way, but my count stays 0, same as if I do it in an int function with a return though I clearly have some false elements in the arr. You could also use count:

Re: foreach loop

2015-11-03 Thread Namal via Digitalmars-d-learn
Hello guys, I remember it is possible to get the index for each element in the foreach loop, but I forgot how to do it. Can you help me out please. Thx.

Re: foreach loop

2015-11-03 Thread Adam D. Ruppe via Digitalmars-d-learn
On Tuesday, 3 November 2015 at 14:47:14 UTC, Namal wrote: I remember it is possible to get the index for each element in the foreach loop, but I forgot how to do it. Can you help me out please. Thx. for many of them it is as simple as: foreach(index, element; array) { }

Re: foreach loop

2015-11-03 Thread wobbles via Digitalmars-d-learn
On Tuesday, 3 November 2015 at 14:47:14 UTC, Namal wrote: Hello guys, I remember it is possible to get the index for each element in the foreach loop, but I forgot how to do it. Can you help me out please. Thx. auto arr = ["Hello", "World"]; foreach(int idx, string st

Re: foreach loop

2015-10-19 Thread Dominikus Dittes Scherkl via Digitalmars-d-learn
On Monday, 19 October 2015 at 14:28:06 UTC, Namal wrote: Is it possible to create a foreach loop with a breakstetemen? I mean something like that for the second loop where i want to break if element from: int [] g = [9,15,21]; int [] v = [2,3,5,7,8,9,11,13,17,19]; foreach(j;1..10) for(int

Re: foreach loop

2015-10-19 Thread Rikki Cattermole via Digitalmars-d-learn
On 20/10/15 3:28 AM, Namal wrote: Is it possible to create a foreach loop with a breakstetemen? I mean something like that for the second loop where i want to break if element from: int [] g = [9,15,21]; int [] v = [2,3,5,7,8,9,11,13,17,19]; foreach(j;1..10) for(int i = 0; v[i]<j;++i){

foreach loop

2015-10-19 Thread Namal via Digitalmars-d-learn
Is it possible to create a foreach loop with a breakstetemen? I mean something like that for the second loop where i want to break if element from: int [] g = [9,15,21]; int [] v = [2,3,5,7,8,9,11,13,17,19]; foreach(j;1..10) for(int i = 0; v[i]<j;++i){ do something; }

Re: foreach loop

2015-10-19 Thread Daniel Kozak via Digitalmars-d-learn
V Mon, 19 Oct 2015 14:28:03 + Namal via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> napsáno: > Is it possible to create a foreach loop with a breakstetemen? > > I mean something like that for the second loop where i want to > break if element from: > &

Re: foreach loop

2015-10-19 Thread Namal via Digitalmars-d-learn
On Monday, 19 October 2015 at 14:43:04 UTC, Rikki Cattermole wrote: On 20/10/15 3:28 AM, Namal wrote: Is it possible to create a foreach loop with a breakstetemen? I mean something like that for the second loop where i want to break if element from: int [] g = [9,15,21]; int [] v

Re: foreach loop

2015-10-19 Thread Namal via Digitalmars-d-learn
Is it possible to use foreach backwards? foreach(int i;20..1) writeln(i); compiles but I get nothing.

Re: foreach loop

2015-10-19 Thread novice2 via Digitalmars-d-learn
On Monday, 19 October 2015 at 15:56:00 UTC, Namal wrote: Is it possible to use foreach backwards? yes http://dlang.org/statement.html#ForeachStatement http://dpaste.dzfl.pl/cf847a9e1595

Re: foreach loop

2015-10-19 Thread Mike Parker via Digitalmars-d-learn
On Monday, 19 October 2015 at 15:56:00 UTC, Namal wrote: Is it possible to use foreach backwards? foreach(int i;20..1) writeln(i); compiles but I get nothing. foreach_reverse(i; 1 .. 20) writeln(i); Or: import std.range : iota, retro; foreach(i; iota(1, 20).retro) writeln(i); But

Re: Dynamic array and foreach loop

2015-08-09 Thread Alex Parrill via Digitalmars-d-learn
On Sunday, 9 August 2015 at 15:37:23 UTC, Binarydepth wrote: So I should use the REF like this ? import std.stdio : writeln; void main() { immutable a=5; int[a] Arr; foreach(num; 0..a) { Arr[num] = num; } foreach(num, ref ele;

Re: Dynamic array and foreach loop

2015-08-09 Thread Binarydepth via Digitalmars-d-learn
On Sunday, 9 August 2015 at 00:22:53 UTC, Jay Norwood wrote: On Saturday, 8 August 2015 at 18:28:25 UTC, Binarydepth wrote: This is the new code : foreach(num; 0..liEle) {//Data input loop write(Input the element : , num+1, ); readf( %d,

Re: Dynamic array and foreach loop

2015-08-09 Thread Binarydepth via Digitalmars-d-learn
On Sunday, 9 August 2015 at 16:42:16 UTC, Jay Norwood wrote: The i+3 initialization is just so you can see that v is the Arr member (not the index) in the other loops. import std.stdio : writeln; void main() { immutable a=5; int[a] Arr; foreach(i, ref v; Arr) {

Re: Dynamic array and foreach loop

2015-08-09 Thread Jay Norwood via Digitalmars-d-learn
On Sunday, 9 August 2015 at 15:37:23 UTC, Binarydepth wrote: So I should use the REF like this ? import std.stdio : writeln; void main() { immutable a=5; int[a] Arr; foreach(num; 0..a) { Arr[num] = num; } foreach(num, ref ele;

Re: Dynamic array and foreach loop

2015-08-09 Thread Jay Norwood via Digitalmars-d-learn
On Sunday, 9 August 2015 at 19:10:01 UTC, Binarydepth wrote: On Sunday, 9 August 2015 at 16:42:16 UTC, Jay Norwood wrote: Oooh... I like how this works import std.stdio : writeln, readf; void main() { immutable a=5; int[a] Arr; int nim; foreach(num, ref nem;

Dynamic array and foreach loop

2015-08-08 Thread Binarydepth via Digitalmars-d-learn
I'm writing a program that rotates numbers then asks the user if a new set of numbers should be rotated. I'm having trouble using a Foreach loop to fill a dynamic array with the elements to be rotated. Here's my code, I add a TAB when a loop is inside a loop and and do that too

Re: Dynamic array and foreach loop

2015-08-08 Thread Binarydepth via Digitalmars-d-learn
Here's what happens : How many elements need to be used? 5 Input the element : 1 1 Input the element : 1 2 Input the element : 1 3 Input the element : 1 4 Input the element : 1 5 How many positions do you wish to rotate ? 3 The original patter is : 5 0 0 0 0 The final is : 0 0 0 5 0 Do you want

Re: Dynamic array and foreach loop

2015-08-08 Thread DarthCthulhu via Digitalmars-d-learn
On Saturday, 8 August 2015 at 15:57:15 UTC, Binarydepth wrote: Here's what happens : How many elements need to be used? 5 Input the element : 1 1 Input the element : 1 2 Input the element : 1 3 Input the element : 1 4 Input the element : 1 5 How many positions do you wish to rotate ? 3 The

Re: Dynamic array and foreach loop

2015-08-08 Thread Binarydepth via Digitalmars-d-learn
On Saturday, 8 August 2015 at 17:19:08 UTC, DarthCthulhu wrote: You can fix it like the following: foreach(num, element; liaOrig) {//Data input loop writefln(num: %s current element: %s liaOrig.length: %s, num, element, liaOrig.length);

Re: Dynamic array and foreach loop

2015-08-08 Thread Binarydepth via Digitalmars-d-learn
On Saturday, 8 August 2015 at 18:24:48 UTC, Binarydepth wrote: On Saturday, 8 August 2015 at 17:19:08 UTC, DarthCthulhu wrote: Now 'num' is just an iterative number starting from 0 (the .init value of an int), while the actual element value is stored in 'element'. I added the writefln()

  1   2   >