Following is a list of pages with sample code. Types of code include standalone reusable functions/classes, reusable modules, example code illustrating the use of other modules, tutorials on using other modules, full programs, and design patterns.
Lua 5.x
- Metatables - Classes
- Metatables - Other Design Patterns
- AccessHiddenTables - Access hidden tables using metatables (functions)
- ComparisonByValue - make a table compare its keys by value rather than by reference
- FuncTables - memoization of functions using callable tables (design pattern, code, and tutorial) (editor note: possibly move)
- FunctionalTuples - implementing tuples in terms functions (design pattern and tutorial) (editor note: possibly move)
- MutableFunctions - using functions to simulate the semantics of tables (design pattern)
- ObjectProperties - implementing object properties - obj.field (function and design pattern)
- ReadOnlyTables - making a table read-only via metamethods (function and design pattern)
- OrderedAssociativeTable - iterate over a table using a sorted index (function and design pattern)
- OrderedTable - iterate over tables in the order of key insertion (function and design pattern)
- OrderedTableSimple - iterate over tables in the order of key insertion (function and design pattern)
- ProxyBasedEncapsulation - encapsulation of objects via proxy tables (function/design pattern)
- RecursiveDataTypes - handling recursive data types (design pattern and tutorial)
- RecursiveReadOnlyTables - making a nested table read-only via metamethods (function and design pattern)
- SetVariablesAndTablesWithFunction - recursively build and set table fields with dot notation--e.g. "x.y.z" (function and design pattern)
- GeneralizedPairsAndIpairs -
next
, pairs
, and ipairs
reimplemented to call metamethods (design pattern and functions)
- NextMetamethodForIndexTable -
next()
-like function for iterating table against table __index
(function and design pattern)
- AsciiMenu - console-based menu system, illustrating menu definition via chain of '.' calls (design pattern)
- OverloadedFunctions - function dispatch by argument types which is defined implicitly by assignment into a virtual table
- StrictStructs - create tables from a template, with checking for unknown fields
- Data Structures
- Algorithms/Traversal
- BinaryInsert - inserts a value into a sorted array via a binary search algorithm (function)
- BinarySearch - searches for a value in a sorted array via a binary search algorithm (function)
- InterpolatingSearch - searches for a value in a sorted array via an interpolating search algorithm (function)
- LazySort - sort only as much of a table as is necessary (function and tutorial)
- LuaSorting - a sample shellsort implementation in pure Lua (function)
- SortedIteration - iterating a table in order by keys (function)
- SortedIterationSimple - iterating a table in order by keys (function)
- RangeIterator - a 'range' iterator function that acts like basic for loops
- MultipleKeyIndexing - indexing by multiple keys implemented as a hash tree (module)
- String/Table Manipulation
- SplitJoin - Perl-like string split/join functions (functions)
- StandardLibraries - os, io, getopt, string, regex, xml, list/set/table, object, and math modules (modules)
- StringInterpolation - interpolating variables into strings in Lua (functions and design patterns)
- StringRecipes - solutions to various common string manipulation needs (example code)
- VarExpand - bash-style inline variable and code expansion
- LuaRecipes - useful code snippets and functions (design patterns)
- LuaPrintf - emulation of printf string formatting (function)
- FormattingNumbers - format numbers in monetary format (function)
- EnhancedFileLines - functions for portably handling '\r\n' chars (functions)
- StringIndexing - easy access to characters and substrings
- Preprocessing/Templates/Parsing
- Serialization
- Mathematics/Numbers
- FloatSum - accurate floating point summation
- SimpleRound - round number to n decimal places (functions)
- SimpleStats - some simple statistics routes for tables.
- LuaMatrix - basic matrix functions (module)
- SimpleFit - curve fitting (module)
- HammingNumbers - enumerate hamming numbers (function)
- HammingNumbersVariant - enumerate hamming numbers, with lazy list (function)
- ComplexNumbers - simple attempt to handle complex numbers (functions)
- IntegerDomain - calculates the actual limits for integer numbers which can be exactly represented by Lua (function)
- UnitConversion - converting intervals of time (can be extended to any non-uniform system of measurement, such as Imperial units)
- HyperbolicFunctions - a pure Lua implementation for the hyperbolic trigonometric functions
- Encoding/Cryptography/Compression
- Filesystem
- Coroutines
- Error Handling
- LuaCarp - ensures
error
function is passed correct level, like Perl Carp (module)
- Testing / Types Checking / Profiling
- Command Line
- Module System
- Time/Date
- XML
- LuaXml - handling XML with various libraries (sample code)
- LxpTree - constructs an XML tree from a string/file using LazyKit (example code)
- LuaProjectListToXml - read list of Lua projects from Lua website and output as XML (program)
- Web/HTML
- Device I/O
- Techniques / Design Patterns / Code Structure
- Other
- LuaCompilerInLua - reimplements the Lua compiler front-end (luac.c) in Lua (program)
- LuaInterpreterInLua - reimplements the Lua interpreter (lua.c) in Lua (program)
- RandomSample - random permutation/combination via lazy table approach (design pattern)
- RandomStrings - generate random strings from defined char sets (function)
- DemoScripts - random examples to showcase Lua
- StlToObj - convert ASCII STL files to OBJ (Wavefront) files with optional coloring
- LuaCad - Simple OpenSCAD wrapper for Lua in Windows
- Lua C API samples
Lua 4.x
These should be upgraded to the latest Lua version.
If you are thinking of contributing source code the SourceCodeFormatter may be helpful to you.
See also
RecentChanges · preferences
edit · history
Last edited February 14, 2020 5:59 pm GMT (diff)