File: vim_lambdas.vim

package info (click to toggle)
vim 2%3A9.1.2103-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 93,456 kB
  • sloc: ansic: 433,730; cpp: 6,399; makefile: 4,597; sh: 2,397; java: 2,312; xml: 2,099; python: 1,595; perl: 1,419; awk: 730; lisp: 501; cs: 458; objc: 369; sed: 8; csh: 6; haskell: 1
file content (135 lines) | stat: -rw-r--r-- 2,027 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
" Vim lambda expressions
" VIM_TEST_SETUP hi link vimLambdaOperator Todo
" VIM_TEST_SETUP hi link vimLambdaBrace Todo
" VIM_TEST_SETUP hi link vimFuncParam Identifier


let expr = 42

let Foo = {-> expr}
let Foo = {_ -> expr}
let Foo = {... -> expr}
let Foo = {x -> expr}
let Foo = {x, _ -> expr}
let Foo = {x, ... -> expr}
let Foo = {x, y -> expr}


" Line continuations

let Foo = {->
      "\ comment
      \ expr
      \ }
let Foo = {_ ->
      "\ comment
      \ expr
      \ }
let Foo = {... ->
      "\ comment
      \ expr
      \ }
let Foo = {x ->
      \ expr
      "\ comment
      \ }
let Foo = {x, y ->
      "\ comment
      \ expr
      \ }

let Foo = {
      \ ->
      "\ comment
      \ expr
      \ }
let Foo = {x
      \ ->
      "\ comment
      \ expr
      \ }
let Foo = {x, y
      \ ->
      "\ comment
      \ expr
      \ }

let Foo = {x,
      \ y,
      \ z -> expr}

let Foo = {
      "\ comment
      \ x,
      "\ comment
      \ y,
      "\ comment
      \ z
      "\ comment
      \ ->
      "\ comment
      \ expr
      "\ comment
      \ }

let Foo = {-> [
      \ 42,
      \ 83
      \]}

let Foo = {-> {
      \ 'a': 42,
      \ 'b': 83
      \}}

let Foo = {-> #{
      \ a: 42,
      \ b: 83
      \}}

let Foo = {-> {->[
      \ 42,
      \ 83
      \]}}

let Foo = {-> {-> {
      \ 'a': 42,
      \ 'b': 83
      \}}}

let Foo = {-> {-> #{
      \ a: 42,
      \ b: 83
      \}}}


" :help lambda

:let F = {arg1, arg2 -> arg1 - arg2}
:echo F(5, 2)

:let F = {-> 'error function'}
:echo F('ignored')

:function Foo(arg)
:  let i = 3
:  return {x -> x + i - a:arg}
:endfunction
:let Bar = Foo(4)
:echo Bar(6)

:echo map([1, 2, 3], {idx, val -> val + 1})
" [2, 3, 4]  

:echo sort([3,7,2,1,4], {a, b -> a - b})
" [1, 2, 3, 4, 7]
:let timer = timer_start(500,
		\ {-> execute("echo 'Handler called'", "")},
		\ {'repeat': 3})


" Issue https://github.com/vim/vim/pull/17420#issuecomment-2927798687
" (string immediately after -> operator)

let [func, _func_] = [{->"func"}(), 'func']