Closed felixhao28 closed 6 years ago
I'm closing it because I didn't have time to think about it for a year. If you'd like to add this functionality to ObjectPath, create PR.
I have created a completely new DSL to solve my problem and then moved on from the previous project. Now I don't have the incentive anymore.
Currently we have
+
for union operation to combine two or more collections together. This is very helpful in the way you can split your desired results into sub-sections and write different query paths for each. In addition to+
, in certain use cases, more collection operators will prove their usefulness (borrowing from set theory).I am proposing 2 new collection operators: intersection and difference. These operations require operands to have non duplicate elements. So for these operations, there will be two modes: by-ref and by-value.
A
andB
are by-ref equal if they have the excact same path.A
andB
are by-value equal if they have the same primitive value or all fields of them are by-value equal.An array can be a set as long as there are no two elements equal to each other. If otherwise, duplicates are removed before performing any of the set operations.
Intersection
^
(by-ref) and.^
(by-value)A ^ B
is an intersection ofA
andA
iff every element inA ^ B
is contained by bothA
andB
.$.poems.author .^ $.novels.author
$.poems.author
=>Pushkin, Pushkin, Pushkin, Tagore, Tagore, Tagore
$.novels.author
=>Tagore, Tagore, Victor Hugo
$.poems.author
=>Pushkin, Tagore
$.poems.author .^ $.novels.author
=>Tagore
$.poems.author ^ $.novels.author
$.poems.author
=><poems[0].author>, ..., <poems[5].author>
$.novels.author
=><novels[0].author>, ..., <novels[2].author>
$.poems.author ^ $.novels.author
=><None>
$..*[@.author is 'Tagore'] ^ $.novels
($..*[@.author is 'Tagore']
=><poems[3]>, <poem[4]>, <poem[5]>, <novels[0]>, <novels[1]>
$.novels
=><novels[0]>, ..., <novels[5]>
$..*[@.author is 'Tagore'] ^ $.novels
=><novels[0]>, <novels[1]>
$.poems .^ $.novels
=>{name: Gitanjali, author: Tagore}
Difference/complement
-
and.-
A - B
is a complement ofB
inA
(or difference ofA
andB
) iff every element inA - B
is contained byA
but not byB
.$.poems.author - $.novels.author
=>Pushkin, Pushkin, Pushkin, Tagore, Tagore, Tagore
$.poems.author .- $.novels.author
=>Pushkin
($..*[@.author is 'Tagore'] - $.novels).name
=>Gitanjali, Manasi, Sonar Tori
For performance concerns,
A - B
should only evaluateB
within results ofA
.