Software /
code /
prosody
Annotate
util/array.lua @ 1905:e3e0a17e0b33
util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Mon, 05 Oct 2009 14:38:04 +0100 |
parent | 1522:569d58d21612 |
child | 1915:e9d5406caf8c |
rev | line source |
---|---|
1522
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
1373
diff
changeset
|
1 -- Prosody IM |
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
1373
diff
changeset
|
2 -- Copyright (C) 2008-2009 Matthew Wild |
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
1373
diff
changeset
|
3 -- Copyright (C) 2008-2009 Waqas Hussain |
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
1373
diff
changeset
|
4 -- |
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
1373
diff
changeset
|
5 -- This project is MIT/X11 licensed. Please see the |
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
1373
diff
changeset
|
6 -- COPYING file in the source package for more information. |
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
1373
diff
changeset
|
7 -- |
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
1373
diff
changeset
|
8 |
1905
e3e0a17e0b33
util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
9 local t_insert, t_sort, t_remove, t_concat |
e3e0a17e0b33
util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
10 = table.insert, table.sort, table.remove, table.concat; |
918 | 11 |
1905
e3e0a17e0b33
util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
12 local array = {}; |
e3e0a17e0b33
util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
13 local array_base = {}; |
e3e0a17e0b33
util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
14 local array_methods = {}; |
e3e0a17e0b33
util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
15 local array_mt = { __index = array_methods, __tostring = function (array) return array:concat(", "); end }; |
e3e0a17e0b33
util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
16 |
920
e302cbc9d036
util.array: Expose array.* functions, to be used for unwrapped arrays
Matthew Wild <mwild1@gmail.com>
parents:
918
diff
changeset
|
17 local function new_array(_, t) |
918 | 18 return setmetatable(t or {}, array_mt); |
19 end | |
20 | |
1373
120275376bbb
util.array: Add support for + operator to create a new array from two arrays joined
Matthew Wild <mwild1@gmail.com>
parents:
1372
diff
changeset
|
21 function array_mt.__add(a1, a2) |
120275376bbb
util.array: Add support for + operator to create a new array from two arrays joined
Matthew Wild <mwild1@gmail.com>
parents:
1372
diff
changeset
|
22 local res = new_array(); |
120275376bbb
util.array: Add support for + operator to create a new array from two arrays joined
Matthew Wild <mwild1@gmail.com>
parents:
1372
diff
changeset
|
23 return res:append(a1):append(a2); |
120275376bbb
util.array: Add support for + operator to create a new array from two arrays joined
Matthew Wild <mwild1@gmail.com>
parents:
1372
diff
changeset
|
24 end |
120275376bbb
util.array: Add support for + operator to create a new array from two arrays joined
Matthew Wild <mwild1@gmail.com>
parents:
1372
diff
changeset
|
25 |
920
e302cbc9d036
util.array: Expose array.* functions, to be used for unwrapped arrays
Matthew Wild <mwild1@gmail.com>
parents:
918
diff
changeset
|
26 setmetatable(array, { __call = new_array }); |
e302cbc9d036
util.array: Expose array.* functions, to be used for unwrapped arrays
Matthew Wild <mwild1@gmail.com>
parents:
918
diff
changeset
|
27 |
1905
e3e0a17e0b33
util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
28 function array_base.map(outa, ina, func) |
e3e0a17e0b33
util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
29 for k,v in ipairs(ina) do |
e3e0a17e0b33
util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
30 outa[k] = func(v); |
918 | 31 end |
1905
e3e0a17e0b33
util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
32 return outa; |
918 | 33 end |
34 | |
1905
e3e0a17e0b33
util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
35 function array_base.filter(outa, ina, func) |
e3e0a17e0b33
util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
36 for k,v in ipairs(ina) do |
918 | 37 if func(v) then |
1905
e3e0a17e0b33
util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
38 outa:push(v); |
918 | 39 end |
40 end | |
1905
e3e0a17e0b33
util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
41 return outa; |
918 | 42 end |
43 | |
1905
e3e0a17e0b33
util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
44 function array_base.sort(outa, ina, ...) |
e3e0a17e0b33
util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
45 if ina ~= outa then |
e3e0a17e0b33
util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
46 outa:append(ina); |
e3e0a17e0b33
util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
47 end |
e3e0a17e0b33
util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
48 t_sort(outa, ...); |
e3e0a17e0b33
util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
49 return outa; |
e3e0a17e0b33
util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
50 end |
918 | 51 |
1905
e3e0a17e0b33
util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
52 --- These methods only mutate |
e3e0a17e0b33
util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
53 function array_methods:random() |
918 | 54 return self[math.random(1,#self)]; |
55 end | |
56 | |
1905
e3e0a17e0b33
util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
57 function array_methods:shuffle(outa, ina) |
918 | 58 local len = #self; |
59 for i=1,#self do | |
60 local r = math.random(i,len); | |
61 self[i], self[r] = self[r], self[i]; | |
62 end | |
1372
3b13bb57002e
util.array: Make array:reverse() and array:shuffle() return the array to allow chaining
Matthew Wild <mwild1@gmail.com>
parents:
1371
diff
changeset
|
63 return self; |
918 | 64 end |
65 | |
1905
e3e0a17e0b33
util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
66 function array_methods:reverse() |
922
0e45234360cd
util.array: Add :reverse() method
Matthew Wild <mwild1@gmail.com>
parents:
920
diff
changeset
|
67 local len = #self-1; |
0e45234360cd
util.array: Add :reverse() method
Matthew Wild <mwild1@gmail.com>
parents:
920
diff
changeset
|
68 for i=len,1,-1 do |
0e45234360cd
util.array: Add :reverse() method
Matthew Wild <mwild1@gmail.com>
parents:
920
diff
changeset
|
69 self:push(self[i]); |
0e45234360cd
util.array: Add :reverse() method
Matthew Wild <mwild1@gmail.com>
parents:
920
diff
changeset
|
70 self:pop(i); |
0e45234360cd
util.array: Add :reverse() method
Matthew Wild <mwild1@gmail.com>
parents:
920
diff
changeset
|
71 end |
1372
3b13bb57002e
util.array: Make array:reverse() and array:shuffle() return the array to allow chaining
Matthew Wild <mwild1@gmail.com>
parents:
1371
diff
changeset
|
72 return self; |
922
0e45234360cd
util.array: Add :reverse() method
Matthew Wild <mwild1@gmail.com>
parents:
920
diff
changeset
|
73 end |
0e45234360cd
util.array: Add :reverse() method
Matthew Wild <mwild1@gmail.com>
parents:
920
diff
changeset
|
74 |
1905
e3e0a17e0b33
util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
75 function array_methods:append(array) |
1371
9e45bdf55353
util.array: Add array:append() method, to append a new array to an existing one
Matthew Wild <mwild1@gmail.com>
parents:
1027
diff
changeset
|
76 local len,len2 = #self, #array; |
9e45bdf55353
util.array: Add array:append() method, to append a new array to an existing one
Matthew Wild <mwild1@gmail.com>
parents:
1027
diff
changeset
|
77 for i=1,len2 do |
9e45bdf55353
util.array: Add array:append() method, to append a new array to an existing one
Matthew Wild <mwild1@gmail.com>
parents:
1027
diff
changeset
|
78 self[len+i] = array[i]; |
9e45bdf55353
util.array: Add array:append() method, to append a new array to an existing one
Matthew Wild <mwild1@gmail.com>
parents:
1027
diff
changeset
|
79 end |
9e45bdf55353
util.array: Add array:append() method, to append a new array to an existing one
Matthew Wild <mwild1@gmail.com>
parents:
1027
diff
changeset
|
80 return self; |
9e45bdf55353
util.array: Add array:append() method, to append a new array to an existing one
Matthew Wild <mwild1@gmail.com>
parents:
1027
diff
changeset
|
81 end |
9e45bdf55353
util.array: Add array:append() method, to append a new array to an existing one
Matthew Wild <mwild1@gmail.com>
parents:
1027
diff
changeset
|
82 |
1905
e3e0a17e0b33
util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
83 array_methods.push = table.insert; |
e3e0a17e0b33
util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
84 array_methods.pop = table.remove; |
e3e0a17e0b33
util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
85 array_methods.concat = table.concat; |
e3e0a17e0b33
util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
86 array_methods.length = function (t) return #t; end |
e3e0a17e0b33
util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
87 |
e3e0a17e0b33
util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
88 --- These methods always create a new array |
1027
fe2e3d3dba6a
util.array: Add array.collect() to collect results from iterators into an array, and use module() to correctly set the module name
Matthew Wild <mwild1@gmail.com>
parents:
922
diff
changeset
|
89 function array.collect(f, s, var) |
fe2e3d3dba6a
util.array: Add array.collect() to collect results from iterators into an array, and use module() to correctly set the module name
Matthew Wild <mwild1@gmail.com>
parents:
922
diff
changeset
|
90 local t, var = {}; |
fe2e3d3dba6a
util.array: Add array.collect() to collect results from iterators into an array, and use module() to correctly set the module name
Matthew Wild <mwild1@gmail.com>
parents:
922
diff
changeset
|
91 while true do |
fe2e3d3dba6a
util.array: Add array.collect() to collect results from iterators into an array, and use module() to correctly set the module name
Matthew Wild <mwild1@gmail.com>
parents:
922
diff
changeset
|
92 var = f(s, var); |
fe2e3d3dba6a
util.array: Add array.collect() to collect results from iterators into an array, and use module() to correctly set the module name
Matthew Wild <mwild1@gmail.com>
parents:
922
diff
changeset
|
93 if var == nil then break; end |
fe2e3d3dba6a
util.array: Add array.collect() to collect results from iterators into an array, and use module() to correctly set the module name
Matthew Wild <mwild1@gmail.com>
parents:
922
diff
changeset
|
94 table.insert(t, var); |
fe2e3d3dba6a
util.array: Add array.collect() to collect results from iterators into an array, and use module() to correctly set the module name
Matthew Wild <mwild1@gmail.com>
parents:
922
diff
changeset
|
95 end |
fe2e3d3dba6a
util.array: Add array.collect() to collect results from iterators into an array, and use module() to correctly set the module name
Matthew Wild <mwild1@gmail.com>
parents:
922
diff
changeset
|
96 return setmetatable(t, array_mt); |
fe2e3d3dba6a
util.array: Add array.collect() to collect results from iterators into an array, and use module() to correctly set the module name
Matthew Wild <mwild1@gmail.com>
parents:
922
diff
changeset
|
97 end |
fe2e3d3dba6a
util.array: Add array.collect() to collect results from iterators into an array, and use module() to correctly set the module name
Matthew Wild <mwild1@gmail.com>
parents:
922
diff
changeset
|
98 |
1905
e3e0a17e0b33
util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
99 --- |
e3e0a17e0b33
util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
100 |
e3e0a17e0b33
util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
101 -- Setup methods from array_base |
e3e0a17e0b33
util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
102 for method, f in pairs(array_base) do |
e3e0a17e0b33
util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
103 local method = method; -- Yes, this is necessary :) |
e3e0a17e0b33
util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
104 local base_method = f; |
e3e0a17e0b33
util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
105 -- Setup global array method which makes new array |
e3e0a17e0b33
util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
106 array[method] = function (old_a, ...) |
e3e0a17e0b33
util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
107 local a = new_array(); |
e3e0a17e0b33
util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
108 return base_method(a, old_a, ...); |
e3e0a17e0b33
util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
109 end |
e3e0a17e0b33
util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
110 -- Setup per-array (mutating) method |
e3e0a17e0b33
util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
111 array_methods[method] = function (self, ...) |
e3e0a17e0b33
util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
112 return base_method(self, self, ...); |
e3e0a17e0b33
util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
113 end |
e3e0a17e0b33
util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
114 end |
e3e0a17e0b33
util.array: Per-array methods now always mutate the array, array.* return a mutated copy, and most methods (e.g. sort) now return the array
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
115 |
1027
fe2e3d3dba6a
util.array: Add array.collect() to collect results from iterators into an array, and use module() to correctly set the module name
Matthew Wild <mwild1@gmail.com>
parents:
922
diff
changeset
|
116 _G.array = array; |
fe2e3d3dba6a
util.array: Add array.collect() to collect results from iterators into an array, and use module() to correctly set the module name
Matthew Wild <mwild1@gmail.com>
parents:
922
diff
changeset
|
117 module("array"); |
fe2e3d3dba6a
util.array: Add array.collect() to collect results from iterators into an array, and use module() to correctly set the module name
Matthew Wild <mwild1@gmail.com>
parents:
922
diff
changeset
|
118 |
fe2e3d3dba6a
util.array: Add array.collect() to collect results from iterators into an array, and use module() to correctly set the module name
Matthew Wild <mwild1@gmail.com>
parents:
922
diff
changeset
|
119 return array; |