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
- Lua samples
- AccessHiddenTables - Access hidden tables using metatables (functions)
- AsciiMenu - console-based menu system, illustrating menu definition via chain of '.' calls (design pattern)
- BaseSixtyFour - Base64 encode/decode (functions)
- 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)
- ClassesAndMethodsExample - a simple class implementation with inheritance (module)
- CommandLineModule - command line parsing
- ComparisonByValue - make a table compare its keys by value rather than by reference
- CompleteWithReadline - completion engine for standalone interpreter (functions)
- ComplexNumbers - simple attempt to handle complex numbers (functions)
- CopyTable - return deep copy of table (function)
- CopasExample - TCP socket server utilizing Copas library (examples)
- CoroutinesAsConnectionHandlers - scaffolding for handling multiple sockets using coroutines (design pattern)
- CoroutinesAsEventHandlers - scaffolding for handling event responders (design pattern)
- CppStreamChunkReader - lua_load chunk reader for any C++ std::istream (function)
- CppMetaprogramming - C++ metaprogramming using Lua (design pattern, library)
- DataDumper - dumps nearly any data into a Lua chunk string (function)
- DateFormattingFunctions - format date into various date string format (functions)
- DayOfWeekAndDaysInMonthExample - calculate number of days in month or day of week in calendar day (functions)
- DisplayCalendarInHtml - creates calendar in HTML (function)
- DirTreeIterator - iterator function for recursive file/directory traversal (function)
- ExtensionProposal - extended os and io API (modules)
- FileInputOutput - input/output file utility functions (functions)
- FileGlob - matching filenames with glob expressions (function)
- FiniteStateMachine - finite state machine (FSMs) (design pattern)
- FormattingNumbers - format numbers in monetary format (function)
- 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)
- FunWithCoroutines - tricks using coroutines: reverse string and control inversion (design patterns)
- FunWithTables - example that shows the expressive power of Lua's tables
- GdThumbnail - thumbnailer using lua-gd (e.g. for web galleries or similar)
- GeneralizedPairsAndIpairs -
next, pairs, and ipairs reimplemented to call metamethods (design pattern and functions)
- GettingTheTitleFromHtmlFiles - parse title tag from HTML files (command-line program)
- HammingNumbers - enumerate hamming numbers (function)
- HammingNumbersVariant - enumerate hamming numbers, with lazy list (function)
- HashedModulesLoader - a module loader that verifies the module cryptographic hash prior to loading
- HexDump - dump a string in typical 'hex dump' format
- 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)
- LpegRecipes - examples of using LPeg for parsing (examples)
- LuaCarp - ensures
error function is passed correct level, like Perl Carp (module)
- LuaClassesWithMetatable - creating classes via metatables (tutorial and code) (editor note: possibly move)
- LuaCompilerInLua - luac compiler implemented in Lua (program)
- LuaModulesLoader - a Lua implementation of the standard Lua module loader
- LuaPrintf - emulation of printf string formatting (function)
- LuaProjectListToXml - read list of Lua projects from Lua website and output as XML (program)
- LuaRecipes - useful code snippets and functions (design patterns)
- LuaSorting - a sample shellsort implementation in pure Lua (function)
- LuaToHtml - convert (htmlize) Lua source to HTML (command-line program)
- LuaTypeChecking - implementing type checking in Lua (design patterns and code)
- LuaUnit - LuaUnit unit testing framework (module)
- LuaXml - handling XML with various libraries (sample code)
- LxpTree - constructs an XML tree from a string/file using LazyKit (example code)
- MakingLuaLikePhp - PHP-like functions implemented in Lua (functions)
- MakingLuaLikeRuby - Simulating some of the semantics of Ruby in Lua (design pattern)
- MultipleInheritanceClasses - A multiple-inheritance class system including shared (virtual) inheritance (module)
- MutableFunctions - using functions to simulate the semantics of tables (design pattern)
- NextMetamethodForIndexTable -
next()-like function for iterating table against table __index (function and design pattern)
- ObjectProperties - implementing object properties - obj.field (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)
- OutputLuaTableToHtmlFile - converts a Lua table into an HTML file (function)
- PersistentTablesSqlite - persist Lua tables in SQLite database (sample code)
- PickleTable - Lua table serialization (module)
- ProfilerInLua - very simple Lua profiler (module)
- ProxyBasedEncapsulation - encapsulation of objects via proxy tables (function/design pattern)
- RandomSample - random permutation/combination via lazy table approach (design pattern)
- RandomStrings - generate random strings from defined char sets (function)
- ReadDefinedChunks - read delimited chunks from an file (function)
- ReadOnlyTables - making a table read-only via metamethods (function and design pattern)
- ReadWriteFormat - a way to pack / unpack integer values into a binary string (functions)
- RecursiveDataTypes - handling recursive data types (design pattern and tutorial)
- RecursiveReadOnlyTables - making a nested table read-only via metamethods (function and design pattern)
- ResourceAcquisitionIsInitialization - automatically releasing acquired resources on scope exit, RAII (module,pattern)
- SaveTableToFile - Save a table to file/string (functions)
- SetOperations - test for set or list membership (design pattern)
- SetVariablesAndTablesWithFunction - recursively build and set table fields with dot notation--e.g. "x.y.z" (function and design pattern)
- SimpleFit - curve fitting (module)
- SimpleLuaClasses - class declaration - a metatable approach (tutorial, design pattern, and function)
- SimpleLuaPreprocessor - macro processor (function)
- LuaMatrix - basic matrix functions (module)
- SimpleRound - round number to n decimal places (functions)
- SimpleStack - stack implemented via table (class)
- SimpleStats - some simple statistics routes for tables.
- SimpleTuples - immutable interned 2-tuples (module)
- SlightlyLessSimpleLuaPreprocessor - macro processor (function/program)
- SortedIteration - iterating a table in order by keys (function)
- SortedIterationSimple - iterating a table in order by keys (function)
- 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)
- SwitchStatement - emulating a C-style switch statement in Lua (design pattern)
- TextTemplate - macro processor (module)
- TimeZone - obtaining portable timezone string in form +hhmm or -hhmm (function)
- VarExpand - bash-style inline variable and code expansion
- YetAnotherClassImplementation - a complex class implementation with inheritance (module)
- Lua C API samples
-
- See also
- Full Programs
- LuaShell - an incomplete sample shell which understands Lua syntax
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: OptimisationTips, OptimizedSampleCode
FindPage · RecentChanges · preferences
edit · history
Last edited September 2, 2008 6:11 pm GMT (diff)