LuaJIT高性能函數式編程庫:Lua Fun
Lua Fun 是一個高性能函數式編程庫,專為 LuaJIT tracing just-in-time compiler設計。 提供了想諸如標準ML,Haskell,Erlang,JavaScript,Python和Lisp等語言那樣的50的編程原語。所提供的map, filter, reduce, zip高階函數助你編寫簡單而高效的函數式代碼。
示例:
> -- Functional style > require "fun" () > -- calculate sum(x for x^2 in 1..n) > n = 100 > print(reduce(operator.add, 0, map(function(x) return x^2 end, range(n)))) 328350 > -- Object-oriented style > local fun = require "fun" > -- calculate sum(x for x^2 in 1..n) > print(fun.range(n):map(function(x) return x^2 end):reduce(operator.add, 0)) 328350
Lua Fun takes full advantage of the innovative tracing JIT compiler to achieve transcendental performance on nested functional expressions. Functional compositions and high-order functions can be translated into efficient machine code. Can you believe it? Just try to run the example above withluajit -jdumpand see what happens:
-- skip some initilization code -- ->LOOP: 0bcaffd0 movaps xmm5, xmm7 0bcaffd3 movaps xmm7, xmm1 0bcaffd6 addsd xmm7, xmm5 0bcaffda ucomisd xmm7, xmm0 0bcaffde jnb 0x0bca0024 ->5 0bcaffe4 movaps xmm5, xmm7 0bcaffe7 mulsd xmm5, xmm5 0bcaffeb addsd xmm6, xmm5 0bcaffef jmp 0x0bcaffd0 ->LOOP ---- TRACE 1 stop -> loop
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!