Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

maktaba#function#Filter modifies input #250

Open
flwyd opened this issue Oct 16, 2021 · 2 comments
Open

maktaba#function#Filter modifies input #250

flwyd opened this issue Oct 16, 2021 · 2 comments

Comments

@flwyd
Copy link
Contributor

flwyd commented Oct 16, 2021

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
Copy link
Contributor Author

flwyd commented Oct 22, 2021

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.

@dbarnett
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants