git.iko.soy/typeable/pandoc
on master help sign in
Lua: add methods `insert`, `remove`, and `sort` to pandoc.List

The functions `table.insert`, `table.remove`, and `table.sort` are added to pandoc.List elements. They can be used as methods, e.g. local numbers = pandoc.List {2, 3, 1} numbers:sort() -- numbers is now {1, 2, 3}

commit4ede11a75f37bb35d384fc9ce95231671e18acf3 authorAlbert Krewinkel <albert@zeitkraut.de> dateSat 11 Jan 2020, 20:35 (7 years ago) parent23d8108
3 files changed+156−40
data/pandoc.List.lua+17 doc/lua-filters.md+59 test/lua/module/pandoc-list.lua+80−40
data/pandoc.List.lua+17
@@ -104,6 +104,12 @@ function List:includes (needle, init)
104104 return not (List.find(self, needle, init) == nil)
105105end
106106
107+--- Insert an element into the list. Alias for `table.insert`.
108+-- @param list list
109+-- @param[opt] pos position at which the new element is to be inserted
110+-- @param value value to insert
111+List.insert = table.insert
112+
107113--- Returns a copy of the current list by applying the given function to
108114-- all elements.
109115-- @param fn function which is applied to all list items.
@@ -115,6 +121,17 @@ function List:map (fn)
115121 return res
116122end
117123
124+--- Remove element from list (alias for `table.remove`)
125+-- @param list list
126+-- @param[opt] pos position of the element to be removed (default: #list)
127+-- @return the removed element
128+List.remove = table.remove
129+
130+--- Sort list in-place (alias for `table.sort`)
131+-- @param list list
132+-- @param[opt] comp comparison function; default to `<` operator.
133+List.sort = table.sort
134+
118135-- Set metatable with __call metamethod. This allows the use of `List`
119136-- as a constructor function.
120137local ListMT = {
doc/lua-filters.md+59
@@ -2734,6 +2734,22 @@ methods and convenience functions.
27342734 Returns: true if a list item is equal to the needle, false
27352735 otherwise
27362736
2737+[`pandoc.List:insert ([pos], value)`]{#pandoc.list:insert}
2738+
2739+: Inserts element `value` at position `pos` in list, shifting
2740+ elements to the next-greater indix if necessary.
2741+
2742+ This function is identical to
2743+ [`table.insert`](https://www.lua.org/manual/5.3/manual.html#6.6).
2744+
2745+ Parameters:
2746+
2747+ `pos`:
2748+ : index of the new value; defaults to length of the list + 1
2749+
2750+ `value`:
2751+ : value to insert into the list
2752+
27372753[`pandoc.List:map (fn)`]{#pandoc.list:map}
27382754
27392755: Returns a copy of the current list by applying the given
@@ -2757,6 +2773,49 @@ methods and convenience functions.
27572773
27582774 Returns: the updated input value
27592775
2776+[`pandoc.List:remove ([pos])`]{#pandoc.list:remove}
2777+
2778+: Removes the element at position `pos`, returning the value
2779+ of the removed element.
2780+
2781+ This function is identical to
2782+ [`table.remove`](https://www.lua.org/manual/5.3/manual.html#6.6).
2783+
2784+ Parameters:
2785+
2786+ `pos`:
2787+ : position of the list value that will be remove; defaults
2788+ to the index of the last element
2789+
2790+ Returns: the removed element
2791+
2792+[`pandoc.List:sort ([comp])`]{#pandoc.list:sort}
2793+
2794+: Sorts list elements in a given order, in-place. If `comp` is
2795+ given, then it must be a function that receives two list
2796+ elements and returns true when the first element must come
2797+ before the second in the final order (so that, after the
2798+ sort, `i < j` implies `not comp(list[j],list[i]))`. If comp
2799+ is not given, then the standard Lua operator `<` is used
2800+ instead.
2801+
2802+ Note that the comp function must define a strict partial
2803+ order over the elements in the list; that is, it must be
2804+ asymmetric and transitive. Otherwise, no valid sort may be
2805+ possible.
2806+
2807+ The sort algorithm is not stable: elements considered equal
2808+ by the given order may have their relative positions changed
2809+ by the sort.
2810+
2811+ This function is identical to
2812+ [`table.sort`](https://www.lua.org/manual/5.3/manual.html#6.6).
2813+
2814+ Parameters:
2815+
2816+ `comp`:
2817+ : Comparison function as described above.
2818+
27602819# Module pandoc.system
27612820
27622821Access to system information and functionality.
test/lua/module/pandoc-list.lua+80−40
@@ -15,33 +15,6 @@ return {
1515 end)
1616 },
1717
18− group 'new' {
19− test('make table usable as list', function ()
20− local test = List:new{1, 1, 2, 3, 5}
21− assert.are_same(
22− {1, 1, 4, 9, 25},
23− test:map(function (x) return x^2 end)
24− )
25− end),
26− test('return empty list if no argument is given', function ()
27− assert.are_same({}, List:new())
28− end),
29− test('metatable of result is pandoc.List', function ()
30− local test = List:new{5}
31− assert.are_equal(List, getmetatable(test))
32− end)
33− },
34−
35− group 'includes' {
36− test('finds elements in list', function ()
37− local lst = List:new {'one', 'two', 'three'}
38− assert.is_truthy(lst:includes('one'))
39− assert.is_truthy(lst:includes('two'))
40− assert.is_truthy(lst:includes('three'))
41− assert.is_falsy(lst:includes('four'))
42− end)
43− },
44−
4518 group 'clone' {
4619 test('changing the clone does not affect original', function ()
4720 local orig = List:new {23, 42}
@@ -54,7 +27,25 @@ return {
5427 local orig = List:new {23, 42}
5528 assert.are_equal(List, getmetatable(orig:clone()))
5629 end),
57− },
30+ },
31+
32+ group 'extend' {
33+ test('extends list with other list', function ()
34+ local primes = List:new {2, 3, 5, 7}
35+ primes:extend {11, 13, 17}
36+ assert.are_same({2, 3, 5, 7, 11, 13, 17}, primes)
37+ end)
38+ },
39+
40+ group 'filter' {
41+ test('keep elements for which property is truthy', function ()
42+ local is_small_prime = function (x)
43+ return List.includes({2, 3, 5, 7}, x)
44+ end
45+ local numbers = List:new {4, 7, 2, 9, 5, 11}
46+ assert.are_same({7, 2, 5}, numbers:filter(is_small_prime))
47+ end),
48+ },
5849
5950 group 'find' {
6051 test('returns element and index if found', function ()
@@ -87,11 +78,26 @@ return {
8778 end),
8879 },
8980
90− group 'extend' {
91− test('extends list with other list', function ()
92− local primes = List:new {2, 3, 5, 7}
93− primes:extend {11, 13, 17}
94− assert.are_same({2, 3, 5, 7, 11, 13, 17}, primes)
81+ group 'includes' {
82+ test('finds elements in list', function ()
83+ local lst = List:new {'one', 'two', 'three'}
84+ assert.is_truthy(lst:includes('one'))
85+ assert.is_truthy(lst:includes('two'))
86+ assert.is_truthy(lst:includes('three'))
87+ assert.is_falsy(lst:includes('four'))
88+ end)
89+ },
90+
91+ group 'insert' {
92+ test('insert value at end of list.', function ()
93+ local count_norsk = List {'en', 'to', 'tre'}
94+ count_norsk:insert('fire')
95+ assert.are_same({'en', 'to', 'tre', 'fire'}, count_norsk)
96+ end),
97+ test('insert value in the middle of list.', function ()
98+ local count_norsk = List {'fem', 'syv'}
99+ count_norsk:insert(2, 'seks')
100+ assert.are_same({'fem', 'seks', 'syv'}, count_norsk)
95101 end)
96102 },
97103
@@ -108,13 +114,47 @@ return {
108114 end)
109115 },
110116
111− group 'filter' {
112− test('keep elements for which property is truthy', function ()
113− local is_small_prime = function (x)
114− return List.includes({2, 3, 5, 7}, x)
115− end
116− local numbers = List:new {4, 7, 2, 9, 5, 11}
117− assert.are_same({7, 2, 5}, numbers:filter(is_small_prime))
117+ group 'new' {
118+ test('make table usable as list', function ()
119+ local test = List:new{1, 1, 2, 3, 5}
120+ assert.are_same(
121+ {1, 1, 4, 9, 25},
122+ test:map(function (x) return x^2 end)
123+ )
124+ end),
125+ test('return empty list if no argument is given', function ()
126+ assert.are_same({}, List:new())
127+ end),
128+ test('metatable of result is pandoc.List', function ()
129+ local test = List:new{5}
130+ assert.are_equal(List, getmetatable(test))
131+ end)
132+ },
133+
134+ group 'remove' {
135+ test('remove value at end of list.', function ()
136+ local understand = List {'jeg', 'forstår', 'ikke'}
137+ local norsk_not = understand:remove()
138+ assert.are_same({'jeg', 'forstår'}, understand)
139+ assert.are_equal('ikke', norsk_not)
118140 end),
141+ test('remove value at beginning of list.', function ()
142+ local count_norsk = List {'en', 'to', 'tre'}
143+ count_norsk:remove(1)
144+ assert.are_same({'to', 'tre'}, count_norsk)
145+ end)
146+ },
147+
148+ group 'sort' {
149+ test('sort numeric list', function ()
150+ local numbers = List {71, 5, -1, 42, 23, 0, 1}
151+ numbers:sort()
152+ assert.are_same({-1, 0, 1, 5, 23, 42, 71}, numbers)
153+ end),
154+ test('reverse-sort numeric', function ()
155+ local numbers = List {71, 5, -1, 42, 23, 0, 1}
156+ numbers:sort(function (x, y) return x > y end)
157+ assert.are_same({71, 42, 23, 5, 1, 0, -1}, numbers)
158+ end)
119159 },
120160}