Jump to page: 1 2
Thread overview
July 14
Is there a 'easy' way to sort a string in D like it is possible in Python? Also how can I remove whitespace between characters if I am reading a line from a file and safe it as a string?

    string[] buffer;

    foreach (line ; File("test.txt").byLine)
        buffer ~= line.to!string;
July 14
On 7/14/17 11:06 AM, Namal wrote:
> Is there a 'easy' way to sort a string in D like it is possible in Python? Also how can I remove whitespace between characters if I am reading a line from a file and safe it as a string?
> 
>      string[] buffer;
> 
>      foreach (line ; File("test.txt").byLine)
>          buffer ~= line.to!string;

import std.algorithm: filter;
import std.uni: isWhite;

line.filter!(c => !c.isWhite).to!string;

be warned, this is going to be a bit slow, but that's the cost of autodecoding.

If you are looking for just removing ascii whitespace, you can do it a bit more efficiently, but it's not easy.

About the string sorting, you'd have to be more specific.

-Steve
July 14
On Friday, 14 July 2017 at 15:15:42 UTC, Steven Schveighoffer wrote:


> import std.algorithm: filter;
> import std.uni: isWhite;
>
> line.filter!(c => !c.isWhite).to!string;
>
> be warned, this is going to be a bit slow, but that's the cost of autodecoding.
>
> If you are looking for just removing ascii whitespace, you can do it a bit more efficiently, but it's not easy.
>
> About the string sorting, you'd have to be more specific.
>
> -Steve

Thx Steve! By sorting string I mean a function or series of functions that sorts a string by ASCII code, "cabA" to "Aabc" for instance.


July 14
On Friday, 14 July 2017 at 15:56:49 UTC, Namal wrote:
> Thx Steve! By sorting string I mean a function or series of functions that sorts a string by ASCII code, "cabA" to "Aabc" for instance.

import std.algorithm : sort;
import std.stdio : writeln;

"cabA".dup.sort.writeln;

`dup` is used, because string cannot be modified, so a copy of string used instead.
July 14
On 7/14/17 12:43 PM, Anton Fediushin wrote:
> On Friday, 14 July 2017 at 15:56:49 UTC, Namal wrote:
>> Thx Steve! By sorting string I mean a function or series of functions that sorts a string by ASCII code, "cabA" to "Aabc" for instance.
> 
> import std.algorithm : sort;
> import std.stdio : writeln;
> 
> "cabA".dup.sort.writeln;
> 
> `dup` is used, because string cannot be modified, so a copy of string used instead.

Don't do this, because it's not what you think. It's not actually calling std.algorithm.sort, but the builtin array sort property. This will be going away soon.

Annoyingly, because of autodecoding, you have to cast to ubytes via representation to do it the "proper" way:

import std.string: representation, assumeUTF;
import std.algorithm: sort;

auto bytes = line.representation.dup;
bytes.sort;
auto result = bytes.assumeUTF; // result is now char[]

-Steve
July 14
On Friday, 14 July 2017 at 16:43:42 UTC, Anton Fediushin wrote:
> On Friday, 14 July 2017 at 15:56:49 UTC, Namal wrote:
>> Thx Steve! By sorting string I mean a function or series of functions that sorts a string by ASCII code, "cabA" to "Aabc" for instance.
>
> import std.algorithm : sort;
> import std.stdio : writeln;
>
> "cabA".dup.sort.writeln;
>
> `dup` is used, because string cannot be modified, so a copy of string used instead.

Thx alot. One final question. If I do it like that. I get a deprrecation warning:

use std.algorithm.sort instead of .sort property

Wasn't .sort() the proper way to use it, no? Because that won't compile.
July 14
On Friday, 14 July 2017 at 17:28:29 UTC, Namal wrote:
> On Friday, 14 July 2017 at 16:43:42 UTC, Anton Fediushin wrote:
>> On Friday, 14 July 2017 at 15:56:49 UTC, Namal wrote:
>>> Thx Steve! By sorting string I mean a function or series of functions that sorts a string by ASCII code, "cabA" to "Aabc" for instance.
>>
>> import std.algorithm : sort;
>> import std.stdio : writeln;
>>
>> "cabA".dup.sort.writeln;
>>
>> `dup` is used, because string cannot be modified, so a copy of string used instead.
>
> Thx alot. One final question. If I do it like that. I get a deprrecation warning:
>
> use std.algorithm.sort instead of .sort property
>
> Wasn't .sort() the proper way to use it, no? Because that won't compile.

With 2.075 you want need this anymore, as the builtin properties have finally been removeD:

https://dlang.org/changelog/2.075.0_pre.html#removeArrayProps
July 14
On 7/14/17 1:42 PM, Seb wrote:
> On Friday, 14 July 2017 at 17:28:29 UTC, Namal wrote:
>> On Friday, 14 July 2017 at 16:43:42 UTC, Anton Fediushin wrote:
>>> On Friday, 14 July 2017 at 15:56:49 UTC, Namal wrote:
>>>> Thx Steve! By sorting string I mean a function or series of functions that sorts a string by ASCII code, "cabA" to "Aabc" for instance.
>>>
>>> import std.algorithm : sort;
>>> import std.stdio : writeln;
>>>
>>> "cabA".dup.sort.writeln;
>>>
>>> `dup` is used, because string cannot be modified, so a copy of string used instead.
>>
>> Thx alot. One final question. If I do it like that. I get a deprrecation warning:
>>
>> use std.algorithm.sort instead of .sort property
>>
>> Wasn't .sort() the proper way to use it, no? Because that won't compile.
> 
> With 2.075 you want need this anymore, as the builtin properties have finally been removeD:
> 
> https://dlang.org/changelog/2.075.0_pre.html#removeArrayProps

With 2.075, it won't compile even without the parentheses, because a char[] is not an array according to std.algorithm...

See my other post.

-Steve
July 14
On Friday, 14 July 2017 at 17:23:41 UTC, Steven Schveighoffer wrote:
> Don't do this, because it's not what you think. It's not actually calling std.algorithm.sort, but the builtin array sort property. This will be going away soon.

This sucks. I know, that `.sort` will be removed, but I thought it won't break any code.

> With 2.075, it won't compile even without the parentheses, because a char[] is not an array according to std.algorithm...

But why? This should be true for `char[]`, isn't it?
-----
if ((ss == SwapStrategy.unstable && (hasSwappableElements!Range || hasAssignableElements!Range) || ss != SwapStrategy.unstable && hasAssignableElements!Range) && isRandomAccessRange!Range && hasSlicing!Range && hasLength!Range)
-----
(It's from https://dlang.org/phobos/std_algorithm_sorting.html#sort)
July 14
On 7/14/17 3:50 PM, Anton Fediushin wrote:
> On Friday, 14 July 2017 at 17:23:41 UTC, Steven Schveighoffer wrote:
>> Don't do this, because it's not what you think. It's not actually calling std.algorithm.sort, but the builtin array sort property. This will be going away soon.
> 
> This sucks. I know, that `.sort` will be removed, but I thought it won't break any code.
> 
>> With 2.075, it won't compile even without the parentheses, because a char[] is not an array according to std.algorithm...
> 
> But why? This should be true for `char[]`, isn't it?
> -----
> if ((ss == SwapStrategy.unstable && (hasSwappableElements!Range || hasAssignableElements!Range) || ss != SwapStrategy.unstable && hasAssignableElements!Range) && isRandomAccessRange!Range && hasSlicing!Range && hasLength!Range)
> -----
> (It's from https://dlang.org/phobos/std_algorithm_sorting.html#sort)

static assert(!isRandomAccessRange!(char[]));
static assert(!hasSlicing!(char[]));
static assert(!hasAssignableElements!(char[]));
static assert(!hasSwappableElements!(char[]));
static assert(!hasLength!(char[]));

It's because of autodecoding :) Phobos does not view char[] as an array, but rather a range of decoded dchar elements. It causes no end of problems.

-Steve
« First   ‹ Prev
1 2