Task to throw away string parts, use joiner and splitter not very successful
Rémy Mouëza
remy.moueza at gmail.com
Wed Jan 1 08:13:14 PST 2014
The popBack function returns the element you are removing from the
array, not the array itself, thus "breaking" the chaining of function.
On 01/01/2014 08:36 AM, 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.
>
>> As Chris wrote, using double quotes to use strings instead of char
>> solves the typing issse.
>>
>> I'd also suggest the following alternative, if you're going to discard
>> a lot of last elements in your code:
>> import std.stdio;
>> import std.algorithm;
>> import std.array;
>> import std.range;
>>
>> /// Return seq without its last element.
>> auto poppedBack (T) (T seq) if (isInputRange!T) {
>> seq.popBack; // Discards the last element.
>> return seq;
>> }
>>
>> void main () {
>> // Prints "this.is.a".
>> "this.is.a.string"
>> .splitter (".")
>> .poppedBack
>> .joiner (".")
>> .array
>> .writeln;
>> }
>>
>>
>> On 12/31/2013 09:57 PM, Chris Cain wrote:
>>> On Tuesday, 31 December 2013 at 20:49:55 UTC, Dfr wrote:
>>>> Hello, i have string like "this.is.a.string" and want to throw away
>>>> some parts separated by dots, here is first attempt:
>>>>
>>>> name = "this.is.a.string"; // <-- want to make "this.is.a" from this
>>>> auto nameparts = splitter(name, '.');
>>>> auto name1 = joiner(nameparts[0 .. $-1], '.');
>>>>
>>>> And got this error: "Error: Result cannot be sliced with []"
>>>>
>>>> So, kinda fixed it (correct way?):
>>>>
>>>> name = "this.is.a.string";
>>>> auto nameparts = splitter(name, '.').array;
>>>> auto name1 = joiner(nameparts[0 .. $-1], '.');
>>>>
>>>> got this:
>>>>
>>>> Error: template std.algorithm.joiner does not match any function
>>>> template declaration. Candidates are:
>>>> /usr/include/dmd/phobos/std/algorithm.d(2846):
>>>> std.algorithm.joiner(RoR, Separator)(RoR r, Separator sep) if
>>>> (isInputRange!RoR && isInputRange!(ElementType!RoR) &&
>>>> isForwardRange!Separator && is(ElementType!Separator :
>>>> ElementType!(ElementType!RoR)))
>>>>
>>>> Stuck here, thank you for any help.
>>>
>>> From your error message: isForwardRange!Separator
>>>
>>> Your separator is a character, which isn't a forward range. Try this:
>>> `auto name1 = joiner(nameparts[0 .. $-1], ".");`
More information about the Digitalmars-d-learn
mailing list