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

Adding splitAsList to String #113

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

BalmungSan
Copy link
Contributor

@BalmungSan BalmungSan commented Jan 11, 2022

I personally always call toList after a split since I don't like using Arrays for multiple reasons.

Additionally, this implementation should be way more efficient than the current approach. And, I personally believe that semantics are more useful.

Happy to receive any kind of feedback about the implementation and, especially, about the tests.

PS: Someone also mentioned that having a splitAsIterator could also be helpful, specially for potentially large Strings
Thoughts on that one?


Motivated by a personal desire for such method since a long time ago, plus some recent conversation in the discord channel.

@s5bug
Copy link

s5bug commented Jan 11, 2022

Re splitAsIterator: I was thinking of java.util.regex.Matcher usage. Could also be splitAsLazyList by wrapping the iterator. Something like

def splitAsIterator(pattern: String): Iterator[String] = {
  val pat = Pattern.compile(pattern)
  val mat = pat.matcher(str)

But I'm blanking on how to use mat.find to actually get it done.

@SethTisue
Copy link
Member

SethTisue commented Jan 11, 2022

I hope we can do something in this area, as I think it's sad that to have a common task like string-splitting require Array, given that we constantly tell language newcomers not to use Array unless they're doing Java interop or super high performance work.

@sjrd
Copy link
Member

sjrd commented Jan 11, 2022

If you call it split*, make sure it behaves exactly like String.split (i.e., Pattern.split) regarding empty matches, empty strings and empty substrings. The behavior is very touchy. You can look up the implementation (and the tests) in Scala.js:
https://github.com/scala-js/scala-js/blob/b0f1dc501edb15203176c636d35323c2bcc24550/javalib/src/main/scala/java/util/regex/Pattern.scala#L168-L220

@BalmungSan
Copy link
Contributor Author

Hi @sjrd sorry for the delay, I changed the implementation to return a List("") when the input string is empty and the preserveEmptySubStrings is set to true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants