Skip to content

Simplify r.match() usage when dealing with arrays #2578

Open

Description

@marshall007

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:

r.expr([
  { tags: [ 'hello', 'world' ] },
  { tags: [ 'world' ] }
])
.filter(
  r.row('tags').filter(function (tag) {
    return tag.match('^h');
  }).count().gt(0)
)

If the query were automatically expanded, you could get away with simply:

sequence.filter(
  r.row('tags').match('^h').count().gt(0)
)

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:

r.expr([
  { tags: 'hello' },
  { tags: 'world' },
  { tags: [ 'hello', 'world' ] }
])
.filter(r.branch(
  r.row('tags').typeOf().eq('ARRAY'),
  r.row('tags').filter(function (tag) {
    return tag.match('^h');
  }).count().gt(0),
  r.row('tags').match('^h')
))

Activity

  1. added this to the subsequent milestone on Jun 20, 2014
  2. mlucy commented on Jun 20, 2014

    @mlucy
    Member

    Thanks for the suggestion @marshall007 .

    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.

  3. coffeemug commented on Jun 23, 2014

    @coffeemug
    Contributor

    See #2026 which is related. Note that I wanted map functionality and not filter there.

  4. marshall007 commented on Jan 7, 2015

    @marshall007
    ContributorAuthor

    What if r.match() could be partially applied?

    r.row('tags').contains(r.match('^h'))
    r.row('tags').map(r.match('^h'))

    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.

  5. danielmewes commented on Jan 7, 2015

    @danielmewes
    Member

    That's an interesting idea @marshall007 .

    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.

  6. danielmewes commented on Jan 7, 2015

    @danielmewes
    Member

    Related to #1190 - Allow for curried functions

  7. mlucy commented on Jan 9, 2015

    @mlucy
    Member

    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.

  8. deontologician commented on Jan 9, 2015

    @deontologician
    Contributor

    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

  9. danielmewes commented on Jan 9, 2015

    @danielmewes
    Member

    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.

  10. marshall007 commented on Jan 9, 2015

    @marshall007
    ContributorAuthor

    @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.

    .filter(r.contains(1)(2)(3)) // curried
    .filter(r.contains(1, 2, 3)) // partially applied

    Simplified implementation example (can be run in data explorer):

    r.contains = function () {
      var args = arguments;
      return function (row) {
        return row.contains.apply(row, args);
      }
    };
    
    r.expr([[1], [2]]).filter(r.contains(1)) // -> [ [1] ]
  11. mlucy commented on Jan 9, 2015

    @mlucy
    Member

    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.

  12. marshall007 commented on Jan 9, 2015

    @marshall007
    ContributorAuthor

    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.

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

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions