September 06, 2015
On Sunday, 6 September 2015 at 15:52:38 UTC, anonymous wrote:
> On Sunday, 6 September 2015 at 15:41:34 UTC, Namal wrote:
>> is there any function that removes double elements in a sorted array?
>
> std.algorithm.iteration.uniq
>
> http://dlang.org/phobos/std_algorithm_iteration.html#uniq

Hmm, I get

Error: module comparison is in file 'std/algorithm/comparison.d' which cannot be read
import path[0] = /usr/include/dmd/phobos
import path[1] = /usr/include/dmd/druntime/import

when I try to load the headers like in the example
September 06, 2015
On Sunday, 6 September 2015 at 16:17:29 UTC, Namal wrote:
> Error: module comparison is in file 'std/algorithm/comparison.d' which cannot be read
> import path[0] = /usr/include/dmd/phobos
> import path[1] = /usr/include/dmd/druntime/import
>
> when I try to load the headers like in the example

Are you on 2.066 or older? Back then std.algorithm hasn't been split into submodules yet. Just import std.algorithm then instead of std.algorithm.comparison, std.algorithm.iteration, etc.
September 06, 2015
> Are you on 2.066 or older? Back then std.algorithm hasn't been split into submodules yet. Just import std.algorithm then instead of std.algorithm.comparison, std.algorithm.iteration, etc.

Yeah, I just checked, it is 2.066,  how can I install the new version on ubuntu with sudo apt-get? I don't realy want to mess with is manually. Also tried just to use algorithm and I get same error message.
September 06, 2015
On Sunday, 6 September 2015 at 17:57:49 UTC, Namal wrote:

> Yeah, I just checked, it is 2.066,  how can I install the new version on ubuntu with sudo apt-get?

sudo apt-get install dmd
will give you dmd v2.067.1. Don't know when it will be upgraded to 2.068 though.
September 06, 2015
On Sunday, 6 September 2015 at 20:39:27 UTC, deed wrote:
> On Sunday, 6 September 2015 at 17:57:49 UTC, Namal wrote:
>
>> Yeah, I just checked, it is 2.066,  how can I install the new version on ubuntu with sudo apt-get?
>
> sudo apt-get install dmd
> will give you dmd v2.067.1. Don't know when it will be upgraded to 2.068 though.

I used the software center to install the newest one. Now it compiles but I have no clue how to use uniq properly. I just tried

uniq(sort(arr));

and

auto arr = sort(a).uniq!("a==b").array;

but I don't get an array with unique elements.
September 06, 2015
On Sunday, 6 September 2015 at 21:01:09 UTC, Namal wrote:
> On Sunday, 6 September 2015 at 20:39:27 UTC, deed wrote:
>> On Sunday, 6 September 2015 at 17:57:49 UTC, Namal wrote:
>>
>>> Yeah, I just checked, it is 2.066,  how can I install the new version on ubuntu with sudo apt-get?
>>
>> sudo apt-get install dmd
>> will give you dmd v2.067.1. Don't know when it will be upgraded to 2.068 though.
>
> I used the software center to install the newest one. Now it compiles but I have no clue how to use uniq properly. I just tried
>
> uniq(sort(arr));
>
> and
>
> auto arr = sort(a).uniq!("a==b").array;
>
> but I don't get an array with unique elements.

That should be it though... Could you try this minimal complete test?

import std.stdio;
import std.algorithm;

void main(string[] args) {
    int[] arr = [1, 2, 4, 2, 3, 4, 1];
    arr.sort.uniq.writeln;
}

// [1, 2, 3, 4]
September 06, 2015
> That should be it though... Could you try this minimal complete test?
>
> import std.stdio;
> import std.algorithm;
>
> void main(string[] args) {
>     int[] arr = [1, 2, 4, 2, 3, 4, 1];
>     arr.sort.uniq.writeln;
> }
>
> // [1, 2, 3, 4]

yes, it works likte that.

unique(arr) I get

Error: undefined identifier 'unique', did you mean template 'uniq(alias pred = "a == b", Range)(Range r) if (isInputRange!Range && is(typeof(binaryFun!pred(r.front, r.front)) == bool))'?


September 06, 2015
On Sunday, 6 September 2015 at 21:18:28 UTC, Namal wrote:
>> That should be it though... Could you try this minimal complete test?
>>
>> import std.stdio;
>> import std.algorithm;
>>
>> void main(string[] args) {
>>     int[] arr = [1, 2, 4, 2, 3, 4, 1];
>>     arr.sort.uniq.writeln;
>> }
>>
>> // [1, 2, 3, 4]
>
> yes, it works likte that.
>
> unique(arr) I get
>
> Error: undefined identifier 'unique', did you mean template 'uniq(alias pred = "a == b", Range)(Range r) if (isInputRange!Range && is(typeof(binaryFun!pred(r.front, r.front)) == bool))'?

Well, if you don't type function names right, it will be hard to help you.
September 06, 2015
> Well, if you don't type function names right, it will be hard to help you.

oh, sorry. But I found out what I have been doing wrong besides that.

arr.sort.uniq;

uniq(arr) or arr.sort.uniq; compiles but doesn't store it in the arr array, I need to store it in a new one.

September 07, 2015
On Sunday, 6 September 2015 at 22:04:55 UTC, Namal wrote:
> oh, sorry. But I found out what I have been doing wrong besides that.
>
> arr.sort.uniq;
>
> uniq(arr) or arr.sort.uniq; compiles but doesn't store it in the arr array, I need to store it in a new one.

Right, it's like

int x = 3;
// x + 5;      // Just an expression evaluated to 8,
               // but what do you want to do with it?
               // It won't affect your program and the
               // compiler will give you an error.

int y = x + 5; // But you can assign the expression to
               // a new variable
x = x + 5;     // or you can assign it back
writeln(x);    // or you can pass it to a function.


// For your case:

int[] arr = [1, 2, 3, 2, 1, 4];
arr.sort;          // Operating on arr in place -> arr itself is mutated
arr.writeln;       // [1, 1, 2, 2, 3, 4]
arr.uniq;          // Not operating on arr, it's like the expression
                   // x + 5 (but no compiler error is given).
arr.uniq.writeln;  // [1, 2, 3, 4] (Expression passed to writeln)
arr.writeln;       // [1, 1, 2, 2, 3, 4] (Not affected)

int[] newArr = arr.uniq.array;
                   // Expression put into a new array assigned to newArr
newArr.writeln;    // [1, 2, 3, 4]
arr.writeln;       // Still the sorted array. [1, 1, 2, 2, 3, 4]
arr = arr.uniq.array; // Now arr is assigned the uniq array
arr.writeln;       // [1, 2, 3, 4]


You need to know whether the function will mutate your array; sort does, while uniq doesn't. If you want to do things requiring mutation, but still want your original data unchanged, you can duplicate the data with .dup before the mutating operations, like this:

int[] data = [1, 2, 2, 1];
int[] uniqData = data.dup.sort.uniq.array;
data.writeln;      // [1, 2, 2, 1] Unchanged, a duplicate was sorted.
uniqData.writeln;  // [1, 2]