Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

String.last, first #9314

Open
alan-knight opened this issue Mar 20, 2013 · 5 comments
Open

String.last, first #9314

alan-knight opened this issue Mar 20, 2013 · 5 comments
Labels
area-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. core-n library-core P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug

Comments

@alan-knight
Copy link
Contributor

For the same reasons it's useful to have Iterable.last it would be nice to have String.last. And for symmetry, String.first.

@lrhn
Copy link
Member

lrhn commented Mar 21, 2013

Should that return a string containing the last code point or the last rune?

If it is similar to Iterable/List, it should return string[string.length - 1] .
That would be just the code-point, not the rune, so it's likely to not be very interesting. I'll assume you want the last rune as a string.

Anything based on runes should require you to go through the runes getter.
You can do:

(string.runes..reset(string.length)..movePrevious()).currentAsString

for the last rune, but I can see that isn't very succinct.
The first one is shorter:

(string.runes..moveNext()).currentAsString

Why is string.first and string.last important?
If you just want to check if the string ends with something, it's better to do

string.endsWith(">")

than

string.last == ">"

@floitschG
Copy link
Contributor

floitschG commented Mar 22, 2013

There is also new String.fromCharCode(string.runes.last)

@alan-knight
Copy link
Contributor Author

Returning something based on the rune would be inconsistent with the index operation, which would be bad. I think it should just return string[string.length - 1] as you initially suggest. Which might only be part of a character, but you managed to convince me earlier that that was fine in most circumstances. And if it isn't, then you should use runes.

endsWith(">") is useful for the case that prompted me to file this, so that's handy. But I didn't know about it, so I suspect users may not always find it.

The other suggestions seem so verbose as to be entirely unusable, other than in the implementation of one's own helper functions to do these things.

In general, it would be useful to operate on strings like lists of characters. This can be done via string.split("") but that's rather obscure and probably even less efficient than most of the alternatives. So, for example, I've been working on numeric formatting code. This wants to parse a formatting specification, e.g. "##.##­0E00". This iterates through the string and has a bunch of case statement clauses
        switch (ch) {
          case _PATTERN_DIGIT: <stuff>
          case _PATTERN_ZERO_DIGIT: <stuff>
          case _PATTERN_GROUPING_SEPARATOR: <stuff>
etc.

The original version maintains its own index and moves it around in the string. An iterator would be much more pleasant. I can get one via split("") or I can just write my own in a few lines, but this seems generally useful and omitting it will just mean there are N different buggy versions of it. I could iterate over code units or runes and define the constants as numbers rather than strings, but that would make the code even more painful to debug.

@lrhn
Copy link
Member

lrhn commented Mar 27, 2013

It seems like a large and unnecessary overhead to create new one-char strings for each character, when its numerical value is equivalent. Ofcourse that is based on the understanding that strings are expensive (relatively) and small integers are cheap.

Why do you use strings of length 1 instead of code units?
Is it easier to read/write? If so, would character code constants change that?
(e.g., instead of (ch == "a") you would write something like (c == #"a")).

@alan-knight
Copy link
Contributor Author

Character constants would make it shorter to write some kinds of code that worked with code units. It wouldn't help in my case, where what I'm comparing to are long symbolic constant names. And character constants don't help with debugging comprehensibility. It's much easier for me to see that it's looking for ';' and finding '0' than it is to see that it's looking for 59 and found 48.

It's clearly less efficient to allocate the additional strings, but it's a great deal more convenient to work with strings rather than integers. Are we planning to remove the large and unnecessary expense of the [] operator for strings?

If people don't have an iterator, it's not that they'll fall back to using integers. They'll fall back to using a for loop with an index. Or using split(""). Or writing their own iterator. So it's not a matter of whether people will incur the expense of creating strings. They will. It's a matter of whether we provide reasonable operations, or end with worse user code and multiple private implementations of these operations.

@alan-knight alan-knight added Type-Enhancement area-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-core labels Mar 27, 2013
@kevmoo kevmoo added P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug and removed triaged labels Feb 29, 2016
@lrhn lrhn added the core-m label Aug 11, 2017
@floitschG floitschG added core-n and removed core-m labels Aug 29, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. core-n library-core P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

4 participants