I have a list of strings which is interesting in its own right, but I also want a filtered version of it for other purposes. But I discovered to my horror that filter
, in addition to returning a filtered version of the list passed in, also changes the list passed in. Even copying it over apparently doesn't work. Try these directly in command-mode:
let l=[2,3]
let m=l
let o=filter(m,'v:val!=2')
echo l
All three lists are modified by this operation. Can we write a fixed filter version that does what we want? E.g.
function realfilter(list, predicate) abort
return filter(list, predicate) " this still has the same problem
endfunction
m=l
doesn't make a copy of the list it only copies the reference to it. Copy withcopy()
ordeepcopy()
depending on whether you want shallow or deep copy.