Collections: Difference between revisions
Content added Content deleted
(Add Factor example) |
No edit summary |
||
Line 1,757: | Line 1,757: | ||
It is idiomatic in Lua to represent a Set data structure with a table of keys to the true value. |
It is idiomatic in Lua to represent a Set data structure with a table of keys to the true value. |
||
=={{header|M2000 Interpreter}}== |
|||
===Ordered List (array)=== |
|||
<lang M2000 Interpreter> |
|||
Module Arr { |
|||
\\ array as tuple |
|||
A=(1,2,3,4,5) |
|||
Print Array(A,0)=1 |
|||
Print A |
|||
\\ add two arrays |
|||
A=Cons(A, (6,)) |
|||
Print Len(A)=6 |
|||
Print A |
|||
\\ arrays may have arrays, inventories, stacks as items |
|||
A=((1,2,3),(4,5,6)) |
|||
Print Array(Array(A, 0),2)=3 |
|||
} |
|||
Arr |
|||
<lang M2000 Interpreter> |
|||
===Ordered List (stack)=== |
|||
A stack may have values inventories,arrays, stacks, groups |
|||
<lang M2000 Interpreter> |
|||
Module CheckStack { |
|||
\\ ordered collection: Stack |
|||
\\ we can add values to top or bottom, |
|||
\\ we can move values to and from top |
|||
A=Stack:=100,300,600,800,900 |
|||
Print StackItem(A, 2)=300, Len(A)=5 |
|||
Stack A { |
|||
\\ push to bottom (or end) |
|||
Data 2000, 4000 |
|||
} |
|||
Print StackItem(A, 7)=4000, Len(A)=7 |
|||
Stack A { |
|||
\\ pop from top |
|||
Read X, Y |
|||
Print X=100, Y=300 |
|||
} |
|||
Print StackItem(A,5)=4000, Len(A)=5 |
|||
Stack A { |
|||
\\ push to top |
|||
Push 2, 1 |
|||
Stack ' display stack items |
|||
} |
|||
\\ we can make a new stack merging other stacks |
|||
A=Stack(A, stack:=5000,6000,7000) |
|||
Print Len(A)=10 |
|||
Stack A { |
|||
Shift 1,-Len(A) ' Reverse order |
|||
Stack ' Display |
|||
} |
|||
Stack A {Drop 8} |
|||
Print Len(A)=2 |
|||
Flush ' empty current stack |
|||
Stack A ' dump A to current stack |
|||
Print Stack.Size=2, Len(A)=0 |
|||
} |
|||
CheckStack |
|||
<lang M2000 Interpreter> |
|||
===Inventories as Maps=== |
|||
An Inventory may have values inventories,arrays, stacks, groups |
|||
<lang M2000 Interpreter> |
|||
Module Maps { |
|||
\\ Inventory as pairs of keys/values |
|||
\\ keys has to be unique |
|||
\\ Empty string "" can be used as key |
|||
\\ Search, Add and Delete in O(1) |
|||
\\ if we use delete we lost the order |
|||
\\ keys can be numbers or strings, either can exist in same inventory. Values can be anything (including objects) |
|||
\\ 0 can be used for string |
|||
\\ Keys must be unique |
|||
\\ a variable which hold an inventory is a pointer type |
|||
Inventory A=10:="A",20:="B",40:="C" |
|||
Print A$(10)="A", A$("20")="B" |
|||
\\ split search from retrieval, using key one time |
|||
If Exist(A,40) Then Print Eval$(A)="C" |
|||
k=Each(A) |
|||
While k { |
|||
\\ print keys as strings and values |
|||
Print Eval$(k, k^), Eval$(k) |
|||
} |
|||
\\ We can use Sort to sort as numbers or text |
|||
Append A, 5:="First" |
|||
Sort A as number |
|||
\\ Print can print an inventory using columns |
|||
Print A ' First A B C |
|||
Sort A as text |
|||
Print A ' A B C First |
|||
} |
|||
Maps |
|||
</lang> |
|||
===Inventories as Sets=== |
|||
<lang M2000 Interpreter> |
|||
Module Sets { |
|||
\\ Inventory as set of keys |
|||
\\ keys has to be unique |
|||
\\ Empty string "" can be used as key |
|||
\\ Search, Add and Delete in O(1) |
|||
\\ if we use delete we lost the order |
|||
\\ keys can be numbers or strings, either can exist in same inventory |
|||
\\ 0 can be used for string |
|||
\\ Keys must be unique |
|||
\\ a variable which hold an inventory is a pointer type |
|||
Inventory A=10,20,40 |
|||
If Exist(A,20) Then Print Eval(A)=20 |
|||
k=Each(A) |
|||
While k { |
|||
\\ print keys as strings and value same as key (as number here) |
|||
Print Eval$(k, k^), Eval(k) |
|||
} |
|||
\\ sort is a Quick sort |
|||
Sort Descending A as number |
|||
Print A ' 40 20 10 |
|||
\\ For no unique keys |
|||
\\ we can't delete from anywhere. |
|||
\\ we can drop some keys from the end only |
|||
\\ Exist() move internal index to last of the same key |
|||
\\ we can give values also (make it as Map) |
|||
Inventory Queue B=1,1,1,2,2,6,10 |
|||
Drop B 3 |
|||
Print B ' prints 1 1 1 2 |
|||
\\ sort is an insertion sort (stable) |
|||
} |
|||
Sets |
|||
</lang> |
|||
=={{header|Maple}}== |
=={{header|Maple}}== |
||
Defining lists: |
Defining lists: |