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

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;

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,

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 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

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

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 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

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

D idom for removing array elements

2017-01-26 Thread albert-j via Digitalmars-d-learn
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, 7]);

Re: General performance tips

2017-01-23 Thread albert-j via Digitalmars-d-learn
Without seeing the source there is nothing we can do. Usually performant d-code looks quite diffrent from java code. For example to avoid the gc :) Well it is actually ODE solver from Numerical recipes (originally in C++) that I am trying to do in D. Code translation seems very

General performance tips

2017-01-23 Thread albert-j via Digitalmars-d-learn
I have translated some simulation code from Java into D (a few classes, mostly manipulation of double arrays in small methods). D version runs 10-30% slower than Java (ldc2, dub release build). Profiling did not show any obvious bottlenecks. I am wondering whether I missed something, or such

Re: Profiling calls to small functions

2017-01-22 Thread albert-j via Digitalmars-d-learn
I'm not sure if it's what happening in this case but, in code as simple as this, function calls can sometimes be the bottleneck. You should see how compiling with/without -O affects performance, and adding `pragma(inline)` to funcB. I guess my question is whether it is possible to have

Re: Why does multidimensional arrays not allocate properly?

2017-01-22 Thread albert-j via Digitalmars-d-learn
In anycase, what is the correct notation for indexing? x = new int[][](width, height) and x[height][width] or x[width][height]? It's x[width][height], but because indexing is 0-based, largest valid indexes are x[width-1][height-1].

Re: Profiling calls to small functions

2017-01-21 Thread albert-j via Digitalmars-d-learn
I'm not sure if it's what happening in this case but, in code as simple as this, function calls can sometimes be the bottleneck. You should see how compiling with/without -O affects performance, and adding `pragma(inline)` to funcB. When compiled with -inline, the profiler does not report the

Profiling calls to small functions

2017-01-21 Thread albert-j via Digitalmars-d-learn
Let's say I want to create an array of random numbers and do some operations on them: void main() { import std.random; //Generate array of random numbers int arrSize = 1; double[] arr = new double[](arrSize); foreach (i; 0..arrSize) arr[i] = uniform01();

Re: Referring to array element by descriptive name

2017-01-16 Thread albert-j via Digitalmars-d-learn
Thank you for all your answers. I was concerned because I'm dealing with a small function that is called many times and where the bulk of the calculations in the simulation takes place. So even 5% performance difference would be significant for me. But it is good to know that compilers are

Referring to array element by descriptive name

2017-01-14 Thread albert-j via Digitalmars-d-learn
Is it possible to refer to an array element by a descriptive name, just for code clarity, without performance overhead? E.g. void aFunction(double[] arr) { double importantElement = arr[3]; ... use importantElement ... } But the above, I suppose, introduces an extra copy operation?

Re: Using tango with dub

2016-12-18 Thread albert-j via Digitalmars-d-learn
Have you seen https://github.com/economicmodeling/containers it has a HashSet http://economicmodeling.github.io/containers/containers/hashset.HashSet.html Just curious, how is it different from Tango's implementation?

Re: Using tango with dub

2016-12-18 Thread albert-j via Digitalmars-d-learn
Try an older version. Before resorting to that, I am also trying to "dub build --compiler=gdc". Getting different types of errors: ../../../.dub/packages/tango-1.0.3_2.068/tango/tango/math/IEEE.d:614:17: error: instead of C-style syntax, use D-style syntax 'real[3][] vals' [-Werror]

Re: Using tango with dub

2016-12-17 Thread albert-j via Digitalmars-d-learn
I thought Tango was obsolete a long time ago. Is there a specific reason you need to use Tango and can't use Phobos? I need a Set implementation and from what I understand there isn't one in Phobos right now?

Re: Using tango with dub

2016-12-17 Thread albert-j via Digitalmars-d-learn
Since I just do "dub build", I assume it invokes dmd? I have v2.072.0.

Using tango with dub

2016-12-17 Thread albert-j via Digitalmars-d-learn
I am trying to use Tango in a dub project because I need a HashSet. I added Tango as a dependency to the dub.json, but now dub gives me a bunch of depreciation warnings and a few errors, like ../../../.dub/packages/tango-1.0.3_2.068/tango/tango/util/log/Log.d(349,51): Error: undefined