1function! Test_lambda_with_filter() 2 let s:x = 2 3 call assert_equal([2, 3], filter([1, 2, 3], {i, v -> v >= s:x})) 4endfunction 5 6function! Test_lambda_with_map() 7 let s:x = 1 8 call assert_equal([2, 3, 4], map([1, 2, 3], {i, v -> v + s:x})) 9endfunction 10 11function! Test_lambda_with_sort() 12 call assert_equal([1, 2, 3, 4, 7], sort([3,7,2,1,4], {a, b -> a - b})) 13endfunction 14 15function! Test_lambda_with_timer() 16 if !has('timers') 17 return 18 endif 19 20 let s:n = 0 21 let s:timer_id = 0 22 function! s:Foo() 23 "let n = 0 24 let s:timer_id = timer_start(50, {-> execute("let s:n += 1 | echo s:n", "")}, {"repeat": -1}) 25 endfunction 26 27 call s:Foo() 28 sleep 200ms 29 " do not collect lambda 30 call test_garbagecollect_now() 31 let m = s:n 32 sleep 200ms 33 call timer_stop(s:timer_id) 34 call assert_true(m > 1) 35 call assert_true(s:n > m + 1) 36 call assert_true(s:n < 9) 37endfunction 38 39function! Test_lambda_with_partial() 40 let l:Cb = function({... -> ['zero', a:1, a:2, a:3]}, ['one', 'two']) 41 call assert_equal(['zero', 'one', 'two', 'three'], l:Cb('three')) 42endfunction 43 44function Test_lambda_fails() 45 call assert_equal(3, {a, b -> a + b}(1, 2)) 46 call assert_fails('echo {a, a -> a + a}(1, 2)', 'E15:') 47 call assert_fails('echo {a, b -> a + b)}(1, 2)', 'E15:') 48endfunc 49 50func Test_not_lamda() 51 let x = {'>' : 'foo'} 52 call assert_equal('foo', x['>']) 53endfunc 54 55function! 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)) 60endfunction 61 62function! 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)()) 74endfunction 75 76function! 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()) 87endfunction 88 89function! 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]()) 101endfunction 102 103function! 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()) 115endfunction 116 117function! 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()) 127endfunction 128 129function! 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()()) 137endfunction 138 139function! 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:') 150endfunction 151 152function! 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()) 168endfunction 169 170function! 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()) 183endfunction 184 185function! 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() 196endfunction 197 198function! 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)) 211endfunction 212