Re: D idom for removing array elements

2017-01-30 Thread ag0aep6g via Digitalmars-d-learn
On 01/30/2017 01:33 PM, albert-j wrote: On Monday, 30 January 2017 at 12:31:33 UTC, albert-j wrote: OK, got it. Can you do removal without reallocation with std.container.array? Array!int arr; foreach (i; 0..10) arr ~= i; Sorry, sent too early. arr = arr[].remove!(x=> x > 5); /

Re: D idom for removing array elements

2017-01-30 Thread albert-j via Digitalmars-d-learn
On Monday, 30 January 2017 at 12:31:33 UTC, albert-j wrote: OK, got it. Can you do removal without reallocation with std.container.array? Array!int arr; foreach (i; 0..10) arr ~= i; Sorry, sent too early. arr = arr[].remove!(x=> x > 5); //Doesn't work withouth calling .Array!

Re: D idom for removing array elements

2017-01-30 Thread albert-j via Digitalmars-d-learn
On Monday, 30 January 2017 at 10:45:03 UTC, cym13 wrote: Meh. Forget that, bad memory. remove isn't working in-place. However slapping ".array" is still asking explicitely for reallocation, so just forget it. Here is a code that works: import std.conv; import std.stdio; import std.format; i

Re: D idom for removing array elements

2017-01-30 Thread cym13 via Digitalmars-d-learn
On Monday, 30 January 2017 at 10:30:22 UTC, cym13 wrote: On Monday, 30 January 2017 at 08:50:14 UTC, albert-j wrote: On Monday, 30 January 2017 at 00:17:51 UTC, ag0aep6g wrote: Removing works by overwriting the array with only the wanted values and discarding the rest. But then why do I get

Re: D idom for removing array elements

2017-01-30 Thread cym13 via Digitalmars-d-learn
On Monday, 30 January 2017 at 08:50:14 UTC, albert-j wrote: On Monday, 30 January 2017 at 00:17:51 UTC, ag0aep6g wrote: Removing works by overwriting the array with only the wanted values and discarding the rest. But then why do I get this: import std.stdio, std.algorithm, std.array;

Re: D idom for removing array elements

2017-01-30 Thread albert-j via Digitalmars-d-learn
On Monday, 30 January 2017 at 00:17:51 UTC, ag0aep6g wrote: Removing works by overwriting the array with only the wanted values and discarding the rest. But then why do I get this: import std.stdio, std.algorithm, std.array; int[] arr; foreach (i; 0..10) arr ~= i; // [0, 1, 2, 3,

Re: D idom for removing array elements

2017-01-29 Thread albert-j via Digitalmars-d-learn
On Monday, 30 January 2017 at 00:17:51 UTC, ag0aep6g wrote: [...] Great explanation, thank you!

Re: D idom for removing array elements

2017-01-29 Thread ag0aep6g via Digitalmars-d-learn
On Sunday, 29 January 2017 at 21:41:57 UTC, albert-j wrote: int[] arr; foreach (i; 0..10) arr ~= i; writeln("Original array: ",arr); // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] -- OK auto arrMap = arr.filter!(x => x > 5).map!(x => x^^2); arrMap is a range. The filter and map

Re: D idom for removing array elements

2017-01-29 Thread albert-j via Digitalmars-d-learn
On Sunday, 29 January 2017 at 23:48:40 UTC, Jordan Wilson wrote: You need to do something like this: auto arrMap = arr.filter!(x => x > 5).map!(x => x^^2).array; It's because arrMap is lazy evaluated. So does it mean that I cannot assign FilterResult and MapResult to a variable and safely u

Re: D idom for removing array elements

2017-01-29 Thread Jordan Wilson via Digitalmars-d-learn
On Sunday, 29 January 2017 at 23:42:40 UTC, Jordan Wilson wrote: On Sunday, 29 January 2017 at 21:41:57 UTC, albert-j wrote: On Saturday, 28 January 2017 at 11:54:58 UTC, cym13 wrote: [...] I am trying to wrap my head around lazy evaluation during filtering/mapping, but there's something I d

Re: D idom for removing array elements

2017-01-29 Thread Jordan Wilson via Digitalmars-d-learn
On Sunday, 29 January 2017 at 21:41:57 UTC, albert-j wrote: On Saturday, 28 January 2017 at 11:54:58 UTC, cym13 wrote: [...] I am trying to wrap my head around lazy evaluation during filtering/mapping, but there's something I don't understand. I want to create an array, square some elements,

Re: D idom for removing array elements

2017-01-29 Thread albert-j via Digitalmars-d-learn
On Saturday, 28 January 2017 at 11:54:58 UTC, cym13 wrote: I am trying to wrap my head around lazy evaluation during filtering/mapping, but there's something I don't understand. I want to create an array, square some elements, remove some elements from original array and add the squared ones

Re: D idom for removing array elements

2017-01-28 Thread cym13 via Digitalmars-d-learn
On Saturday, 28 January 2017 at 10:46:29 UTC, albert-j wrote: On Friday, 27 January 2017 at 08:15:56 UTC, Dukc wrote: void main() { import std.stdio, std.algorithm, std.range, std.array, std.datetime; int[] a = [1, 2, 3, 4, 5, 6, 7, 4].cycle.take(2000).array; int[] b = [3, 4, 6].cyc

Re: D idom for removing array elements

2017-01-28 Thread cym13 via Digitalmars-d-learn
On Friday, 27 January 2017 at 11:56:02 UTC, Dukc wrote: On Friday, 27 January 2017 at 10:20:19 UTC, albert-j wrote: I am also wondering why the standard library doesn't have convenience functions for this, e.g. like Java's removeAll? Now there's more typing than necessary for a relatively commo

Re: D idom for removing array elements

2017-01-28 Thread albert-j via Digitalmars-d-learn
On Friday, 27 January 2017 at 08:15:56 UTC, Dukc wrote: void main() { import std.stdio, std.algorithm, std.range, std.array, std.datetime; int[] a = [1, 2, 3, 4, 5, 6, 7, 4].cycle.take(2000).array; int[] b = [3, 4, 6].cycle.take(2000).array; void originalMethod() { auto c

Re: D idom for removing array elements

2017-01-27 Thread cym13 via Digitalmars-d-learn
On Friday, 27 January 2017 at 15:39:57 UTC, cym13 wrote: On Friday, 27 January 2017 at 08:30:41 UTC, Dukc wrote: [...] Note that if the set of values to be excluded isn't smaller than the haystack then using partition is way faster and your method is the slowest of all. If the order of the a

Re: D idom for removing array elements

2017-01-27 Thread cym13 via Digitalmars-d-learn
On Friday, 27 January 2017 at 08:30:41 UTC, Dukc wrote: On Friday, 27 January 2017 at 08:15:56 UTC, Dukc wrote: My method is much better for large arrays I tested here. Trough, considering the size of the arrays the performance difference should be even greater, like 1000X better instead of

Re: D idom for removing array elements

2017-01-27 Thread Dukc via Digitalmars-d-learn
On Friday, 27 January 2017 at 10:20:19 UTC, albert-j wrote: I am also wondering why the standard library doesn't have convenience functions for this, e.g. like Java's removeAll? Now there's more typing than necessary for a relatively common task. That might be a good addition considering how w

Re: D idom for removing array elements

2017-01-27 Thread albert-j via Digitalmars-d-learn
On Friday, 27 January 2017 at 08:15:56 UTC, Dukc wrote: TickDuration(28085) TickDuration(42868) TickDuration(1509) Thank you, this is very helpful. I am also wondering why the standard library doesn't have convenience functions for this, e.g. like Java's removeAll? Now there's more typing t

Re: D idom for removing array elements

2017-01-27 Thread Dukc via Digitalmars-d-learn
On Friday, 27 January 2017 at 08:15:56 UTC, Dukc wrote: My method is much better for large arrays I tested here. Trough, considering the size of the arrays the performance difference should be even greater, like 1000X better instead of 15X better so it's definitely not that great.

Re: D idom for removing array elements

2017-01-27 Thread Dukc via Digitalmars-d-learn
On Thursday, 26 January 2017 at 23:10:02 UTC, albert-j wrote: Will it also work correctly and fast for arrays of custom objects? How should opCmp() be defined if objects don't have a meaningful ordering? The order of elements in the original array does not matter. Two options: either define o

Re: D idom for removing array elements

2017-01-27 Thread Dukc via Digitalmars-d-learn
On Friday, 27 January 2017 at 05:48:27 UTC, Stefan Koch wrote: To me it looks rather slow. please benchmark! void main() { import std.stdio, std.algorithm, std.range, std.array, std.datetime; int[] a = [1, 2, 3, 4, 5, 6, 7, 4].cycle.take(2000).array; int[] b = [3, 4, 6].cycle.take(2

Re: D idom for removing array elements

2017-01-26 Thread Stefan Koch via Digitalmars-d-learn
On Thursday, 26 January 2017 at 23:10:02 UTC, albert-j wrote: On Thursday, 26 January 2017 at 13:21:38 UTC, Dukc wrote: import std.stdio, std.algorithm, std.range, std.array; int[] a = [1, 2, 3, 4, 5, 6, 7, 4]; int[] b = [3, 4, 6]; auto sortedB = sort(b.dup); auto c = a . filter!(i => !sorted

Re: D idom for removing array elements

2017-01-26 Thread albert-j via Digitalmars-d-learn
On Thursday, 26 January 2017 at 13:21:38 UTC, Dukc wrote: import std.stdio, std.algorithm, std.range, std.array; int[] a = [1, 2, 3, 4, 5, 6, 7, 4]; int[] b = [3, 4, 6]; auto sortedB = sort(b.dup); auto c = a . filter!(i => !sortedB.contains(i)) . array ; assert(c == [1, 2, 5, 7]); If array

Re: D idom for removing array elements

2017-01-26 Thread Jordan Wilson via Digitalmars-d-learn
On Thursday, 26 January 2017 at 08:22:09 UTC, albert-j wrote: What is the D idiom for removing array elements that are present in another array? Is this the right/fastest way? int[] a = [1, 2, 3, 4, 5, 6, 7, 4]; int[] b = [3, 4, 6]; auto c = a.remove!(x => b.canFind(x)); assert(c == [1, 2, 5,

Re: D idom for removing array elements

2017-01-26 Thread Stefan Koch via Digitalmars-d-learn
On Thursday, 26 January 2017 at 11:44:27 UTC, Nicholas Wilson wrote: On Thursday, 26 January 2017 at 08:22:09 UTC, albert-j wrote: What is the D idiom for removing array elements that are present in another array? Is this the right/fastest way? int[] a = [1, 2, 3, 4, 5, 6, 7, 4]; int[] b = [3

Re: D idom for removing array elements

2017-01-26 Thread Dukc via Digitalmars-d-learn
On Thursday, 26 January 2017 at 08:22:09 UTC, albert-j wrote: What is the D idiom for removing array elements that are present in another array? Is this the right/fastest way? int[] a = [1, 2, 3, 4, 5, 6, 7, 4]; int[] b = [3, 4, 6]; auto c = a.remove!(x => b.canFind(x)); assert(c == [1, 2, 5,

Re: D idom for removing array elements

2017-01-26 Thread Nicholas Wilson via Digitalmars-d-learn
On Thursday, 26 January 2017 at 08:22:09 UTC, albert-j wrote: What is the D idiom for removing array elements that are present in another array? Is this the right/fastest way? int[] a = [1, 2, 3, 4, 5, 6, 7, 4]; int[] b = [3, 4, 6]; auto c = a.remove!(x => b.canFind(x)); assert(c == [1, 2, 5,