Jump to page: 1 2
Thread overview
How to extend the string class to return this inside the square bracket?
Aug 13, 2021
Marcone
Aug 13, 2021
user1234
Aug 13, 2021
Marcone
Aug 13, 2021
Marcone
Aug 13, 2021
jfondren
Aug 13, 2021
Ali Çehreli
Aug 13, 2021
Marcone
Aug 13, 2021
Marcone
Aug 13, 2021
jfondren
Aug 13, 2021
Paul Backus
Aug 14, 2021
user1234
Aug 14, 2021
Marcone
Aug 13, 2021
Ali Çehreli
Aug 14, 2021
H. S. Teoh
August 13, 2021

How to extend the string class to return this inside the square bracket the same way opDollar $ returns the length of the string? Thank you.

import std;

void main(){
	writeln("Hello World!"[0..this.indexOf("o")]);
}
August 13, 2021

On Friday, 13 August 2021 at 21:05:22 UTC, Marcone wrote:

>

How to extend the string class to return this inside the square bracket the same way opDollar $ returns the length of the string? Thank you.

import std;

void main(){
	writeln("Hello World!"[0..this.indexOf("o")]);
}

this does not exist (and see few reason for) but algo + ufcs allows this easily, e.g

"Hello World!".findSplit("o")[0].writeln;

bonus: both can throw bound error

August 13, 2021

On Friday, 13 August 2021 at 21:14:29 UTC, user1234 wrote:

>

On Friday, 13 August 2021 at 21:05:22 UTC, Marcone wrote:

>

How to extend the string class to return this inside the square bracket the same way opDollar $ returns the length of the string? Thank you.

import std;

void main(){
	writeln("Hello World!"[0..this.indexOf("o")]);
}

this does not exist (and see few reason for) but algo + ufcs allows this easily, e.g

"Hello World!".findSplit("o")[0].writeln;

bonus: both can throw bound error

My example was just an example. I don't want this solution. I want to have the power to handle the string inside the square brackets.

August 13, 2021

On 8/13/21 5:05 PM, Marcone wrote:

>

How to extend the string class to return this inside the square bracket the same way opDollar $ returns the length of the string? Thank you.

    import std;

    void main(){
        writeln("Hello World!"[0..this.indexOf("o")]);
    }

There is no string class to extend.

$ is a special token the compiler changes to arr.length for arrays.

-Steve

August 13, 2021

On Friday, 13 August 2021 at 21:47:22 UTC, Steven Schveighoffer wrote:

>

On 8/13/21 5:05 PM, Marcone wrote:

>

How to extend the string class to return this inside the square bracket the same way opDollar $ returns the length of the string? Thank you.

    import std;

    void main(){
        writeln("Hello World!"[0..this.indexOf("o")]);
    }

There is no string class to extend.

$ is a special token the compiler changes to arr.length for arrays.

-Steve

Isn't there some unario operator template that I can use with lambda to handle a string literal?

August 13, 2021

On Friday, 13 August 2021 at 22:09:59 UTC, Marcone wrote:

>

Isn't there some unario operator template that I can use with lambda to handle a string literal?

So, something other than an exact "lit"[0..this.xx(..)] syntax is fine?

What didn't you like about "Hello World!".findSplit("o")[0].writeln; then?

What is a real example of something you want to do?

August 13, 2021
On 8/13/21 4:08 PM, jfondren wrote:
> On Friday, 13 August 2021 at 22:09:59 UTC, Marcone wrote:
>>
>> Isn't there some unario operator template that I can use with lambda to handle a string literal?
> 
> So, something other than an exact "lit"[0..this.xx(..)] syntax is fine?
> 
> What didn't you like about `"Hello World!".findSplit("o")[0].writeln;` then?
> 
> What is a real example of something you want to do?

And I started writing the following but stopped because the semantics are not clear. I first called it 'between' but then should the 'o' that was searched be a part of the output?

Should "from 'o' to 'o'" produce an empty string, should it include a single 'o' or should it go all the way to the next 'o'?

What about the last line which says "from 'd' to 'a'"? Is that an entirely empty range or just 'd' or 'd' till the end?

I don't think the programming language can decide one way or the other.

import std.algorithm;
import std.range;
import std.stdio;

auto inclusive(R, E)(R range, E fromNeedle, E toNeedle) {
  auto found = range.find(fromNeedle);
  return chain(found.front.only, found.drop(1).findSplitAfter(only(toNeedle))[0]);
}

void main() {
  const s = "Hello World!";
  auto r = s.inclusive('o', 'o');
  writeln(r);

  writeln("abcdef".inclusive('d', 'a'));
}

Ali
August 13, 2021

On Friday, 13 August 2021 at 23:08:07 UTC, jfondren wrote:

>

On Friday, 13 August 2021 at 22:09:59 UTC, Marcone wrote:

>

Isn't there some unario operator template that I can use with lambda to handle a string literal?

So, something other than an exact "lit"[0..this.xx(..)] syntax is fine?

What didn't you like about "Hello World!".findSplit("o")[0].writeln; then?

What is a real example of something you want to do?

writeln("Hello World!"[x.indexOf("e")..x.indexOf("r")]);

indexOf()is just a simple example, not the goal. I want handle literal inside [] like it bellow, but in literal:

string x = "Hello World!";
writeln(x[x.indexOf("e")..x.indexOf("r")]);

August 13, 2021

On Friday, 13 August 2021 at 23:23:55 UTC, Marcone wrote:

>

On Friday, 13 August 2021 at 23:08:07 UTC, jfondren wrote:

>

On Friday, 13 August 2021 at 22:09:59 UTC, Marcone wrote:

>

Isn't there some unario operator template that I can use with lambda to handle a string literal?

So, something other than an exact "lit"[0..this.xx(..)] syntax is fine?

What didn't you like about "Hello World!".findSplit("o")[0].writeln; then?

What is a real example of something you want to do?

writeln("Hello World!"[x.indexOf("e")..x.indexOf("r")]);

indexOf()is just a simple example, not the goal. I want handle literal inside [] like it bellow, but in literal:

string x = "Hello World!";
writeln(x[x.indexOf("e")..x.indexOf("r")]);

unittest {
    import std.functional : pipe;
    import std.string : indexOf;

    assert("Hello, world!".pipe!(x => x[x.indexOf("e") .. x.indexOf("r")])
            == "ello, wo");
}
August 13, 2021

On Friday, 13 August 2021 at 23:23:55 UTC, Marcone wrote:

>

writeln("Hello World!"[x.indexOf("e")..x.indexOf("r")]);

indexOf()is just a simple example, not the goal. I want handle literal inside [] like it bellow, but in literal:

string x = "Hello World!";
writeln(x[x.indexOf("e")..x.indexOf("r")]);

You can use the pipe function to bind an arbitrary expression to a variable:

import std.functional: pipe;
import std.algorithm: countUntil;
import std.stdio: writeln;

"Hello world!"
    .pipe!(s => s[s.countUntil('e') .. s.countUntil('r')])
    .writeln;
« First   ‹ Prev
1 2