Lua5.0 示例程序抽样

Lua5.0 的 test 目录里是一些 Lua 示例程序,有些挺有意思的,这里拿出来看看。

简单的介绍在 README 文件里有描述。

这些程序应该就是原作者的团队写的,有的程序的含金量很高,也很有意思。

比如协程的使用(sieve.lua),比如生命游戏(life.lua),比如排序(sort.lua),比如几个算法。


看一下排序

-- two implementations of a sort function
-- this is an example only. Lua has now a built-in function "sort"
-- extracted from Programming Pearls, page 110
function qsort(x,l,u,f)
 if l<u then
  local m=math.random(u-(l-1))+l-1-- choose a random pivot in range l..u
  x[l],x[m]=x[m],x[l] -- swap pivot to first position
  local t=x[l] -- pivot value
  m=l
  local i=l+1
  while i<=u do
    -- invariant: x[l+1..m] < t <= x[m+1..i-1]
    if f(x[i],t) then
      m=m+1
      x[m],x[i]=x[i],x[m] -- swap x[i] and x[m]
    end
    i=i+1
  end
  x[l],x[m]=x[m],x[l] -- swap pivot to a valid place
  -- x[l+1..m-1] < x[m] <= x[m+1..u]
  qsort(x,l,m-1,f)
  qsort(x,m+1,u,f)
 end
end
function selectionsort(x,n,f)
 local i=1
 while i<=n do
  local m,j=i,i+1
  while j<=n do
   if f(x[j],x[m]) then m=j end
   j=j+1
  end
 x[i],x[m]=x[m],x[i] -- swap x[i] and x[m]
 i=i+1
 end
end
function show(m,x)
 io.write(m,"\n\t")
 local i=1
 while x[i] do
  io.write(x[i])
  i=i+1
  if x[i] then io.write(",") end
 end
 io.write("\n")
end
function testsorts(x)
 local n=1
 while x[n] do n=n+1 end; n=n-1 -- count elements
 show("original",x)
 qsort(x,1,n,function (x,y) return x<y end)
 show("after quicksort",x)
 selectionsort(x,n,function (x,y) return x>y end)
 show("after reverse selection sort",x)
 qsort(x,1,n,function (x,y) return x<y end)
 show("after quicksort again",x)
end
-- array to be sorted
x={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"}
testsorts(x)


有一个快速排序和一个选择排序。

代码说明一切,不做过多解释。


看一下一个闭包的使用。

-- function closures are powerful
-- traditional fixed-point operator from functional programming
Y = function (g)
      local a = function (f) return f(f) end
      return a(function (f)
                 return g(function (x)
                             local c=f(f)
                             return c(x)
                           end)
               end)
end
-- factorial without recursion
F = function (f)
      return function (n)
               if n == 0 then return 1
               else return n*f(n-1) end
             end
    end
factorial = Y(F)   -- factorial is the fixed point of F
-- now test it
function test(x)
 io.write(x,"! = ",factorial(x),"\n")
end
for n=0,16 do
 test(n)
end

非递归的求阶乘,这里的 Y combinator 很是漂亮。

说明了 Lua 的函数式编程能力。

关于 Y combinator 的相关描述,这里有一篇写的很好的文章,感兴趣的可以看一下。

http://blog.csdn.net/pongba/article/details/1336028 

标题叫做: 康托尔、哥德尔、图灵——永恒的金色对角线


etc 目录里也有几个实用小程序,比如 bin2c.c 。看一下 README 里的介绍:

bin2c.c

 This program converts files to byte arrays that are automatically run

 with lua_dobuffer. This allows C programs to include all necessary Lua

 code, even in precompiled form. Even if the code is included in source

 form, bin2c is useful because it avoids the hassle of having to quote

 special characters in C strings.

 Example of usage: Run bin2c file1 file2 ... > init.h. Then, in your

 C program, just do #include "init.h" anywhere in the *body* of a

 function. This will be equivalent to calling

  lua_dofile(L,"file1"); lua_dofile(L,"file2"); ...

 Note that the Lua state is called "L". If you use a different name,

 say "mystate", just #define L mystate before you #include "init.h".

简单地说,这个程序可以把一个文件转换为字节流。保存到一个大大的字节数组里。

使用的时候调用 lua_dobuffer 即可。


转载于:https://my.oschina.net/xhan/blog/502741

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值