Comments (8)
Is anyone progressing this proposal?
from jsonata.
I've been thinking for a while about how to best implement this. For starters, I need to upgrade the equality operator to work for non-atomic types. Currently [1,2] = [1,2]
returns false
which is clearly wrong. This needs fixing anyway. The bigger challenge is eliminating duplicate values in an efficient way, without comparing every value with every other (n-squared efficiency - terrible!). Hash sets normally come to the rescue here, but I would need to come up with a hash function that copes with all data types including hierarchies of maps and arrays.
from jsonata.
This seems pretty ambitious to me without external dependencies. There are whole libraries out there to handle things like equality testing. Getting into implementing uniqueness as well and you are opening to door to reimplementing something like lodash
. I'm not saying it is necessarily a bad idea. But I would be cautious.
Also note that we've discussed in the past the idea of having a "core" language and "extensions". If structured that way, the core could be dependency free. But an extension might bring something like lodash
in and be able to provide a lot of additional functionality with very little implementation cost.
from jsonata.
Here is my solutions to the problem using current functions. One thing to note is since I do sort you do lose order.
(
$unique := function($v, $i, $a){$i != 0 ? (( $v = $a[$i-1] ) ? "": $v) : $v };
$notEmpty := function($v){$v !=""};
{
"productIDs": **.ProductID,
"uniqProductIDs" :$filter($map($sort( **.ProductID),$unique),$notEmpty)
}
)
from jsonata.
This probably only works for first-order objects as it relies on the in operator, but it's succinct and clear:
(
$dupReduce := function($acc, $el) {
$el in $acc ? $acc : $append($acc,$el)
};
$removeDups := function ($a) {
$reduce($a,$dupReduce)
};
$removeDups(input)
)
The only part you'd need to change for more complex comparisons would be the test at the start of the $dupReduce higher-order function.
Alternately you wait for @andrew-coleman to extend in to do more complex comparisons.
from jsonata.
$distinct(array)
+1
that would be good
from jsonata.
I found @shrickus suggestion useful if the array is a selection of strings, for example a query:
devices.*.itemSelected
Then you can use the strings temporarily as object keys to eliminate duplicates, then return the keys:
$keys(devices.*{itemSelected:""})
from jsonata.
If the result contains only one object, $keys returns a string so you need to include all in brackets to always return an array.
[$keys(devices.*{itemSelected:""})]
from jsonata.
Related Issues (20)
- Help resolve current pinning to 1.8.6 for JSONata-Wrapper (possibly ducktape compatibility)
- Reserved Tokens Documentation
- ($.some_non_existent_key != 'anything') evaluates to false HOT 3
- $string(number) capped at precision-15
- Error handling and asynchronous behavior in evaluate function
- Rounding number without "Round half to even"
- There isn't a uuid() function for JSONata HOT 2
- Hard spaces in documentation not rendered correctly
- Security contact for JSONata HOT 4
- default query on try.jsonata.org HOT 1
- is 2.0.4 a fake release? HOT 4
- Multi-level JSON processing: Unwrapping Objects
- How to work with parent object and transform values
- Stronger `registerFunction` TS types by inferring types from signature, and a road to a language service (PR coming!)
- Feature: Jsonata json type inference HOT 5
- JSONata leaks internal references in expression when using lambdas
- JSONAta expression can be created that ends up in mutation of original input object
- `__evaluate_entry` and `__evaluate_exit` can be overridden inside expression
- $sort fails for >6000-element arrays HOT 1
- Why is jsonata async/await? HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jsonata.