Git Product home page Git Product logo

Comments (13)

rtfeldman avatar rtfeldman commented on June 30, 2024 7

Keep in mind that the function map accepts does receive index, so you can just do:

var indexToUpdate = 2, newVal = "THREE";

Immutable(["one", "two", "three", "four"]).map(function(val, index) {
  return (index === indexToUpdate) ? newVal : val;
});

Admittedly more verbose than mutableArray[indexToUpdate] = newVal, but in my experience this hasn't come up all that often.

from seamless-immutable.

bradwestfall avatar bradwestfall commented on June 30, 2024 6

oh, duh. Thanks

from seamless-immutable.

starInEcust avatar starInEcust commented on June 30, 2024 5

How about splice?

// pretty ugly
log.slice(0, foundIdx).concat(newval).concat(log.slice(foundIdx+1))

from seamless-immutable.

RangerMauve avatar RangerMauve commented on June 30, 2024 4

@bradwestfall array is immutable. .concat() always returns a new array. Your example should instead be:

let array = Immutable([1,2,3]).concat([4])
let array2 = array.concat([5])
console.log(array2) // [1,2,3,4,5]

from seamless-immutable.

rtfeldman avatar rtfeldman commented on June 30, 2024 1

For that example, I would just use concat:

 // equivalent to Immutable([1, 2, 3, 4])
Immutable([1,2,3]).concat([4])

The Object and Array methods that do not mutate are still available, although they return Immutable instances instead of normal arrays and objects.

The non-mutating array methods seamless-immutable supports are map, filter, slice, concat, reduce, and reduceRight, and for objects just keys.

Does that help?

from seamless-immutable.

RangerMauve avatar RangerMauve commented on June 30, 2024

Just to interject, the docs say that the arguments to concat don't have to be an array, so the square brackets can be left out, and it'll look more like your push code.

from seamless-immutable.

holyjak avatar holyjak commented on June 30, 2024

Thanks. I need to think through it to see whether filter, concat etc. cover
all modification cases. I guess eg. replacing an element at a given index
could be achieved with map though that is cumbersome.
5. juni 2015 19:30 skrev "Richard Feldman" [email protected]:

For that example, I would just use concat:

// equivalent to Immutable([1, 2, 3, 4])
Immutable([1,2,3]).concat([4])

The Object and Array methods that do not mutate are still available,
although they return Immutable instances instead of normal arrays and
objects.

The non-mutating array methods seamless-immutable supports are map, filter,
slice, concat, reduce, and reduceRight, and for objects just keys.

Does that help?


Reply to this email directly or view it on GitHub
#43 (comment)
.

from seamless-immutable.

holyjak avatar holyjak commented on June 30, 2024

So if I should sum it up, seamless-immutable supports most/all mutation cases though sometimes you need to think carefully how to combine the available methods to achieve it.

My particular use case is: If an array has an element with a given id, remove it and re-insert it to the end with an increased count; otherwise just append it. So I'd need to do something like:

var _ = require("lodash");
var log = Immutable([]);
function add(idToFind) {
var foundIdx = _.find(log, { id: idToFind });
var count = 1;
if (foundIdx) {
  log = log.slice(0, foundIdx).concat(log.slice(foundIdx+1));
  // With icepick I could do: `log = i.splice(log, foundIdx, 1);`
  count += 1;
}
log = log.concat({id: idToFind, count: count});
} //fn

In some cases, when performance is important - e.g. when I need to "change" an element of a long array relatively often - then I have to use a less straightforward solution. For example:

// This is slow as it iterates over the whole array each time, not stopping after the element is found:
log = log.map(function(val, idx) { if(val.id === idToFind) return {id: val.id, count: val.count+1}; else return val; });

// This is *hopefully* faster since it only needs to iterate on average over 1/2 of the array
// (even though we still need to combine a few operations):
var foundIdx = _.find(log, { id: idToFind });
var val = log[idToFind]; var newval = {id: val.id, count: val.count+1};
log = log.slice(0, foundIdx).concat(newval).concat(log.slice(foundIdx+1))

On the other hand it might me better to just use mutable data structures for this and only use immutability where changes are infrequent.

from seamless-immutable.

rtfeldman avatar rtfeldman commented on June 30, 2024

Yeah, it all depends on your use case...for our use case, we haven't encountered any performance problems where the underlying culprit turned out to be using immutable data structures over mutable ones. Of course, you may be dealing with much larger data than we do. 😄

from seamless-immutable.

bitinn avatar bitinn commented on June 30, 2024

Given the lack of object mutation method, am I right to say the common (and fastest) way to update (or diff) immutable object is to use merge/without? I have a feeling using asMutable then mutate object and then wrap them into an immutable again will be at least O(n).

PS: assume I want to modify a nested immutable object { a: { b: 1 } }, what's the best way to set b = 2 in seamless-immutable case?

from seamless-immutable.

rtfeldman avatar rtfeldman commented on June 30, 2024

Yeah, merge and without are definitely the most common ways.

For the nested case, just use merge to override a to have a new value that includes a new value of b. If you really needed to, you could get fancier with a custom merger, but I've personally had no issues just using vanilla merge.

from seamless-immutable.

dgieselaar avatar dgieselaar commented on June 30, 2024

@rtfeldman: there's a pattern w/ gulp-cached and gulp-remember which looks a little like this:

stream
  .pipe(cache())
  .pipe(doStuffOnUncachedItemsOnly())
  .pipe(remember())
  .pipe(doStuffOnAllItems())

Perhaps something similar would would solve this problem as well:

collection
  .map(mapFn)
  .only(filterFn)
    .map(mapFilteredFn)
  .all()

Just thinking out loud here.

from seamless-immutable.

bradwestfall avatar bradwestfall commented on June 30, 2024

I know it's an old thread, buy why does .concat not work after the fact

let array = Immutable([1,2,3]).concat([4])
array.concat([5])
console.log(array) // [1,2,3,4]

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.