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

Lua: vim.func functional utils #28449

Open
justinmk opened this issue Apr 21, 2024 · 3 comments
Open

Lua: vim.func functional utils #28449

justinmk opened this issue Apr 21, 2024 · 3 comments
Labels
enhancement feature request lua stdlib
Milestone

Comments

@justinmk
Copy link
Member

justinmk commented Apr 21, 2024

Problem

This issue tracks some known functions that we want in vim.func.

Related: vim.iter #23233

Expected behavior

from #8677 (comment) :

@justinmk justinmk added enhancement feature request lua stdlib labels Apr 21, 2024
@justinmk justinmk added this to the backlog milestone Apr 21, 2024
@lewis6991
Copy link
Member

lewis6991 commented Apr 22, 2024

nil is the very annoy params in lua because it not only none value but also absent argument. we can not tell the difference, so if we curry the method, the nil args is rejected to send or the method will return a curried function as a invalid result.

I don't think this is true. select('#', ...) should tell you the number of passed arguments, including any tailing nil's.

I would implement currying as this:

local function curry(fn, ...)
  local n, args = select('#', ...), { ... }
  return function(...)
    local fargs = {}
    for i = 1, n do
      fargs[i] = args[i]
    end
    for i = 1, select('#', ...) do
      fargs[n+i] = select(i, ...)
    end
    return fn(unpack(fargs, 1, n + n2)
  end
end

local curried = curry(f, 1, 2, 3, 4, nil, nil)

curried(5, 6, 7, nil)
-- equiv to:
-- f(1, 2, 3, 4, nil, nil, 5, 6, 7, nil)

@justinmk
Copy link
Member Author

curried(5, 6, 7, nil)

Is that currying or partial application? https://stackoverflow.com/a/218055/152142

@lewis6991
Copy link
Member

lewis6991 commented Apr 22, 2024

Ah yeah, it's creating a partial, which is what you usually want.

I'm sure the same concept can be used to create a currying function.

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

No branches or pull requests

2 participants