How to make rsplit (like in Python) in D
Uranuz via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sat Oct 1 10:32:59 PDT 2016
On Saturday, 1 October 2016 at 17:23:16 UTC, Uranuz wrote:
> On Saturday, 1 October 2016 at 16:45:11 UTC, Uranuz wrote:
>> How to make rsplit (like in Python) in D without need for
>> extra allocation using standard library? And why there is no
>> algorithms (or parameter in existing algorithms) to process
>> range from the back. Is `back` and `popBack` somehow worse
>> than `front` and `popFront`.
>>
>> I've tried to write somethig that would work without
>> allocation, but failed.
>> I have searching in forum. Found this thread:
>> https://forum.dlang.org/post/bug-10309-3@http.d.puremagic.com%2Fissues%2F
>>
>> I tried to use `findSplitBefore` with `retro`, but it doesn't
>> compile:
>>
>> import std.stdio;
>> import std.algorithm;
>> import std.range;
>> import std.string;
>>
>> void main()
>> {
>> string str = "Human.Engineer.Programmer.DProgrammer";
>>
>> writeln( findSplitBefore(retro(str), ".")[0].retro );
>> }
>>
>> Compilation output:
>> /d153/f534.d(10): Error: template std.range.retro cannot
>> deduce function from argument types !()(Result), candidates
>> are:
>> /opt/compilers/dmd2/include/std/range/package.d(198):
>> std.range.retro(Range)(Range r) if
>> (isBidirectionalRange!(Unqual!Range))
>>
>>
>> Why I have to write such strange things to do enough
>> wide-spread operation. I using Python at the job and there is
>> very much cases when I use rsplit. So it's very strange to me
>> that D library has a lot of `advanced` algorithms that are not
>> very commonly used, but there is no rsplit.
>>
>> Maybe I missing something, so please give me some advice)
>
> Sorry for noise. It was easy enough:
>
> import std.stdio;
> import std.algorithm;
> import std.range;
> import std.string;
>
> void main()
> {
> string str = "Human.Engineer.Programmer.DProgrammer";
>
> writeln( splitter(str, '.').back );
> }
>
>
> But I still interested why the above not compiles and how to do
> `rfind` or indexOf from the right in D. I think even if we do
> not have exactly algorithms with these names we could provide
> some examples how to *emulate* behaviour of standard functions
> from other popular languages)
But these example fails. Oops. Looks like a bug(
import std.stdio;
import std.algorithm;
import std.range;
import std.string;
void main()
{
string str = "";
writeln( splitter(str, '.').back );
}
core.exception.AssertError at std/algorithm/iteration.d(3132):
Assertion failure
----------------
??:? _d_assert [0x43dd1f]
??:? void std.algorithm.iteration.__assert(int) [0x4432b0]
??:? pure @property @safe immutable(char)[]
std.algorithm.iteration.splitter!("a == b", immutable(char)[],
char).splitter(immutable(char)[], char).Result.back() [0x43b8d6]
??:? _Dmain [0x43ae41]
??:?
_D2rt6dmain211_d_run_mainUiPPaPUAAaZiZ6runAllMFZ9__lambda1MFZv
[0x43e33e]
??:? void rt.dmain2._d_run_main(int, char**, extern (C) int
function(char[][])*).tryExec(scope void delegate()) [0x43e288]
??:? void rt.dmain2._d_run_main(int, char**, extern (C) int
function(char[][])*).runAll() [0x43e2fa]
??:? void rt.dmain2._d_run_main(int, char**, extern (C) int
function(char[][])*).tryExec(scope void delegate()) [0x43e288]
??:? _d_run_main [0x43e1f9]
??:? main [0x43d049]
More information about the Digitalmars-d-learn
mailing list