google/vim-maktaba

maktaba#function#Filter modifies input

flwyd opened this issue · 2 comments

flwyd commented

Documentation for maktaba#function#Filter says This is like |filter|, except {func} may be any maktaba callable and a new list is created. Unlike the builtin filter() function, {list} WILL NOT be modified in place.

The output from the following test indicates that it does modify the input.

let s:numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
echo 'starting list is' s:numbers
let s:odds = maktaba#function#Filter(s:numbers, {x -> x % 2 == 1})
echo 'list with odds is' s:odds
let s:threes = maktaba#function#Filter(s:odds, {x -> x % 3 == 0})
echo 'list with threes is' s:threes
echo 'now odds is' s:odds
echo 'now numbers is' s:numbers

Output:

starting list is [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
list with odds is [1, 3, 5, 7, 9]
list with threes is [3, 9]
now odds is [3, 9]
now numbers is [3, 9]
flwyd commented

The docs could also clarify that it's unlike filter() in that func takes one arg, not two (builtin filter() passes v:key and v:val to functions. For example, filter(l:list, {x -> !empty(x)}) results in a list without its first element (whether or not the value was empty), but maktaba#function#filter(l:list, {x -> !empty(x)}) results in a list with no empty elements.

I didn't even notice that 2-arg difference. And also on that note, it only accepts lists where built-in filter() also accepts dictionaries. OTOH, it's a thin convenience wrapper around filter(copy(...), maktaba#function#Call(...)), so we probably don't need to bend over backwards to make it handle every case.