Skip to content

Consider string-array polymorphism #264

Closed

Description

@coffeemug

If we treat strings as arrays of characters, every operation that works on an array can work on a string and vice versa. This sounds like it's worth doing both because of pragmatism and beauty.

Activity

  1. coffeemug commented on May 22, 2013

    @coffeemug
    ContributorAuthor

    The only operation people have requested so far is slice on strings. We can do that, or we can make everything that works on arrays also work on strings. We should discuss what we can get done in our timeframe for 1.6.

  2. ghost assigned on May 22, 2013
  3. srh commented on May 24, 2013

    @srh
    Contributor

    Note that strings are unicode, and we represent them internally, with UTF-8. At least I hope so. We do so not very wittingly. Implementers should be aware of such things, and API designers should realize that indexing a string is O(n) if the representation internally is UTF-8.

  4. coffeemug commented on May 28, 2013

    @coffeemug
    ContributorAuthor

    This isn't making it into 1.6, partially because 1.6 already has a lot of stuff, and partially because this is ridden with edge cases. We'll come back to this later if it becomes necessary.

  5. neumino commented on Jul 2, 2013

    @neumino
    Member

    Bump.

    .slice would be useful on strings (more than all others methods on array I think)

  6. coffeemug commented on Jul 2, 2013

    @coffeemug
    ContributorAuthor

    So, if we ever want to get this issue done, we should probably list specific array functions we also want to work on strings, otherwise this issue is too general. This is a fairly low priority now and I have lots to do so I don't want to spend time on this yet, but if anyone wants to take a crack at it, it would help move this along I think.

  7. mlucy commented on Jul 3, 2013

    @mlucy
    Member

    Also, note that anyone who really wants to do this can get by with r.match.

    str.slice(a, b)
    str.match('.{a}(.{b})')[:groups][0][:str]
  8. nergdron commented on Jan 31, 2014

    @nergdron

    Per @neumino, bumping this so people can remember to implement it. :)

  9. neumino commented on Jan 31, 2014

    @neumino
    Member

    What is useful, is count on a string.
    I wouldn't prefer calling it length though.

  10. mlucy commented on Jul 4, 2014

    @mlucy
    Member

    This has been languishing for a year, and is less problematic now because .split("") can be used to separate a string into an array of characters. Removing the pr:high tag.

  11. thelinuxlich commented on Dec 11, 2014

    @thelinuxlich

    +1 for count on string

  12. danielmewes commented on Jan 20, 2015

    @danielmewes
    Member

    I think we should implement .count for strings. It should count the number of Unicode characters, and not the number of bytes. Should be pretty easy now that @gchpaco has implemented UTF-8 support for split.
    count would be O(n) on strings, but I think that's fine.

    Similarly I think slice would be useful, and it should be UTF-8 aware.

  13. gchpaco commented on Jan 21, 2015

    @gchpaco
    Contributor

    I have no issue with this provided we define it as counting codepoints. (characters is sort of fuzzy semantically.)

  14. danielmewes commented on Jan 21, 2015

    @danielmewes
    Member

    Ah yeah, code points sound reasonable.

  15. neumino commented on Feb 4, 2015

    @neumino
    Member

    Any chance to schedule that for 2.0?

    Especially count/length and slice (to extract snippets). It's kind of frustrating when people run into that.

  16. modified the milestones: subsequent, backlog on Feb 4, 2015
  17. coffeemug commented on Feb 4, 2015

    @coffeemug
    ContributorAuthor

    I think we should open specific issues for the commands that we want to make polymorphic on strings arrays (the two that always come up are count and slice). I don't think it's worth keeping the general issue around.

  18. marshall007 commented on Feb 5, 2015

    @marshall007
    Contributor

    match is another one, see #2578. The specific issue for count #1146 was closed as a dupe of this so that should be reopened if this is to be closed.

  19. danielmewes commented on May 13, 2015

    @danielmewes
    Member

    Sorry this was pushed back for a while.
    I opened #4227 and #4228 for slice and count.

  20. danielmewes commented on May 13, 2015

    @danielmewes
    Member

    Closing this one instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions