September 07, 2015
On Monday, 7 September 2015 at 10:25:09 UTC, deed wrote:
> writeln(x);    // or you can pass it to a function.

I meant `writeln(x + 5)`
September 12, 2015
On Monday, 7 September 2015 at 10:28:20 UTC, deed wrote:
> On Monday, 7 September 2015 at 10:25:09 UTC, deed wrote:
>> writeln(x);    // or you can pass it to a function.
>
> I meant `writeln(x + 5)`

If I have just red your post before I started using reverse on dynamic arrays...
Anyway, there is no .reverse for strings I guess, what is the way to completely reverse a string in D?
September 13, 2015
On Saturday, 12 September 2015 at 12:51:04 UTC, Namal wrote:
> Anyway, there is no .reverse for strings I guess, what is the way to completely reverse a string in D?

What do you want to do? Do you want to keep your data in original order, but get a reversed view of it for something, or do you actually want to reverse your original array?

You can use `retro` to simply read your array backwards, i.e.:
string s = "Some text";
s.retro.writeln;           // `txet emoS`
s.writeln;                 // `Some text`
s.retro.find("e");         // `Some te` (Surprising to me. Error? 2.067.1)
s.retro.until("e").writeln;// `tx`
s.find("e");               // `Some`

The string is still kept in original order, but `retro` returns a range reading the array backwards. If you want to store a reversed string in memory, that's possible too, of course. One way of doing it, is to convert your retro range to a string:

s = s.retro.to!string;
s.writeln;                 // `txet emoS`

This will allocate new memory for the reversed copy and assign it to `s`.
Reverse is an algorithm that swaps values at different indices and since `string` is an alias for `const(char)[]`, it's not allowed. It means that each element of the array is const, so you cannot mutate any elements, but you can append or remove elements or assign the slice to view another part of the string or some other string. Hence, a `s.reverse` will give you an error. If you use `char[]` instead of `const(char)[]` you can use reverse and reuse the same memory for the reversed `char[]`. To illustrate:

char[] t = ['a', 'b', 'c'];
std.algorithm.reverse(t);
t.writeln;                 // `cba`

// s[0] = s[$-1];          // Error, cannot mutate const elements
auto r = s.retro;
s.length = 0;
r.each!(e => s ~= e);
s.writeln;                 // s has the reversed string, obtained through
                           // a temporary range object, setting length to
                           // zero and appending the elements from the
                           // range, which is allowed for const(char)[]
September 13, 2015
On Sunday, 13 September 2015 at 03:20:31 UTC, deed wrote:
> string s = "Some text";
> s.retro.find("e");         // `Some te` (Surprising to me. Error? 2.067.1)

Sorry, the above is wrong, .retro.find does indeed return what's expected.

string s = "Some text";
s.retro.find("e").writeln;   // Prints `et emoS`, as expected.
September 13, 2015
On Sunday, 13 September 2015 at 03:20:31 UTC, deed wrote:
> ...
> and since `string` is an alias for `const(char)[]`, it's not ...

string is an alias for immutable(char)[], not const(char)[].
http://dlang.org/arrays.html#strings

Sorry about the noise.


September 14, 2015
On Monday, 7 September 2015 at 10:25:09 UTC, deed wrote:
> 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]

As an aside, you should use `sort()` instead of the parentheses-less `sort`. The reason for this is that doing `arr.sort` invokes the old builtin array sorting which is terribly slow, whereas `import std.algorithm; arr.sort()` uses the much better sorting algorithm defined in Phobos.
September 14, 2015
On Monday, 14 September 2015 at 18:36:54 UTC, Meta wrote:
> As an aside, you should use `sort()` instead of the parentheses-less `sort`. The reason for this is that doing `arr.sort` invokes the old builtin array sorting which is terribly slow, whereas `import std.algorithm; arr.sort()` uses the much better sorting algorithm defined in Phobos.

Thanks for pointing out.
September 18, 2015
Hello guys, is there a nice functional way to read the file which is like


1,2,3,4,5,6
2,3,4,5,6,7
8,9,0,9,2,3

line by line, split numbers and remove each ','
convert it to int and save in a matrix int[][] arr?

September 18, 2015
On Friday, 18 September 2015 at 10:26:46 UTC, Namal wrote:
> Hello guys, is there a nice functional way to read the file which is like
>
>
> 1,2,3,4,5,6
> 2,3,4,5,6,7
> 8,9,0,9,2,3
>
> line by line, split numbers and remove each ','
> convert it to int and save in a matrix int[][] arr?

Not tested, but I think the following should work:

auto matrix = str
  .byLine
  .map!((l) => l.split(",")    // Split each line
                .map!(to!int)  // Turn into ints
                .array)        // Return an array
  .array // Copy into an array


September 18, 2015
On Friday, 18 September 2015 at 10:34:41 UTC, Edwin van Leeuwen wrote:
> On Friday, 18 September 2015 at 10:26:46 UTC, Namal wrote:
>> Hello guys, is there a nice functional way to read the file which is like
>>
>>
>> 1,2,3,4,5,6
>> 2,3,4,5,6,7
>> 8,9,0,9,2,3
>>
>> line by line, split numbers and remove each ','
>> convert it to int and save in a matrix int[][] arr?
>
> Not tested, but I think the following should work:
>
> auto matrix = str
>   .byLine
>   .map!((l) => l.split(",")    // Split each line
>                 .map!(to!int)  // Turn into ints
>                 .array)        // Return an array
>   .array // Copy into an array

And how do tell here to read my file?