Software /
code /
prosody
Annotate
util/array.lua @ 4440:c60ed6732b34
util.array: Expand some of the more basic methods to act more sensibly than their names suggested
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Wed, 07 Dec 2011 05:14:58 +0000 |
parent | 4439:1c8d2c0d02db |
child | 4448:d745f4c28294 |
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 |
2923
b7049746bd29
Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents:
1934
diff
changeset
|
2 -- Copyright (C) 2008-2010 Matthew Wild |
b7049746bd29
Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents:
1934
diff
changeset
|
3 -- Copyright (C) 2008-2010 Waqas Hussain |
1522
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 |
3540
bc139431830b
Monster whitespace commit (beware the whitespace monster).
Waqas Hussain <waqas20@gmail.com>
parents:
2923
diff
changeset
|
9 local t_insert, t_sort, t_remove, t_concat |
bc139431830b
Monster whitespace commit (beware the whitespace monster).
Waqas Hussain <waqas20@gmail.com>
parents:
2923
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 |
4440
c60ed6732b34
util.array: Expand some of the more basic methods to act more sensibly than their names suggested
Matthew Wild <mwild1@gmail.com>
parents:
4439
diff
changeset
|
28 -- Read-only methods |
c60ed6732b34
util.array: Expand some of the more basic methods to act more sensibly than their names suggested
Matthew Wild <mwild1@gmail.com>
parents:
4439
diff
changeset
|
29 function array_methods:random() |
c60ed6732b34
util.array: Expand some of the more basic methods to act more sensibly than their names suggested
Matthew Wild <mwild1@gmail.com>
parents:
4439
diff
changeset
|
30 return self[math.random(1,#self)]; |
c60ed6732b34
util.array: Expand some of the more basic methods to act more sensibly than their names suggested
Matthew Wild <mwild1@gmail.com>
parents:
4439
diff
changeset
|
31 end |
c60ed6732b34
util.array: Expand some of the more basic methods to act more sensibly than their names suggested
Matthew Wild <mwild1@gmail.com>
parents:
4439
diff
changeset
|
32 |
c60ed6732b34
util.array: Expand some of the more basic methods to act more sensibly than their names suggested
Matthew Wild <mwild1@gmail.com>
parents:
4439
diff
changeset
|
33 -- These methods can be called two ways: |
c60ed6732b34
util.array: Expand some of the more basic methods to act more sensibly than their names suggested
Matthew Wild <mwild1@gmail.com>
parents:
4439
diff
changeset
|
34 -- array.method(existing_array, [params [, ...]]) -- Create new array for result |
c60ed6732b34
util.array: Expand some of the more basic methods to act more sensibly than their names suggested
Matthew Wild <mwild1@gmail.com>
parents:
4439
diff
changeset
|
35 -- existing_array:method([params, ...]) -- Transform existing array into result |
c60ed6732b34
util.array: Expand some of the more basic methods to act more sensibly than their names suggested
Matthew Wild <mwild1@gmail.com>
parents:
4439
diff
changeset
|
36 -- |
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
|
37 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
|
38 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
|
39 outa[k] = func(v); |
918 | 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.filter(outa, ina, func) |
1915
e9d5406caf8c
util.array: Fix for array:filter() (in-place filtering)
Matthew Wild <mwild1@gmail.com>
parents:
1905
diff
changeset
|
45 local inplace, start_length = ina == outa, #ina; |
e9d5406caf8c
util.array: Fix for array:filter() (in-place filtering)
Matthew Wild <mwild1@gmail.com>
parents:
1905
diff
changeset
|
46 local write = 1; |
e9d5406caf8c
util.array: Fix for array:filter() (in-place filtering)
Matthew Wild <mwild1@gmail.com>
parents:
1905
diff
changeset
|
47 for read=1,start_length do |
e9d5406caf8c
util.array: Fix for array:filter() (in-place filtering)
Matthew Wild <mwild1@gmail.com>
parents:
1905
diff
changeset
|
48 local v = ina[read]; |
918 | 49 if func(v) then |
1915
e9d5406caf8c
util.array: Fix for array:filter() (in-place filtering)
Matthew Wild <mwild1@gmail.com>
parents:
1905
diff
changeset
|
50 outa[write] = v; |
e9d5406caf8c
util.array: Fix for array:filter() (in-place filtering)
Matthew Wild <mwild1@gmail.com>
parents:
1905
diff
changeset
|
51 write = write + 1; |
918 | 52 end |
53 end | |
1915
e9d5406caf8c
util.array: Fix for array:filter() (in-place filtering)
Matthew Wild <mwild1@gmail.com>
parents:
1905
diff
changeset
|
54 |
1922
d5fe0f9b377a
util.array: Small logic fix for array:filter()
Matthew Wild <mwild1@gmail.com>
parents:
1915
diff
changeset
|
55 if inplace and write <= start_length then |
1915
e9d5406caf8c
util.array: Fix for array:filter() (in-place filtering)
Matthew Wild <mwild1@gmail.com>
parents:
1905
diff
changeset
|
56 for i=write,start_length do |
e9d5406caf8c
util.array: Fix for array:filter() (in-place filtering)
Matthew Wild <mwild1@gmail.com>
parents:
1905
diff
changeset
|
57 outa[i] = nil; |
e9d5406caf8c
util.array: Fix for array:filter() (in-place filtering)
Matthew Wild <mwild1@gmail.com>
parents:
1905
diff
changeset
|
58 end |
e9d5406caf8c
util.array: Fix for array:filter() (in-place filtering)
Matthew Wild <mwild1@gmail.com>
parents:
1905
diff
changeset
|
59 end |
e9d5406caf8c
util.array: Fix for array:filter() (in-place filtering)
Matthew Wild <mwild1@gmail.com>
parents:
1905
diff
changeset
|
60 |
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
|
61 return outa; |
918 | 62 end |
63 | |
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
|
64 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
|
65 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
|
66 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
|
67 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
|
68 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
|
69 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
|
70 end |
918 | 71 |
4439
1c8d2c0d02db
util.array: Add pluck() method to pick a given property from each item
Matthew Wild <mwild1@gmail.com>
parents:
4387
diff
changeset
|
72 function array_base.pluck(outa, ina, key) |
1c8d2c0d02db
util.array: Add pluck() method to pick a given property from each item
Matthew Wild <mwild1@gmail.com>
parents:
4387
diff
changeset
|
73 for i=1,#ina do |
1c8d2c0d02db
util.array: Add pluck() method to pick a given property from each item
Matthew Wild <mwild1@gmail.com>
parents:
4387
diff
changeset
|
74 outa[i] = ina[i][key]; |
1c8d2c0d02db
util.array: Add pluck() method to pick a given property from each item
Matthew Wild <mwild1@gmail.com>
parents:
4387
diff
changeset
|
75 end |
1c8d2c0d02db
util.array: Add pluck() method to pick a given property from each item
Matthew Wild <mwild1@gmail.com>
parents:
4387
diff
changeset
|
76 return outa; |
1c8d2c0d02db
util.array: Add pluck() method to pick a given property from each item
Matthew Wild <mwild1@gmail.com>
parents:
4387
diff
changeset
|
77 end |
1c8d2c0d02db
util.array: Add pluck() method to pick a given property from each item
Matthew Wild <mwild1@gmail.com>
parents:
4387
diff
changeset
|
78 |
4440
c60ed6732b34
util.array: Expand some of the more basic methods to act more sensibly than their names suggested
Matthew Wild <mwild1@gmail.com>
parents:
4439
diff
changeset
|
79 --- These methods only mutate the array |
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
|
80 function array_methods:shuffle(outa, ina) |
918 | 81 local len = #self; |
82 for i=1,#self do | |
83 local r = math.random(i,len); | |
84 self[i], self[r] = self[r], self[i]; | |
85 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
|
86 return self; |
918 | 87 end |
88 | |
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
|
89 function array_methods:reverse() |
922
0e45234360cd
util.array: Add :reverse() method
Matthew Wild <mwild1@gmail.com>
parents:
920
diff
changeset
|
90 local len = #self-1; |
0e45234360cd
util.array: Add :reverse() method
Matthew Wild <mwild1@gmail.com>
parents:
920
diff
changeset
|
91 for i=len,1,-1 do |
0e45234360cd
util.array: Add :reverse() method
Matthew Wild <mwild1@gmail.com>
parents:
920
diff
changeset
|
92 self:push(self[i]); |
0e45234360cd
util.array: Add :reverse() method
Matthew Wild <mwild1@gmail.com>
parents:
920
diff
changeset
|
93 self:pop(i); |
0e45234360cd
util.array: Add :reverse() method
Matthew Wild <mwild1@gmail.com>
parents:
920
diff
changeset
|
94 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
|
95 return self; |
922
0e45234360cd
util.array: Add :reverse() method
Matthew Wild <mwild1@gmail.com>
parents:
920
diff
changeset
|
96 end |
0e45234360cd
util.array: Add :reverse() method
Matthew Wild <mwild1@gmail.com>
parents:
920
diff
changeset
|
97 |
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
|
98 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
|
99 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
|
100 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
|
101 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
|
102 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
|
103 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
|
104 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
|
105 |
4440
c60ed6732b34
util.array: Expand some of the more basic methods to act more sensibly than their names suggested
Matthew Wild <mwild1@gmail.com>
parents:
4439
diff
changeset
|
106 function array_methods:push(x) |
c60ed6732b34
util.array: Expand some of the more basic methods to act more sensibly than their names suggested
Matthew Wild <mwild1@gmail.com>
parents:
4439
diff
changeset
|
107 table.insert(self, x); |
c60ed6732b34
util.array: Expand some of the more basic methods to act more sensibly than their names suggested
Matthew Wild <mwild1@gmail.com>
parents:
4439
diff
changeset
|
108 end |
c60ed6732b34
util.array: Expand some of the more basic methods to act more sensibly than their names suggested
Matthew Wild <mwild1@gmail.com>
parents:
4439
diff
changeset
|
109 |
c60ed6732b34
util.array: Expand some of the more basic methods to act more sensibly than their names suggested
Matthew Wild <mwild1@gmail.com>
parents:
4439
diff
changeset
|
110 function array_methods:pop(x) |
c60ed6732b34
util.array: Expand some of the more basic methods to act more sensibly than their names suggested
Matthew Wild <mwild1@gmail.com>
parents:
4439
diff
changeset
|
111 local v = self[x]; |
c60ed6732b34
util.array: Expand some of the more basic methods to act more sensibly than their names suggested
Matthew Wild <mwild1@gmail.com>
parents:
4439
diff
changeset
|
112 table.remove(self, x); |
c60ed6732b34
util.array: Expand some of the more basic methods to act more sensibly than their names suggested
Matthew Wild <mwild1@gmail.com>
parents:
4439
diff
changeset
|
113 return v; |
c60ed6732b34
util.array: Expand some of the more basic methods to act more sensibly than their names suggested
Matthew Wild <mwild1@gmail.com>
parents:
4439
diff
changeset
|
114 end |
c60ed6732b34
util.array: Expand some of the more basic methods to act more sensibly than their names suggested
Matthew Wild <mwild1@gmail.com>
parents:
4439
diff
changeset
|
115 |
c60ed6732b34
util.array: Expand some of the more basic methods to act more sensibly than their names suggested
Matthew Wild <mwild1@gmail.com>
parents:
4439
diff
changeset
|
116 function array_methods:concat(sep) |
c60ed6732b34
util.array: Expand some of the more basic methods to act more sensibly than their names suggested
Matthew Wild <mwild1@gmail.com>
parents:
4439
diff
changeset
|
117 return table.concat(array.map(self, tostring), sep); |
c60ed6732b34
util.array: Expand some of the more basic methods to act more sensibly than their names suggested
Matthew Wild <mwild1@gmail.com>
parents:
4439
diff
changeset
|
118 end |
c60ed6732b34
util.array: Expand some of the more basic methods to act more sensibly than their names suggested
Matthew Wild <mwild1@gmail.com>
parents:
4439
diff
changeset
|
119 |
c60ed6732b34
util.array: Expand some of the more basic methods to act more sensibly than their names suggested
Matthew Wild <mwild1@gmail.com>
parents:
4439
diff
changeset
|
120 function array_methods:length() |
c60ed6732b34
util.array: Expand some of the more basic methods to act more sensibly than their names suggested
Matthew Wild <mwild1@gmail.com>
parents:
4439
diff
changeset
|
121 return #self; |
c60ed6732b34
util.array: Expand some of the more basic methods to act more sensibly than their names suggested
Matthew Wild <mwild1@gmail.com>
parents:
4439
diff
changeset
|
122 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
|
123 |
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
|
124 --- 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
|
125 function array.collect(f, s, var) |
4387
06161b0b83f2
util.array: Fix array.collect() for iterators that expect initial value of var to be preserved
Matthew Wild <mwild1@gmail.com>
parents:
3540
diff
changeset
|
126 local t = {}; |
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
|
127 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
|
128 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
|
129 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
|
130 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
|
131 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
|
132 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
|
133 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
|
134 |
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
|
135 --- |
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
|
136 |
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
|
137 -- 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
|
138 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
|
139 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
|
140 -- 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
|
141 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
|
142 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
|
143 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
|
144 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
|
145 -- 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
|
146 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
|
147 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
|
148 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
|
149 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
|
150 |
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
|
151 _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
|
152 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
|
153 |
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
|
154 return array; |