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)


有一個快速排序和一個選擇排序。dom

代碼說明一切,不作過多解釋。函數式編程


看一下一個閉包的使用。函數

-- 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 非常漂亮。ui

說明了 Lua 的函數式編程能力。this

關於 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 便可。

相關文章
相關標籤/搜索