Updated doc.
This commit is contained in:
parent
99c379870f
commit
1a06de84dd
25
README.md
25
README.md
@ -55,25 +55,22 @@ DogSingleton = immutable(0, {name="DogSingleton", bark=function(self) print("woo
|
|||||||
DogSingleton():bark()
|
DogSingleton():bark()
|
||||||
```
|
```
|
||||||
|
|
||||||
## Tuple recipe
|
## Tuples
|
||||||
With immutable tables, it's pretty simple to emulate Python-like tuples:
|
If no field names are passed in, `immutable()` defaults to creating immutable tables that behave like Python's tuples:
|
||||||
```lua
|
```lua
|
||||||
local tuple_classes = {}
|
local Tuple = immutable()
|
||||||
Tuple = function(...)
|
local t0 = Tuple()
|
||||||
local n = select('#', ...)
|
local t1 = Tuple(1,2)
|
||||||
if not tuple_classes[n] then
|
local t2 = Tuple(1,2,3,4,5)
|
||||||
tuple_classes[n] = immutable(n)
|
assert(t0 == Tuple())
|
||||||
end
|
assert(({[t1]='yep'})[Tuple(1,2)])
|
||||||
return tuple_classes[n](...)
|
assert(tostring(Tuple(1,2)) == "(1, 2)")
|
||||||
end
|
|
||||||
assert(Tuple(5,6,7) == Tuple(5,6,7))
|
|
||||||
assert(tostring(Tuple(8,9)) == "(8, 9)")
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## General purpose immutable table recipe
|
## General purpose immutable table recipe
|
||||||
Using the tuple recipe above, you can make a function that returns an immutable version
|
Using tuples, you can make a function that returns an immutable version of a table with arbitrary keys, though it is a little bit hacky.
|
||||||
of a table with arbitrary keys.
|
|
||||||
```lua
|
```lua
|
||||||
|
local Tuple = immutable()
|
||||||
local immutable_classes = {}
|
local immutable_classes = {}
|
||||||
Immutable = function(t)
|
Immutable = function(t)
|
||||||
local keys = {}
|
local keys = {}
|
||||||
|
Loading…
Reference in New Issue
Block a user