January 02, 2014 Re: Task to throw away string parts, use joiner and splitter not very successful | ||||
---|---|---|---|---|
| ||||
Posted in reply to Rémy Mouëza | On Tuesday, 31 December 2013 at 21:23:08 UTC, Rémy Mouëza wrote: > I'd also suggest the following alternative, if you're going to discard a lot of last elements in your code: > > /// Return seq without its last element. > auto poppedBack (T) (T seq) if (isInputRange!T) { > seq.popBack; // Discards the last element. > return seq; > } That's a function in "std.range": dropBack. No need for this. On Wednesday, 1 January 2014 at 07:36:11 UTC, Dfr wrote: > This is interesting, why i can't just do it simpler way ? > > "this.is.a.string" > .splitter (".") > .popBack > .joiner (".") > .array > .writeln; > > > Because creating an extra function is not desired. Because "popBack" is not the same as (the oddly named) "poppedBack". "drop" will do what you need, without a new function. |
January 02, 2014 Re: Task to throw away string parts, use joiner and splitter not very successful | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dfr | On Wednesday, 1 January 2014 at 07:40:40 UTC, Dfr wrote:
> And one more problem here:
>
> string name = "test";
> auto nameparts = splitter(name, '.');
> writeln(typeof(joiner(nameparts, ".").array).stringof);
>
> This prints "dchar[]", but i need char[] or string, how to get my 'string' back ?
It's actually because "joiner" returns a range of dchar. It's kind of inefficient actually. If you instead write a minimal "RangeJoiner", you can get:
|
January 02, 2014 Re: Task to throw away string parts, use joiner and splitter not very successful | ||||
---|---|---|---|---|
| ||||
Posted in reply to monarch_dodra | On Thursday, 2 January 2014 at 15:50:10 UTC, monarch_dodra wrote: > On Wednesday, 1 January 2014 at 07:40:40 UTC, Dfr wrote: >> And one more problem here: >> >> string name = "test"; >> auto nameparts = splitter(name, '.'); >> writeln(typeof(joiner(nameparts, ".").array).stringof); >> >> This prints "dchar[]", but i need char[] or string, how to get my 'string' back ? > > It's actually because "joiner" returns a range of dchar. It's kind of inefficient actually. If you instead write a minimal "RangeJoiner", you can get: Pressed "sent" too soon. Anywas, as I was saying, joiner operates on RoR, so will return elements (dchar) individually. You *could* do a bit better with a "simpleJoiner", which will return strings at once: //---- auto simpleJoiner(R, E)(R r, E e) if (isInputRange!R && is(CommonType!(E, ElementType!R))) { static struct Result { private alias ElementType = CommonType!(E, .ElementType!R); private R r; private E e; private bool re = true; bool empty() @property { return r.empty; } ElementType front() @property { return re ? r.front : e; } void popFront() { if (re) r.popFront(); re = !re; } static if (isForwardRange!R) Result save() @property { return Result(r.save, e); } } return Result(r, e); } //---- Using this will produce "string at once" elements, rather than dchars at once, which is a bit more efficient. It can also help depending on how you want to "visualize" the data you are handling. Also note, if all you want to do is print, there is no reason at all call "array", simply format your range with "%-(%s%)": //---- void main() { writefln("%-(%s%)", "this.is.a.test" .splitter('.') .dropBack(1) .joiner(".") //or simpleJoiner(".") ); } //---- In both case, it'll produce: //---- this.is.a //---- If you want to know, "%(" and "%)" means "range-mode-print. The %s inside is the individual element format, and "%-(" means "natural" print. Read more about it here: http://dlang.org/phobos/std_format.html |
Copyright © 1999-2021 by the D Language Foundation