comparison src/testdir/test_lambda.vim @ 9686:8c2553beff0f v7.4.2119

commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f Author: Bram Moolenaar <Bram@vim.org> Date: Fri Jul 29 22:15:09 2016 +0200 patch 7.4.2119 Problem: Closures are not supported. Solution: Capture variables in lambdas from the outer scope. (Yasuhiro Matsumoto, Ken Takata)
author Christian Brabandt <cb@256bit.org>
date Fri, 29 Jul 2016 22:30:08 +0200
parents 3ca0fd9709b1
children 2ea935bdd1a1
comparison
equal deleted inserted replaced
9685:fee40b9f4989 9686:8c2553beff0f
19 19
20 let s:n = 0 20 let s:n = 0
21 let s:timer_id = 0 21 let s:timer_id = 0
22 function! s:Foo() 22 function! s:Foo()
23 "let n = 0 23 "let n = 0
24 let s:timer_id = timer_start(50, {-> execute("let s:n += 1 | echo s:n")}, {"repeat": -1}) 24 let s:timer_id = timer_start(50, {-> execute("let s:n += 1 | echo s:n", "")}, {"repeat": -1})
25 endfunction 25 endfunction
26 26
27 call s:Foo() 27 call s:Foo()
28 sleep 200ms 28 sleep 200ms
29 " do not collect lambda 29 " do not collect lambda
49 49
50 func Test_not_lamda() 50 func Test_not_lamda()
51 let x = {'>' : 'foo'} 51 let x = {'>' : 'foo'}
52 call assert_equal('foo', x['>']) 52 call assert_equal('foo', x['>'])
53 endfunc 53 endfunc
54
55 function! Test_lambda_capture_by_reference()
56 let v = 1
57 let l:F = {x -> x + v}
58 let v = 2
59 call assert_equal(12, l:F(10))
60 endfunction
61
62 function! Test_lambda_side_effect()
63 function! s:update_and_return(arr)
64 let a:arr[1] = 5
65 return a:arr
66 endfunction
67
68 function! s:foo(arr)
69 return {-> s:update_and_return(a:arr)}
70 endfunction
71
72 let arr = [3,2,1]
73 call assert_equal([3, 5, 1], s:foo(arr)())
74 endfunction
75
76 function! Test_lambda_refer_local_variable_from_other_scope()
77 function! s:foo(X)
78 return a:X() " refer l:x in s:bar()
79 endfunction
80
81 function! s:bar()
82 let x = 123
83 return s:foo({-> x})
84 endfunction
85
86 call assert_equal(123, s:bar())
87 endfunction
88
89 function! Test_lambda_do_not_share_local_variable()
90 function! s:define_funcs()
91 let l:One = {-> split(execute("let a = 'abc' | echo a"))[0]}
92 let l:Two = {-> exists("a") ? a : "no"}
93 return [l:One, l:Two]
94 endfunction
95
96 let l:F = s:define_funcs()
97
98 call assert_equal('no', l:F[1]())
99 call assert_equal('abc', l:F[0]())
100 call assert_equal('no', l:F[1]())
101 endfunction
102
103 function! Test_lambda_closure()
104 function! s:foo()
105 let x = 0
106 return {-> [execute("let x += 1"), x][-1]}
107 endfunction
108
109 let l:F = s:foo()
110 call test_garbagecollect_now()
111 call assert_equal(1, l:F())
112 call assert_equal(2, l:F())
113 call assert_equal(3, l:F())
114 call assert_equal(4, l:F())
115 endfunction
116
117 function! Test_lambda_with_a_var()
118 function! s:foo()
119 let x = 2
120 return {... -> a:000 + [x]}
121 endfunction
122 function! s:bar()
123 return s:foo()(1)
124 endfunction
125
126 call assert_equal([1, 2], s:bar())
127 endfunction
128
129 function! Test_lambda_call_lambda_from_lambda()
130 function! s:foo(x)
131 let l:F1 = {-> {-> a:x}}
132 return {-> l:F1()}
133 endfunction
134
135 let l:F = s:foo(1)
136 call assert_equal(1, l:F()())
137 endfunction
138
139 function! Test_lambda_delfunc()
140 function! s:gen()
141 let pl = l:
142 let l:Foo = {-> get(pl, "Foo", get(pl, "Bar", {-> 0}))}
143 let l:Bar = l:Foo
144 delfunction l:Foo
145 return l:Bar
146 endfunction
147
148 let l:F = s:gen()
149 call assert_fails(':call l:F()', 'E117:')
150 endfunction
151
152 function! Test_lambda_scope()
153 function! s:NewCounter()
154 let c = 0
155 return {-> [execute('let c += 1'), c][-1]}
156 endfunction
157
158 function! s:NewCounter2()
159 return {-> [execute('let c += 100'), c][-1]}
160 endfunction
161
162 let l:C = s:NewCounter()
163 let l:D = s:NewCounter2()
164
165 call assert_equal(1, l:C())
166 call assert_fails(':call l:D()', 'E15:') " E121: then E15:
167 call assert_equal(2, l:C())
168 endfunction
169
170 function! Test_lambda_share_scope()
171 function! s:New()
172 let c = 0
173 let l:Inc0 = {-> [execute('let c += 1'), c][-1]}
174 let l:Dec0 = {-> [execute('let c -= 1'), c][-1]}
175 return [l:Inc0, l:Dec0]
176 endfunction
177
178 let [l:Inc, l:Dec] = s:New()
179
180 call assert_equal(1, l:Inc())
181 call assert_equal(2, l:Inc())
182 call assert_equal(1, l:Dec())
183 endfunction
184
185 function! Test_lambda_circular_reference()
186 function! s:Foo()
187 let d = {}
188 let d.f = {-> d}
189 return d.f
190 endfunction
191
192 call s:Foo()
193 call test_garbagecollect_now()
194 let i = 0 | while i < 10000 | call s:Foo() | let i+= 1 | endwhile
195 call test_garbagecollect_now()
196 endfunction
197
198 function! Test_lambda_combination()
199 call assert_equal(2, {x -> {x -> x}}(1)(2))
200 call assert_equal(10, {y -> {x -> x(y)(10)}({y -> y})}({z -> z}))
201 call assert_equal(5.0, {x -> {y -> x / y}}(10)(2.0))
202 call assert_equal(6, {x -> {y -> {z -> x + y + z}}}(1)(2)(3))
203
204 call assert_equal(6, {x -> {f -> f(x)}}(3)({x -> x * 2}))
205 call assert_equal(6, {f -> {x -> f(x)}}({x -> x * 2})(3))
206
207 " Z combinator
208 let Z = {f -> {x -> f({y -> x(x)(y)})}({x -> f({y -> x(x)(y)})})}
209 let Fact = {f -> {x -> x == 0 ? 1 : x * f(x - 1)}}
210 call assert_equal(120, Z(Fact)(5))
211 endfunction