How to make rsplit (like in Python) in D
Uranuz via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sat Oct 1 10:23:16 PDT 2016
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)
More information about the Digitalmars-d-learn
mailing list