GithubHelp home page GithubHelp logo

dseeni / lua-list-comprehension Goto Github PK

View Code? Open in Web Editor NEW

This project forked from davidm/lua-list-comprehension

0.0 0.0 0.0 104 KB

list comprehensions in pure Lua

Home Page: lua-users.org/wiki/ListComprehensions

Lua 100.00%

lua-list-comprehension's Introduction

== 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

lua-list-comprehension's People

Contributors

davidm avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.