Lua language: how to use something like python list comprehension

I'm new to Lua, and I was wondering if there is any compact way to define list , a metatable in Lua, like in Python:

 lis = [i for i in set if i>0] 

Any example is appreciated.

+6
source share
1 answer

Lua does not have its own list

However, as @joachim noted, you can use some hacks to achieve it.

 local comp = require 'comprehension' . new() comp 'table(v,k for k,v in pairs(_1))' {[3]=5, [5]=7} 

This will give:

 {[5]=3, [7]=5} 

Note that comprehension does not work in simple version 5.2.x Lua. This requires the Penlight Lua libraries: http://stevedonovan.github.com/Penlight/api/index.html

You can also use MetaLua or LuaMacros

+2
source

All Articles