2

I'm trying to understand the code suggested in https://vi.stackexchange.com/a/28506/6189:

function MyLinks(pat, spat, ssub, sflags) abort
  return glob(a:pat, v:false, v:true)
        \ ->map({_, v -> printf('[%s](%s)', fnamemodify(v, ':t:r')->substitute(a:spat, a:ssub, a:sflags), fnamemodify(v, ':t'))})
        \ ->join("\n")
endfunction

...and this map({_,... syntax confuses me.

:h map() says: map({expr1}, {expr2}) and states: "Replace each item in {expr1} with the result of evaluating {expr2}".

So map is using the input from glob(...) as its first argument, and this {long..bunch..of..stuff} is the second argument. That makes sense. But "long bunch of stuff" begins with {_,. What is that? What is _?

1

1 Answer 1

5

See my answer to your other question about v: What is `v` in this vimscript?

_ is the chosen name for the key/index parameter of the lambda. As it isn't used, _ is used to say ignored/unused.

1
  • Note that unlike some languages, _ doesn't get treated any different from any other variable name as far as I know; one of the side-effects is that you can't have multiple parameters named _ in your function signature, which is sometimes useful if you want to ignore more than one parameter. Dec 25, 2020 at 2:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.