aearly/icepick

Create intermediate objects for missing keys in assocIn/updateIn

esamattis opened this issue · 2 comments

Coming from Clojure1. I was surprised that assocIn and updateIn does not create the intermediate objects if they are missing. I find that behavior extremely useful when creating complex data structures.

  1. https://clojuredocs.org/clojure.core/assoc-in

I know the current behavior is documented and the change would be backwards incompatible but since icepick is not at 1.0 yet I wanted to bring this up.

I seem to end up writing quite a lot if-statements like this:

  constructor() {
    this.state = I.freeze({});
  }
  handleSetComments(comments) {

        comments.forEach(comment => {
            if (!this.state.comments) {
                this.state = I.assoc(this.state, "comments", {});
            }

            if (!this.state.comments[comment.ticketId]) {
                this.state = I.assocIn(this.state, ["comments", comment.ticketId], {});
            }

            this.state = I.assocIn(this.state, ["comments", comment.ticketId, comment.id], comment);
        });

    }

which is fairly annoying and gets worse when nesting increases.

👍 This shouldn't be too hard to accommodate.