" Test for lambda and closurefunction! Test_lambda_feature()call assert_equal(1, has('lambda'))endfunctionfunction! Test_lambda_with_filter()let s:x = 2call assert_equal([2, 3], filter([1, 2, 3], {i, v -> v >= s:x}))endfunctionfunction! Test_lambda_with_map()let s:x = 1call assert_equal([2, 3, 4], map([1, 2, 3], {i, v -> v + s:x}))endfunctionfunction! Test_lambda_with_sort()call assert_equal([1, 2, 3, 4, 7], sort([3,7,2,1,4], {a, b -> a - b}))endfunctionfunction! Test_lambda_with_timer()if !has('timers')returnendiflet s:n = 0let s:timer_id = 0function! s:Foo()"let n = 0let s:timer_id = timer_start(50, {-> execute("let s:n += 1 | echo s:n", "")}, {"repeat": -1})endfunctioncall s:Foo()sleep 200ms" do not collect lambdacall test_garbagecollect_now()let m = s:nsleep 200mscall timer_stop(s:timer_id)call assert_true(m > 1)call assert_true(s:n > m + 1)call assert_true(s:n < 9)endfunctionfunction! Test_lambda_with_partial()let l:Cb = function({... -> ['zero', a:1, a:2, a:3]}, ['one', 'two'])call assert_equal(['zero', 'one', 'two', 'three'], l:Cb('three'))endfunctionfunction Test_lambda_fails()call assert_equal(3, {a, b -> a + b}(1, 2))call assert_fails('echo {a, a -> a + a}(1, 2)', 'E15:')call assert_fails('echo {a, b -> a + b)}(1, 2)', 'E15:')endfuncfunc Test_not_lamda()let x = {'>' : 'foo'}call assert_equal('foo', x['>'])endfuncfunction! Test_lambda_capture_by_reference()let v = 1let l:F = {x -> x + v}let v = 2call assert_equal(12, l:F(10))endfunctionfunction! Test_lambda_side_effect()function! s:update_and_return(arr)let a:arr[1] = 5return a:arrendfunctionfunction! s:foo(arr)return {-> s:update_and_return(a:arr)}endfunctionlet arr = [3,2,1]call assert_equal([3, 5, 1], s:foo(arr)())endfunctionfunction! Test_lambda_refer_local_variable_from_other_scope()function! s:foo(X)return a:X() " refer l:x in s:bar()endfunctionfunction! s:bar()let x = 123return s:foo({-> x})endfunctioncall assert_equal(123, s:bar())endfunctionfunction! Test_lambda_do_not_share_local_variable()function! s:define_funcs()let l:One = {-> split(execute("let a = 'abc' | echo a"))[0]}let l:Two = {-> exists("a") ? a : "no"}return [l:One, l:Two]endfunctionlet l:F = s:define_funcs()call assert_equal('no', l:F[1]())call assert_equal('abc', l:F[0]())call assert_equal('no', l:F[1]())endfunctionfunction! Test_lambda_closure_counter()function! s:foo()let x = 0return {-> [execute("let x += 1"), x][-1]}endfunctionlet l:F = s:foo()call test_garbagecollect_now()call assert_equal(1, l:F())call assert_equal(2, l:F())call assert_equal(3, l:F())call assert_equal(4, l:F())endfunctionfunction! Test_lambda_with_a_var()function! s:foo()let x = 2return {... -> a:000 + [x]}endfunctionfunction! s:bar()return s:foo()(1)endfunctioncall assert_equal([1, 2], s:bar())endfunctionfunction! Test_lambda_call_lambda_from_lambda()function! s:foo(x)let l:F1 = {-> {-> a:x}}return {-> l:F1()}endfunctionlet l:F = s:foo(1)call assert_equal(1, l:F()())endfunctionfunction! Test_lambda_delfunc()function! s:gen()let pl = l:let l:Foo = {-> get(pl, "Foo", get(pl, "Bar", {-> 0}))}let l:Bar = l:Foodelfunction l:Fooreturn l:Barendfunctionlet l:F = s:gen()call assert_fails(':call l:F()', 'E933:')endfunctionfunction! Test_lambda_scope()function! s:NewCounter()let c = 0return {-> [execute('let c += 1'), c][-1]}endfunctionfunction! s:NewCounter2()return {-> [execute('let c += 100'), c][-1]}endfunctionlet l:C = s:NewCounter()let l:D = s:NewCounter2()call assert_equal(1, l:C())call assert_fails(':call l:D()', 'E15:') " E121: then E15:call assert_equal(2, l:C())endfunctionfunction! Test_lambda_share_scope()function! s:New()let c = 0let l:Inc0 = {-> [execute('let c += 1'), c][-1]}let l:Dec0 = {-> [execute('let c -= 1'), c][-1]}return [l:Inc0, l:Dec0]endfunctionlet [l:Inc, l:Dec] = s:New()call assert_equal(1, l:Inc())call assert_equal(2, l:Inc())call assert_equal(1, l:Dec())endfunctionfunction! Test_lambda_circular_reference()function! s:Foo()let d = {}let d.f = {-> d}return d.fendfunctioncall s:Foo()call test_garbagecollect_now()let i = 0 | while i < 10000 | call s:Foo() | let i+= 1 | endwhilecall test_garbagecollect_now()endfunctionfunction! Test_lambda_combination()call assert_equal(2, {x -> {x -> x}}(1)(2))call assert_equal(10, {y -> {x -> x(y)(10)}({y -> y})}({z -> z}))call assert_equal(5.0, {x -> {y -> x / y}}(10)(2.0))call assert_equal(6, {x -> {y -> {z -> x + y + z}}}(1)(2)(3))call assert_equal(6, {x -> {f -> f(x)}}(3)({x -> x * 2}))call assert_equal(6, {f -> {x -> f(x)}}({x -> x * 2})(3))" Z combinatorlet Z = {f -> {x -> f({y -> x(x)(y)})}({x -> f({y -> x(x)(y)})})}let Fact = {f -> {x -> x == 0 ? 1 : x * f(x - 1)}}call assert_equal(120, Z(Fact)(5))endfunctionfunction! Test_closure_counter()function! s:foo()let x = 0function! s:bar() closurelet x += 1return xendfunctionreturn function('s:bar')endfunctionlet l:F = s:foo()call test_garbagecollect_now()call assert_equal(1, l:F())call assert_equal(2, l:F())call assert_equal(3, l:F())call assert_equal(4, l:F())endfunctionfunction! Test_closure_unlet()function! s:foo()let x = 1function! s:bar() closureunlet xendfunctioncall s:bar()return l:endfunctioncall assert_false(has_key(s:foo(), 'x'))call test_garbagecollect_now()endfunctionfunction! LambdaFoo()let x = 0function! LambdaBar() closurelet x += 1return xendfunctionreturn function('LambdaBar')endfunctionfunc Test_closure_refcount()let g:Count = LambdaFoo()call test_garbagecollect_now()call assert_equal(1, g:Count())let g:Count2 = LambdaFoo()call test_garbagecollect_now()call assert_equal(1, g:Count2())call assert_equal(2, g:Count())call assert_equal(3, g:Count2())delfunc LambdaFoodelfunc LambdaBarendfuncfunc Test_named_function_closure()func! Afoo()let x = 14func! s:Abar() closurereturn xendfunccall assert_equal(14, s:Abar())endfunccall Afoo()call assert_equal(14, s:Abar())call test_garbagecollect_now()call assert_equal(14, s:Abar())endfunc
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。