Git Product home page Git Product logo

Comments (8)

michaelfranz avatar michaelfranz commented on August 15, 2024 1

Is anyone progressing this proposal?

from jsonata.

andrew-coleman avatar andrew-coleman commented on August 15, 2024

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.

xogeny avatar xogeny commented on August 15, 2024

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.

abrodeur avatar abrodeur commented on August 15, 2024

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.

fdavies93 avatar fdavies93 commented on August 15, 2024

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.

Zizaco avatar Zizaco commented on August 15, 2024

$distinct(array) +1

that would be good

from jsonata.

darylwdavis avatar darylwdavis commented on August 15, 2024

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.

darylwdavis avatar darylwdavis commented on August 15, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.