zoukankan      html  css  js  c++  java
  • Lua 的 学习 值类型传递、引用类型传递、闭包、尾调用、迭代器(链)

    -------------------- 值类型传递
    --[[
    a=10;
    b=20;
    function math(x,y)
        x=20;
        y=30;
        return x+y;
    end
    foo= math;
    print(foo(a,b));
    print(a);
    print(b);
    --]]
    -------------------- 值类型传递
    --[[
    tab={
        a=10;
        b=20;
    }
    function math(x,y)
        x=20;
        y=30;
        return x+y;
    end
    foo= math;
    print(foo(tab.a,tab.b));
    print(tab.a);
    print(tab.b);
    --]]
    -------------------- 值类型传递
    --[[
    a="10";
    b="20";
    function math(x,y)
        x="20";
        y="30";
        return x+y;
    end
    foo= math;
    print(foo(a,b));
    print(a);
    print(b);
    --]]




    -------------------- 值类型传递
    --[[
    tab={
        a="10d";
        b="20w";
    }
    function math(x,y)
        x="20dd";
        y="30ww";
        return x..y;
    end
    foo= math;
    print(foo(tab.a,tab.b));
    print(tab.a);
    print(tab.b);
    --]]



    -------------------- 引用类型传递
    --[[
    tab={
        a=10;
        b=20;
    }
    function math(tab)
        tab.a=20;
        tab.b=30;
        return tab.a..tab.b;
    end
    foo= math;
    print(foo(tab));
    print(tab.a);
    print(tab.b);
    --]]



    -------------------- 闭包
    --[[
    function func(x,y)
        local i =0;
        return function()
            i=i+1;
            return x..y..i
        end
    end



    print("-----闭包-----")
    fun= func(1,2);--这里的fun 返回的是 func(x,y) 这个函数
    print(fun())   --这里的fun() 返回的是 func(x,y) 里面的 匿名 function() 这个函数
    print(fun())
    print(fun())
    print(fun())
    print("-----非闭包-----")
    print(func(1,2)());
    print(func(1,2)());
    print(func(1,2)());
    print(func(1,2)());
    --]]
    -------------------- 尾调用



    --[[
    function func(n)
        if n <= 0 then
            print("---");
            return 0;
        end
        local a = func(n-1)
        print(a);
        return n * a;
    end



    func(10000000000000);
    --]]



    --[[
    function func(n, now)
        if n <= 0 then
            print("---");
            return now
        end
        print("---");
        return func(n-1, now*n)
    end
    func(10000000000, 1)



    --]]
    -------------------- 迭代器(链)
    --[[
    function travel(list)
        local function work(list,node)
            if not node then 
                return list;
            else
                return node.next;--这里的对象将返回给 迭代器的 变量列表,同时也会成为迭代函数的  状态变量 参数。
            end
        end
        return work,list,nil;--这里返回的是 迭代函数,状态变量,控制变量
    end
    lists = nil
    lists={index=1,next=lists};
    lists={index=2,next=lists};
    lists={index=3,next=lists};
    for i=4,8 do
        lists={index=i,next=lists};
    end



    for node in travel(lists)   do  --这里的 travel(lists)  不是迭代函数,而是一个普通函数,但是这个普通函数 的返回值 为三个 对象. 这三个对象 才是迭代器所需要的的 迭代函数,状态变量,控制变量
        print(node.index);
    end
    --]]





  • 相关阅读:
    Java实现 LeetCode 189 旋转数组
    Java实现 蓝桥杯 算式最大值
    Java实现 蓝桥杯 算式最大值
    Java实现 蓝桥杯 算式最大值
    Java实现 神犇的悲惨一生
    编译Boost 详细步骤 适用 VC6 VS2003 VS2005 VS2008 VS2010
    boost:asio编译
    VS2008下编译BOOST 1.39的ASIO库
    Boost下载安装编译配置使用指南
    Boost下载安装编译配置使用指南(含Windows和Linux
  • 原文地址:https://www.cnblogs.com/xulang/p/12504773.html
Copyright © 2011-2022 走看看