GithubHelp home page GithubHelp logo

Comments (11)

rtfeldman avatar rtfeldman commented on July 2, 2024 2

I considered it, but the trouble is that doing this can mask bugs. For example:

function foo() {
  var array = [3, 1, 4];

  array.sort()

  return array;
}

If you change that first line to be Immutable([3, 1, 4]), what should happen?

Currently array.sort() throws an exception, letting you know that some code that made sense with a mutable array no longer makes sense. If instead array.sort() just returns a sorted, immutable array, then now an unsorted array will be returned here, and you have a bug to track down - a potentially nasty one, if there is a lot of logic surrounding this code.

I prefer this API because you can take an immutable array and write any number of helper functions that return sorted ones, but the only way to make bugs like this easy to find is to override the mutable methods to throw an exception. πŸ˜ƒ

from seamless-immutable.

nin-jin avatar nin-jin commented on July 2, 2024 1

You can add method sorted(fn) that returns sorted copy of array.

from seamless-immutable.

alanhogan avatar alanhogan commented on July 2, 2024

Great explanation. Richard, you’re an A+ maintainer.

from seamless-immutable.

dariocravero avatar dariocravero commented on July 2, 2024

Thanks for the explanation. It makes sense :). So how are you going about those when you're using methods like sort? Are you doing something like this?

let array = Immutable([3, 1, 4])
array = Immutable(array.asMutable().sort())

from seamless-immutable.

rtfeldman avatar rtfeldman commented on July 2, 2024

Thanks @alanhogan! 😊

@dariocravero Generally I'm using Underscore functions like _.sortBy and just calling Immutable on the result. No need to call .asMutable() in that case!

from seamless-immutable.

dariocravero avatar dariocravero commented on July 2, 2024

Good stuff :) Will give it a go then!

from seamless-immutable.

tylercollier avatar tylercollier commented on July 2, 2024

What about an option for whether an error should be thrown? I am appreciative of the thrown error as I change from ImmutableJS to this library. But after the transition, my code is cluttered using _.sortBy() instead of the built-in sort().

While the workaround is not terrible, I thought I'd voice my opinion so others who find this issue know there's at least 1 other person wishing for it!

from seamless-immutable.

rtfeldman avatar rtfeldman commented on July 2, 2024

@tylercollier Good news! 😸 https://github.com/rtfeldman/seamless-immutable#performance

from seamless-immutable.

tylercollier avatar tylercollier commented on July 2, 2024

Thanks for the link to that section of the doc, which I read (and admit to not reading before, but you already had me sold on performance from your blog post :-) I think you saw my original comment, which was phrased as an option that I might toggle one way in dev and another in production, but I rephrased it because that's not what I'm really after. What I'd like is an option I can set. My concern here is convenience for me as a developer rather than performance. I'm guessing you have a build step that pulls those checks out when you create a minified production build, rather than an option I can set?

from seamless-immutable.

rtfeldman avatar rtfeldman commented on July 2, 2024

Ahh got it, yeah that's correct. Sorry for the confusion!

That said, I'd honestly rather not add that complication to dev mode. :x

from seamless-immutable.

Nickman87 avatar Nickman87 commented on July 2, 2024

I like the idea of introducing additional methods to support the same functionality as original mutation methods, but have them return an immutable result instead as @nin-jin suggested.
Maybe even more obvious like sort_immutable() so you should know what to expect from it?
This would remove the need for custom functions doing the same thing but requiring an additional dependency in your code.

from seamless-immutable.

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.