GithubHelp home page GithubHelp logo

$.uniq doesn't retain order about dollar HOT 9 CLOSED

ankurp avatar ankurp commented on July 20, 2024
$.uniq doesn't retain order

from dollar.

Comments (9)

ankurp avatar ankurp commented on July 20, 2024

The order is not guaranteed as few things can happen. If order is important then either it is sorted and returned, or it is returned in the order it was passed.

from dollar.

djones avatar djones commented on July 20, 2024

it is returned in the order it was passed.

This is what I would expect to happen but that doesn't currently happen.

Order isn't kept for $.uniq but other methods such as $.compact do return things in the order they are passed. So I guess I was expecting $.uniq to do the same.

If you think $.uniq shouldn't keep the order then this issue becomes a feature request for a method that provides $.uniq returning items in the same order they were provided.

from dollar.

djones avatar djones commented on July 20, 2024

Also thanks for a great library. It covers off so many things that should be standard in Swift.

from dollar.

ankurp avatar ankurp commented on July 20, 2024

Yes adding it as an option makes sense as underneath it is using dictionary keys and returning that which are uniq but the order is ignored for speed.

from dollar.

ankurp avatar ankurp commented on July 20, 2024

Now the other thing to figure out is what is the order if there are duplicate values as such [Stockholm, SE, Stockholm]

from dollar.

ankurp avatar ankurp commented on July 20, 2024

Should it be [Stockholm, SE] or [SE, Stockholm]. The question is should we pick the last uniq value and its order or pick the first and use that order.

from dollar.

djones avatar djones commented on July 20, 2024

I think [Stockholm, SE]. It makes sense to me to iterate through the array in order, accepting return values if they are unique and then discarding anything that comes up that isn't uniq.

So $.uniq([1,2,3,1,4]) outputs [1,2,3,4]

You were talking about performance which is great. Would the optional be something like this:

$.uniq([1,2,3,1,4], preserveOrder: true)

The question is would order preservation be turned on or off by default? I'd be interested to know what you think, but my opinion is it would be better to keep Dollar.swift accurate by default, not tripping people up but have the option of disabling order preservation for speed.

Array's in Swift by default preserve order and so this is why I raised this issue because I ran my array through $.uniq and suddenly it was not in order!

Are there other instances where you've had to decide on speed vs accuracy? If so which did you pick?

from dollar.

djones avatar djones commented on July 20, 2024

Thanks heaps @ankurp!

from dollar.

ankurp avatar ankurp commented on July 20, 2024

No problem.

from dollar.

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.