You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
r.expr('hello, world and universe').tokenize(' ',',')// returns ['hello', 'world', 'and', 'universe']
The use case for this is as follows. If the new .group proposal passes review, it will make map/reduce functionality incredibly accessible and it's very much worth marketing and writing about. A common hello world map/reduce example is to count the frequency of words in documents, which requires tokenization. We could do it with javascript, but it would make for a much less friendly example. Hence, r.tokenize would be extremely nice to have.
I was going to say split originally, but thought it might be too general and we might want to use it elsewhere. But if other languages use it, I'm cool with split.
All of these languages take a string with multiple separators rather than being variadic which we should mimic. i.e. split(' ,') not split(' ', ',').
split() uses whitespace characters as its separator.
All of these functions in host languages take a length limit as a second argument although it does slightly different things. I'm not sure I totally understand where that's useful but maybe we should just include it because it's easy and if it's in all 3 of them chances are it's useful for something.
I think it's a good idea to add the inverse of this function join (might want to change that name though) as well. It's incredibly easy because boost has a builtin join method and I could see someone storing their data in a split form because that's convenient for processing but wanting to join the strings back together at some point too. Also string.split().join(' ') is a nice way to strip out tabs and newlines.
I actually have the word count example @coffeemug mentioned in the map reduce blog post I'm working on. At first I had to use JS but with the new .match the example has become much better. This would make it better still.
@jdoliner: that isn't how split works in Ruby. In Ruby if you write .split(", ") it means "split on the string ', '", not "split on either the string ',' or ' '". Instead, split takes a regexp to handle that case:
irb(main):3898:0> "a b, c,d".split(", ")["a b","c,d"]irb(main):3899:0> "a b, c,d".split(/[, ]/)["a","b","","c","d"]
Slava's proposed behavior seems more intuitive to me. Here's what I would propose:
split() does a normal whitespace split
split(', ') means "split on the string ', '"
split(',', ' ') means "split on either the string ',' or ' '"
We skip the length limit, it doesn't seem terribly useful to me.
I'm cool with that split behavior. I actually think that's what python does
and I just misread it. I'd really like to have the length limit since it's
present in all of the languages we target and there really doesn't seem to
be a high cost to it.
irb(main):3898:0> "a b, c,d".split(", ")["a b", "c,d"]irb(main):3899:0> "a b, c,d".split(/[, ]/)["a", "b", "", "c", "d"]
Slava's proposed behavior seems more intuitive to me. Here's what I would
propose:
split() does a normal whitespace split
split(', ') means "split on the string ', '"
split(',', ' ') means "split on either the string ',' or ' '"
We skip the length limit, it doesn't seem terribly useful to me.
We add .join.
—
Reply to this email directly or view it on GitHubhttps://github.com//issues/1099#issuecomment-20322687
.
The only cost is that it makes argument parsing a littler hard, because we have a variable number of strings followed by an optional number, but that's probably fine.
A recent time I needed a limit to a split is when I had previously determined the position of a certain string and needed to split everything leading up to it and then the next section(s) would use different split parameters.
@markenzie I think the limit here is about the number of element returned, not until which character split should work (at least that's what the limit is in Python and JavaScript).
To split until a certain character, I would do something like
string.slice(0, <limit>).split()
Note: It looks like we don't such a slice function for string...
I don't expect split to be variadic. If people want to split with comma and string, they should use a regex, that's what they are for.
The limit (the number of elements returned) is useful. Suppose I want to create a slug according to the first words of a text. I can do
text.split(" ").limit(3).join()
===== vs =====
text.split(" ", 3).join()
I would naturally prefer the second syntax for this problem for performance reason.
In python and other languages, the numeric argument to split is the number of splits to do. The last element of the returned array is the remainder of the strings.
Making split take both regular expressions and strings would be difficult for us because we don't have a distinct regex type; we just have one function that accepts a string and interprets it as a regex. (On the other hand, I suppose we could just make split always take a regex.)
I disagree with @neumino. I think as a general rule, we should distribute over sequences unless there is a compelling reason not to for the specific operation. You learn about it once and it's immensely useful.
For example if downcase could be distributed over sequences, you could write lambda x: x['text'].downcase().split() and lambda x: x['text'].split().downcase(), which I think is really nice.
Splitting an array in 2 is something that is defined (not in ReQL, but in general).
So having split to recurse in streams and split each element if they are a string is confusing..
Can someone point out an example where splitting on a stream is useful? All the examples I have in my mind use a group/map after, in which case users can split inside the group/map.
@coffeemug -- the rule right now is basically "operations on objects distribute over streams". We don't distribute any other string operations over streams (although maybe we should).
@coffeemug -- I think @jdoliner's point was that if you split on whitespace, "word," and "word" would be counted as different words. If we want to fix that, we could add splitting by regular expressions, which wouldn't be insanely difficult.
the rule right now is basically "operations on objects distribute over streams"
Fair enough. I changed my mind, I'm now not sure whether automatically distributing string operations over sequences is the right thing to do, so I think we should make that a separate proposal (if someone feels strongly enough about doing that).
Activity
mlucy commentedon Jun 30, 2013
Could we call this
split? That's what Ruby and Python call it, I think.coffeemug commentedon Jun 30, 2013
I was going to say
splitoriginally, but thought it might be too general and we might want to use it elsewhere. But if other languages use it, I'm cool withsplit.neumino commentedon Jun 30, 2013
JavaScript uses
splittoo.jdoliner commentedon Jun 30, 2013
split(' ,')notsplit(' ', ',').split()uses whitespace characters as its separator.join(might want to change that name though) as well. It's incredibly easy because boost has a builtin join method and I could see someone storing their data in a split form because that's convenient for processing but wanting to join the strings back together at some point too. Alsostring.split().join(' ')is a nice way to strip out tabs and newlines.neumino commentedon Jun 30, 2013
👍 for @jdoliner's four bullet points.
coffeemug commentedon Jul 1, 2013
I'm cool with @jdoliner's API.
wmrowan commentedon Jul 1, 2013
I actually have the word count example @coffeemug mentioned in the map reduce blog post I'm working on. At first I had to use JS but with the new
.matchthe example has become much better. This would make it better still.mlucy commentedon Jul 2, 2013
@jdoliner: that isn't how
splitworks in Ruby. In Ruby if you write.split(", ")it means "split on the string ', '", not "split on either the string ',' or ' '". Instead,splittakes a regexp to handle that case:Slava's proposed behavior seems more intuitive to me. Here's what I would propose:
split()does a normal whitespace splitsplit(', ')means "split on the string ', '"split(',', ' ')means "split on either the string ',' or ' '".join.jdoliner commentedon Jul 2, 2013
I'm cool with that split behavior. I actually think that's what python does
and I just misread it. I'd really like to have the length limit since it's
present in all of the languages we target and there really doesn't seem to
be a high cost to it.
On Mon, Jul 1, 2013 at 7:16 PM, Michael Lucy [email protected]:
mlucy commentedon Jul 2, 2013
The only cost is that it makes argument parsing a littler hard, because we have a variable number of strings followed by an optional number, but that's probably fine.
markenzie commentedon Jul 2, 2013
A recent time I needed a limit to a split is when I had previously determined the position of a certain string and needed to split everything leading up to it and then the next section(s) would use different split parameters.
neumino commentedon Jul 2, 2013
@markenzie I think the limit here is about the number of element returned, not until which character
splitshould work (at least that's what the limit is in Python and JavaScript).To split until a certain character, I would do something like
Note: It looks like we don't such a slice function for string...
I don't expect
splitto be variadic. If people want to split with comma and string, they should use a regex, that's what they are for.The limit (the number of elements returned) is useful. Suppose I want to create a slug according to the first words of a text. I can do
I would naturally prefer the second syntax for this problem for performance reason.
+1 for
.joinAtnNn commentedon Jul 2, 2013
In python and other languages, the numeric argument to split is the number of splits to do. The last element of the returned array is the remainder of the strings.
It is not the same as doing .split.limit
mlucy commentedon Jul 3, 2013
Making split take both regular expressions and strings would be difficult for us because we don't have a distinct regex type; we just have one function that accepts a string and interprets it as a regex. (On the other hand, I suppose we could just make
splitalways take a regex.)jdoliner commentedon Jul 3, 2013
Or just have a
regexoptarg.29 remaining items
neumino commentedon Dec 17, 2013
I would rather not have
splitbeing polymorphic on stream. It just makes the API more confusing.coffeemug commentedon Dec 17, 2013
Ah, I see.
I disagree with @neumino. I think as a general rule, we should distribute over sequences unless there is a compelling reason not to for the specific operation. You learn about it once and it's immensely useful.
For example if
downcasecould be distributed over sequences, you could writelambda x: x['text'].downcase().split()andlambda x: x['text'].split().downcase(), which I think is really nice.neumino commentedon Dec 17, 2013
Splitting an array in 2 is something that is defined (not in ReQL, but in general).
So having
splitto recurse in streams and split each element if they are a string is confusing..Can someone point out an example where splitting on a stream is useful? All the examples I have in my mind use a
group/mapafter, in which case users can split inside thegroup/map.Also here's an example that is confusing
is not the same as
mlucy commentedon Dec 17, 2013
@coffeemug -- the rule right now is basically "operations on objects distribute over streams". We don't distribute any other string operations over streams (although maybe we should).
neumino commentedon Dec 17, 2013
Also about the argument
I think it's not a valid argument because it still delivers a bad experience.
.
I recently saw someone trying to do
And was stuck with
Because
not()doesn't recurse in a stream.mlucy commentedon Dec 17, 2013
@coffeemug -- I think @jdoliner's point was that if you split on whitespace, "word," and "word" would be counted as different words. If we want to fix that, we could add splitting by regular expressions, which wouldn't be insanely difficult.
jdoliner commentedon Dec 17, 2013
@mlucy I think that's really due to the fact that 90% of streams are objects because that's the only thing you can store in a table.
coffeemug commentedon Dec 17, 2013
@neumino -- ok, that's a good point.
Fair enough. I changed my mind, I'm now not sure whether automatically distributing string operations over sequences is the right thing to do, so I think we should make that a separate proposal (if someone feels strongly enough about doing that).
mlucy commentedon Jan 17, 2014
I think we should go with the original plan and just mimic Python's
split. Any objections?AtnNn commentedon Jan 17, 2014
+1for mimicking Python.Haskell's overcomplicated split library is an example of what happens when you try to write a split function that works for every use case.
coffeemug commentedon Jan 17, 2014
No objections on my part.
mlucy commentedon Feb 21, 2014
This is back up in review 1240 by @AtnNn .
mlucy commentedon Feb 22, 2014
This is in next.