bobbicodes / bobbi-lisp

Interactive Lisp environment for learning Clojure
https://bobbicodes.github.io/bobbi-lisp/
0 stars 0 forks source link

Immutability fail #20

Open bobbicodes opened 10 months ago

bobbicodes commented 10 months ago

I just realized that I think I've been negligent about cloning my data structures before operating on them and have introduced mutation. And since it could be any combination of dozens of places it's gonna be fun tracking it down.

No really, it will be fun. After falling on my face dealing with lazy seqs pretty much anything else will be fun

I'm spoiled by Clojure. I messed up because this isn't something I'm used to having to think about.

bobbicodes commented 10 months ago

I fixed this in disj which was where I noticed this was an issue. It was indeed necessary to clone the input set:

function _disj(set) {
    var args = Array.from(arguments).slice(1)
    var new_set = types._clone(set)
    for (let i = 0; i < args.length; i++) {
        new_set.delete(args[i])
    }
    return new_set
}

Curiously, it was not enough to just define a new set! The first thing I tried was var new_set = set which still results in mutating the original set.