D
Deleted member 43669
Guest
Simple question, really. If you were to design a language's standard library, what would you choose and why.
while
function() {
string.split -> iteration or recursion over all bytes in string
}
end while
if string.split
while
function() {
string.split -> iteration or recursion over all bytes in string
}
end while
end if
Given your previous thread I decided to check what Python does in this situation.koala":kr5bgesb said:Simple question, really. If you were to design a language's standard library, what would you choose and why.
>>> ''.split()
[]
>>> ''.split('a') # or any other character
['']
ShuggyCoUk":1lht0dfc said:A zero length string is not null.
Splitting a string which does not contain the separator returns a single member sequence whose sole element is the value of the original string, thus the empty string should be no different.
koala":3ay5dr1b said:Putting in my vote to my own question. It's [] for me.
Yeah, from a "purity" standpoint 'a'.split(',') == ['a'], then ''.split(',') == [''] makes sense. The duality with join looks correct, but...
Write code that turns "1,2,3,4,5" into [1,2,3,4,5]. Realize that "" should be [] and note that ''.split(',') makes the code much nicer. Or write code that counts the number of words in a line. Or... I can't really think of use cases where ''.split(x) being [''] is useful... counterpoint?
Solomonoff's Secret":336fk0sw said:Clearly "".split("blah") should be "file_not_found".
Seriously though, does "abc".split("c") = {"ab", ""} or "ab"? If the former, the array includes empty regions at boundaries so it would be consistent for "".split("c") to be {""}. If the latter, it would be more consistent for "".split("c") to be {}. Anything else (such as null) would be an abomination.
Splitting a string which does not contain the separator returns a single member sequence whose sole element is the value of the original string, thus the empty string should be no different.
In Python, "abc".split('c') returns ['ab','']. Which is maybe weird, but I think there's a realistic use case: parsing a string of delimited values, you want empty strings for missing values instead of just tossing them. That is, if my file look like this (four columns, comma-separated):Well, you crazy ['']-ers, what of it? Should it be ['ab',''] or ['ab']? Testify! I bet you all agree that ['ab'] is more useful.
"".Split("")
Alamout":30vm3myi said:In Python, "abc".split('c') returns ['ab','']. Which is maybe weird, but I think there's a realistic use case: parsing a string of delimited values, you want empty strings for missing values instead of just tossing them. That is, if my file look like this (four columns, comma-separated):Well, you crazy ['']-ers, what of it? Should it be ['ab',''] or ['ab']? Testify! I bet you all agree that ['ab'] is more useful.
A,B,C,D
E,F,G,
H,I,J,K
I want to parse the middle row as ['E','F','G',''] because I want the rows to have the same number of elements.
,,,
,a,,b
I disagree - ['ab'] is NOT more useful.masonk":3k2jqyfd said:I like what this man is preaching.
Well, you crazy ['']-ers, what of it? Should it be ['ab',''] or ['ab']? Testify! I bet you all agree that ['ab'] is more useful.
Dracorat":27b7m0y5 said:I disagree - ['ab'] is NOT more useful.masonk":27b7m0y5 said:I like what this man is preaching.
Well, you crazy ['']-ers, what of it? Should it be ['ab',''] or ['ab']? Testify! I bet you all agree that ['ab'] is more useful.
If this were to output ['ab',''] (as most languages I've used do) then I know that
Count(ResultantArrayLength) = NumberOfMatchesInOriginalString + 1;
If it does what you propose then you never know if it matched or if it just started with something empty.
split , '' == [] (1)
split , 'a' == ['a']
split , 'a,' == ['a', '']
split , 'a,,' == ['a', '', '']
split , ',a' == ['', 'a']
split , ',' == ['', '']
let split _ "" = []
...
for(String str : new String[] { "", "a", "a,", "a,,", ",", ",a"})
System.out.println('"' + str + '"' + " -> " + Arrays.toString(str.split(",")));
"" -> []
"a" -> [a]
"a," -> [a]
"a,," -> [a]
"," -> []
",a" -> [, a]
for(String str : new String[] { "", "a", "a,", "a,,", ",", ",a"})
System.out.println('"' + str + '"' + " -> " + Arrays.toString(str.split(",", -1)));
"" -> []
"a" -> [a]
"a," -> [a, ]
"a,," -> [a, , ]
"," -> [, ]
",a" -> [, a]
for str in split(src)
// do something with str without coding for corner cases
It sounds like we are saying the same thing. Hi-five.fitten":7deqft4p said:Dracorat":7deqft4p said:I disagree - ['ab'] is NOT more useful.masonk":7deqft4p said:I like what this man is preaching.
Well, you crazy ['']-ers, what of it? Should it be ['ab',''] or ['ab']? Testify! I bet you all agree that ['ab'] is more useful.
If this were to output ['ab',''] (as most languages I've used do) then I know that
Count(ResultantArrayLength) = NumberOfMatchesInOriginalString + 1;
If it does what you propose then you never know if it matched or if it just started with something empty.
['ab'] is not more useful with that behavior. Due to the ambiguity of the "don't keep empty strings" behavior, ['ab'] cannot reproduce the original string. Any string of any number of "c" followed by "ab" followed by any number of "c" will match ['ab'] as output ("ccab", "abcc", "ccabcc", etc). Data/information is lost in that transformation. The other way where it yields ['ab',''] can faithfully be used to reconstruct the original. No data/information is lost.
Dracorat":1odtfg88 said:It sounds like we are saying the same thing. Hi-five.fitten":1odtfg88 said:Dracorat":1odtfg88 said:I disagree - ['ab'] is NOT more useful.masonk":1odtfg88 said:I like what this man is preaching.
Well, you crazy ['']-ers, what of it? Should it be ['ab',''] or ['ab']? Testify! I bet you all agree that ['ab'] is more useful.
If this were to output ['ab',''] (as most languages I've used do) then I know that
Count(ResultantArrayLength) = NumberOfMatchesInOriginalString + 1;
If it does what you propose then you never know if it matched or if it just started with something empty.
['ab'] is not more useful with that behavior. Due to the ambiguity of the "don't keep empty strings" behavior, ['ab'] cannot reproduce the original string. Any string of any number of "c" followed by "ab" followed by any number of "c" will match ['ab'] as output ("ccab", "abcc", "ccabcc", etc). Data/information is lost in that transformation. The other way where it yields ['ab',''] can faithfully be used to reconstruct the original. No data/information is lost.
koala":3fqo07bo said:In which situation does Python split work out without needing to account for corner cases?
Especially since it's not correct for many languages. "Words" are not always guaranteed to be "groups of word characters separated by whitespace or punctuation.""corner case" is subjective. I would say that the need to split to count words is a corner case.
Bengie25":1o221av7 said:1) The idea of a zero length array is ludicrous, other than weird structure tricks. But really, how do you have a pointer to a memory location that stores no data? You can't.
I mean, I don't think I've ever actually written this before, but (based on koala's pseudo-code):Dracorat":ek2dut8u said:if split('a', 'b') = {'a'} is useful to you, then I fail to see what logic by which split('', 'b') = {} (instead of {''}) is useful.
Edit: Actually - can someone post some code by which they think that ={} gives them some advantage?
# word counter takes a list of strings and counts the words (where a "word" is any number of non-whitespace characters)
def word_counter(list_of_lines):
counter = defaultdict(int)
for line in list_of_lines:
for word in line.split():
counter[word] += 1
return counter
# word counter takes a list of strings and counts the words (where a "word" is any number of non-whitespace characters)
def word_counter(list_of_lines):
counter = defaultdict(int)
for line in list_of_lines:
for word in line.split():
if (word.length > 0)
counter[word] += 1
return counter
...Okay. I obviously wasn't trying to write a real word-counting function. I was just giving an example in which split returning an empty list on whitespace obviates the need for a conditional. Yeah, the change fitten made is very simple--that's because it was a very simple function.So I guess in short, the example was a noble attempt but even as contrived as it was, I'm still not moved by it.
But most people without I18N experience will start with "split on whitespace" as word-counting, so it's a good discussion to have.Okay. I obviously wasn't trying to write a real word-counting function.
Alamout":345vi2vp said:I have no idea if or when I have taken advantage of this behavior in real code, and I don't care nearly enough to actually look for it just to "move you".![]()