lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Nov 18, 2013, at 12:44 PM, Sir Pogsalot <sir.pogsalot@gmail.com> wrote:

> I want to thank Petite Abeille for being the most contributory, the 2nd approach was creative.  I would probably stick with the from() I posted, but I thought your code was really thought-provoking and I appreciate you putting more code to this thread. :]

More insanity for your entertainment :P


local min, max = import( 'math' )
local tconcat, tpack, tunpack = import( 'table', 't' )

print( min, min( 1, 2 ) )
print( max, max( 1, 2 ) )
print( tconcat, tconcat( { 'a', 'b', 'c' } ) )
print( tpack, tpack( 1, 2, 3 ) )
print( tunpack, tunpack( { 1, 2, 3 } ) )



—8<—
local function import( aName, aPrefix )
  local function Source()
    local aLine = debug.getinfo( 3, 'l' ).currentline
    local aSource = debug.getinfo( 3, 'S' ).source
    local aFile = io.open( aSource:sub( 2 ), 'r' )

    if aFile then
      local anIndex = 0

      for anIndex = 1, aLine - 1 do
        aFile:read( '*l' )
      end

      return aFile:read( '*l' )
    end
  end

  local aModule = require( aName )
  local aPrefix = aPrefix or ''
  local aSource = Source()
  local aList = {}

  for aKey in aSource:sub( aSource:find( 'local ' ) + 6, aSource:find( '=' ) - 1 ):gmatch( '([^,%s]+)' ) do
    aKey = aKey:sub( aPrefix:len() + 1 )
    aList[ #aList + 1 ] = assert( aModule[ aKey ], ( 'unknown key %q in module %q' ):format( aKey, aName ) )
  end

  return table.unpack( aList )
end
—>8—