0

I knew that "contains" will check whether a sequence contains ALL elements of another sequence.I want to know:

Is there any simple way to check whether a sequence contains any element of another sequence?

Update: I don't want to check whether A contains ALL elements of B,but any ONE element of B.

Alex Luya
  • 9,412
  • 15
  • 59
  • 91

2 Answers2

0

If you want to check whether some seq seq contains all the elements of another sequence test_seq, I'd use set_difference for that:

test_seq.setDifference(seq).isEmpty().not()

If seq is very large (like the result of mapping over a table), you can only do this efficiently with reduce:

seq.map(function(row) {
  return test_seq.setIntersection([row])
}).reduce(function(a, b) {
  return a.setUnion(b);
}).count().eq(test_seq.count())
mlucy
  • 5,249
  • 1
  • 17
  • 21
  • Thanks,I don't want to check whether A contains all elements of B,but whether contains any one element of B. – Alex Luya Aug 03 '15 at 03:43
  • In that case, the equivalent would be `test_seq.setIntersection(seq).isEmpty().not()` and `seq.map(function(row) { return test_seq.contains(row); }).reduce(function(a, b) { return a.or(b); })`. – mlucy Aug 03 '15 at 05:01
0

You can try do with difference.

For example you have table MyTable with filed MyArray that contains array of strings.

r.table("MyTable").get("my_id").do(
    function (record) {
        return record('MyArray').difference(["something1", "something2"]).count().ne(record('MyArray').count()
          )
    }
)

It will return true if field MyArray contains any element of ["something1", "something2"]. Otherwise - false