lua-users home
lua-l archive

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


It was thus said that the Great Daurnimator once stated:
> 
> Additionally, your code is only 5 lines of code that sets a global
> object's metatable....
> https://bitbucket.org/TeamSoni/cratable/src/73f6028ef90ee67ced148d894fd93a5333e9a8bd/cratable.lua?at=master&fileviewer=file-view-default#cratable.lua-24
> I'm not sure why this is something that deserves to be its own module.

  A short module can have valid semantic meaning that belies the trivialness
of the module (leftpad notwithstanding [1]).  I have several private modules
that are very short.  For instance, I have this one (org.flummux.editor
[2]):

	local sys = require "org.conman.sys"
	local os  = require "os"
	
	return os.getenv("VISUAL")
	    or os.getenv("EDITOR")
	    or sys.PATHS.vi
	    or "/bin/vi"

  And another one that implements the Y-combinator in Lua (org.flummux.Y)
[3]:

	return function(f)
	  local function g(...) return f(g,...) end
	  return g
	end

  I also have several other ones that consist of a single function (but are
longer than a few lines.  I'm still debating how I feel about this---on the
one hand, writing such hperfocused modules allows one to just include what
is needed and prevent a much larger attack surface for exploits.  It also
prevents a module with a mishmash of functionality from being created.

  On the other hand, it does seem strange to have a module of a few lines.

  -spc

[1]	http://www.haneycodes.net/npm-left-pad-have-we-forgotten-how-to-program/

[2]	Yes, I even namespace my own private modules.  The modules under the
	org.flummux domain are experimental, yet to be published modules.

[3]	Basically, this allows you to do recursion with an anonymous
	function.