dseeni / lua-list-comprehension Goto Github PK
View Code? Open in Web Editor NEWThis project forked from davidm/lua-list-comprehension
list comprehensions in pure Lua
Home Page: lua-users.org/wiki/ListComprehensions
This project forked from davidm/lua-list-comprehension
list comprehensions in pure Lua
Home Page: lua-users.org/wiki/ListComprehensions
== Description == List comprehensions [1] provide concise syntax for building lists in mathematical set-builder notation. A number of programming languages (e.g. Haskell and Python) provide built-in support for list comprehensions. Lua does not; however, there are are ways to implement it in Lua. Unlike some other approaches, the following approach implements list comprehensions in pure Lua as a library (without patching or token filters). It uses a trick with dynamic code generation (loadstring) and caching somewhat analogous to ShortAnonymousFunctions [2]. This library has been incorporated into Penlight [4]. == Project Page == http://lua-users.org/wiki/ListComprehensions == Examples == local comp = require 'comprehension' . new() comp 'x^2 for x' {2,3} --> {2^2,3^2} comp 'x^2 for _,x in ipairs(_1)' {2,3} --> {2^2,3^2} comp 'x^2 for x=_1,_2' (2,3) --> {2^2,3^2} comp 'sum(x^2 for x)' {2,3} --> 2^2+3^2 comp 'max(x*y for x for y if x<4 if y<6)' ({2,3,4}, {4,5,6}) --> 3*5 comp 'table(v,k for k,v in pairs(_1))' {[3]=5, [5]=7} --> {[5]=3, [7]=5} == Run-Time Behavior == To illustrate the run-time characteristics, consider the following code: comp 'sum(x^2 for x if x % 2 == 0)' That gets code generated to this Lua function: local __in1 = ... local __result = ( 0 ) for __idx1 = 1, #__in1 do local x = __in1[__idx1] if x % 2 == 0 then __result = __result + ( __x^2 ) end end return __result Note that no intermediate lists are built. The code efficiently avoids memory allocations (apart from the allocation of the function itself, but that is done only on the first invocation due to caching/memoization). Also, no global variables are referenced. == Dependencies == This module depends on LuaBalanced [4]. == Author == (c) 2008 David Manura. Licensed under the same terms as Lua (MIT license). == References == [1] http://en.wikipedia.org/wiki/List_comprehension [2] http://lua-users.org/wiki/ShortAnonymousFunctions [3] http://penlight.luaforge.net/ [4] http://lua-users.org/wiki/LuaBalanced
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.