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
{{ message }}
Repository navigation
Simplify r.match() usage when dealing with arrays #2578
When filtering documents based on nested string arrays using r.match(), it become a bit hairy. It would be great if array.match(regexp) queries were automatically expanded to a filter function. With the current behavior you have to do something like this:
Part 2: This is a broader issue, but if empty arrays were considered falsy the example above would be further simplified to:
sequence.filter(r.row('tags').match('^h'))
And an interesting side effect is that the above example would now also work in scenarios where the underlying type is ambiguous. So you would no longer have to do crazy things like this:
Making match polymorphic on arrays might make sense, but it's ambiguous what it should mean. match can be used as a predicate, but it can also be used to extract information -- it might not be clear to people whether array.match means array.filter(match) or array.map(match). (Also, as an aside, you can use contains(...) with a function instead of .filter(...).count().gt(0).)
Scheduling this for subsequent and tagging as a ReQL proposal so we can talk about it when the current batch of changes is done.
I would have to think more about empty arrays being falsy, but my gut feeling is that it won't work well just because it's unexpected -- false should obviously be false, and null being false is really common (Ruby, CL, etc.), but having a bunch of different things convert to false is something only JS does, and it can be really confusing if you don't have all the rules in your head.
Providing static, curried versions of certain RQL terms has broader implications and could simplify a lot of queries (see the second part of #3485 for example). Furthermore, I'm pretty sure all such curried terms could be implemented in the drivers with no changes to server code.
One possible source of ambiguity is that in this case, the first argument to match (as in arg1.match(arg2)) is the one that's left undefined. So the currying is not applied from left to right.
Instead we might want to always leave the first argument arg1 open for terms that are usually applied in the form arg1.term(arg2, ...). This is something we have to think about a bit, and it will likely require some additional logic in the drivers.
Currying is hard to do in variadic languages like ReQL because you can't determine just by looking at a call to a variadic term whether or not it's waiting for more arguments. We could only curry non-variadic terms, but then people would be confused about what could be curried and what couldn't.
I like currying and I like variadic functions, but the only place I've seen anyone try to make them live together is in python's functools.partial. I think making some terms curried and others not would be really confusing
In #1190 there's a proposal of using place holders comparable to std::placeholders::_1 in C++. That would avoid the problem with variadic functions at the cost of being a bit more verbose.
@mlucy I didn't mean to conflate the terms "curried" and "partially applied". I only think partial application should be supported. So situations where the arity is unknown wouldn't be a problem.
Ah, I see! That has a different problem. A lot of our terms, in addition to taking a variadic number of arguments, can be called either infix or prefix. A good example is add; you can write r.expr(1).add(2) or r.add(1, 2) in JS.
In the Ruby driver, all terms can be called either infix or prefix (i.e. in Ruby r.foo(a, b) and r.expr(a).foo(b) are always equivalent). So writing r.contains(1, 2, 3) is indistinguishable from r.expr(1).contains(2, 3), and you have the variadic problem all over again.
Good point, I think in those cases (terms that have an arity range of 2, Infinity) when the static (infix) method is called with one argument we return a function that only accepts one additional argument. This seems like an acceptable compromise as it handles the common cases and still provides a lot of convenience.
r.expr([1]).map(r.add(1))// -> [2]r.expr([false]).map(r.or(true))// -> [true]r.expr([1]).map(r.add(1,2))// would not work
I don't know enough Ruby to provide much insight there, but it doesn't sound easily solvable.
Activity
mlucy commentedon Jun 20, 2014
Thanks for the suggestion @marshall007 .
Making
matchpolymorphic on arrays might make sense, but it's ambiguous what it should mean.matchcan be used as a predicate, but it can also be used to extract information -- it might not be clear to people whetherarray.matchmeansarray.filter(match)orarray.map(match). (Also, as an aside, you can usecontains(...)with a function instead of.filter(...).count().gt(0).)Scheduling this for subsequent and tagging as a ReQL proposal so we can talk about it when the current batch of changes is done.
I would have to think more about empty arrays being falsy, but my gut feeling is that it won't work well just because it's unexpected -- false should obviously be false, and null being false is really common (Ruby, CL, etc.), but having a bunch of different things convert to false is something only JS does, and it can be really confusing if you don't have all the rules in your head.
coffeemug commentedon Jun 23, 2014
See #2026 which is related. Note that I wanted
mapfunctionality and notfilterthere.marshall007 commentedon Jan 7, 2015
What if
r.match()could be partially applied?Providing static, curried versions of certain RQL terms has broader implications and could simplify a lot of queries (see the second part of #3485 for example). Furthermore, I'm pretty sure all such curried terms could be implemented in the drivers with no changes to server code.
danielmewes commentedon Jan 7, 2015
That's an interesting idea @marshall007 .
One possible source of ambiguity is that in this case, the first argument to
match(as inarg1.match(arg2)) is the one that's left undefined. So the currying is not applied from left to right.Instead we might want to always leave the first argument
arg1open for terms that are usually applied in the formarg1.term(arg2, ...). This is something we have to think about a bit, and it will likely require some additional logic in the drivers.danielmewes commentedon Jan 7, 2015
Related to #1190 - Allow for curried functions
mlucy commentedon Jan 9, 2015
Currying is hard to do in variadic languages like ReQL because you can't determine just by looking at a call to a variadic term whether or not it's waiting for more arguments. We could only curry non-variadic terms, but then people would be confused about what could be curried and what couldn't.
deontologician commentedon Jan 9, 2015
I like currying and I like variadic functions, but the only place I've seen anyone try to make them live together is in python's functools.partial. I think making some terms curried and others not would be really confusing
danielmewes commentedon Jan 9, 2015
In #1190 there's a proposal of using place holders comparable to
std::placeholders::_1in C++. That would avoid the problem with variadic functions at the cost of being a bit more verbose.marshall007 commentedon Jan 9, 2015
@mlucy I didn't mean to conflate the terms "curried" and "partially applied". I only think partial application should be supported. So situations where the arity is unknown wouldn't be a problem.
Simplified implementation example (can be run in data explorer):
mlucy commentedon Jan 9, 2015
Ah, I see! That has a different problem. A lot of our terms, in addition to taking a variadic number of arguments, can be called either infix or prefix. A good example is
add; you can writer.expr(1).add(2)orr.add(1, 2)in JS.In the Ruby driver, all terms can be called either infix or prefix (i.e. in Ruby
r.foo(a, b)andr.expr(a).foo(b)are always equivalent). So writingr.contains(1, 2, 3)is indistinguishable fromr.expr(1).contains(2, 3), and you have the variadic problem all over again.marshall007 commentedon Jan 9, 2015
Good point, I think in those cases (terms that have an arity range of
2, Infinity) when the static (infix) method is called with one argument we return a function that only accepts one additional argument. This seems like an acceptable compromise as it handles the common cases and still provides a lot of convenience.I don't know enough Ruby to provide much insight there, but it doesn't sound easily solvable.