Giter VIP home page Giter VIP logo

lua-tuple's Introduction

Lua-Tuple

In-mutable and interned tuple table for Lua. Tuples can be sorted lexicographically and concatenated by default.

Installation

Copy the file tuple.lua to your LUA_PATH.

Use

Lua-Tuple allows to declare in-mutable and interned tuples in Lua. Creation of tuples is a time consuming operation due to internalization, however, they can be used as keys of Lua tables thanks to that. Tuples can store numbers, strings and tables, nevertheless, table values will be converted into tuples recursively. Tables can't have reference loops, otherwise the tuple constructor will crash in a stack overflow.

> tuple = require "tuple"
> t1 = tuple(3,2,1)
> print(t1)
tuple{ 3, 2, 1 }
> -- tuple from a table
> t2 = tuple{3,2,1}
> print(t1 == t2)
true
> -- tuples with one element are unpacked by default
> print( tuple(3) )
3
> -- but it can be forced by using table constructor
> print( tuple{3} )
tuple{ 3 }
> t3 = tuple(t1,{4,5,t1})
> print(t3)
tuple{ tuple{ 3, 2, 1 }, tuple{ 4, 5, tuple{ 3, 2, 1 } } }
> aux = { [t1] = "t1", [t3] = "t3" }
> print(aux)
table: 0x26fdc70
> print(aux[t1])
t1
> print(aux[t3])
t3
> loop_table = {4,5}
> table.insert(loop_table, loop_table)
> fail = tuple(loop_table)
./tuple.lua:116: stack overflow
stack traceback:
	./tuple.lua:117: in function 'tuple_constructor'
	./tuple.lua:134: in function 'tuple'
	./tuple.lua:118: in function 'tuple_constructor'
	./tuple.lua:134: in function 'tuple'
	./tuple.lua:118: in function 'tuple_constructor'
	./tuple.lua:134: in function 'tuple'
	./tuple.lua:118: in function 'tuple_constructor'
	./tuple.lua:134: in function 'tuple'
	...
	./tuple.lua:134: in function 'tuple'
	./tuple.lua:118: in function 'tuple_constructor'
	./tuple.lua:134: in function 'tuple'
	./tuple.lua:118: in function 'tuple_constructor'
	./tuple.lua:134: in function 'tuple'
	./tuple.lua:118: in function 'tuple_constructor'
	./tuple.lua:134: in function 'tuple'
	./tuple.lua:118: in function 'tuple_constructor'
	./tuple.lua:134: in function 'tuple'
	stdin:1: in main chunk
	[C]: in ?

lua-tuple's People

Contributors

pakozm avatar

Stargazers

Jonathan McHugh avatar cmo avatar Peter Melnichenko avatar Roland avatar  avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.