From 9940fcf292e61f77741a52fb5ab5aac35c68e748 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Evandro=20Leopoldino=20Gon=C3=A7alves?= Date: Sat, 13 Jun 2020 00:51:44 +0200 Subject: [PATCH 01/14] create gh-page --- index.html | 462 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 462 insertions(+) create mode 100644 index.html diff --git a/index.html b/index.html new file mode 100644 index 0000000..44835fb --- /dev/null +++ b/index.html @@ -0,0 +1,462 @@ + + + + + + array + + + + + Fork me on GitHub + +
+ + +
+
+

+ array.concat(obj:table,obj2:table):table +

+

Return a new table joining all values from the two tables passed by parameter

+
+
+

+ array.counter(obj:table):table +

+

and value represent the number of times the same item was found in the list

+
+
+

+ array.deep_copy(value:*):table +

+

Return a deep copy of the table passed as parameter

+
+
+

+ array.diff(obj:table,obj2:table):table +

+

Return a new table with the items which exist only in the first table

+
+
+

+ array.each(obj:table,callback:function):void +

+

Executes callback once for each table element

+
+
+

+ array.every(obj:table,obj2:table):table +

+

Return a table of the two supplied by pairing up equally-positioned elements from both tables

+
+
+

+ array.fill(value:*,start_or_finish:number,finish:number):table +

+

Creates a table filling all the elements from a start index (default one) to an end index with a default value

+
+
+

+ array.filter(obj:table,callback:function):* +

+

Create a new table containing all elements that pass truth test

+
+
+

+ array.first(obj:table):* +

+

Return first element from the table

+
+
+

+ array.flat(obj:table):table +

+

Create a new table with the sub-table elements concatenated into it

+
+
+

+ array.from_pairs(obj1:table,obj2:table):table +

+

Return a table composed from key-value pairs

+
+
+

+ array.includes(obj:table,value:*):boolean +

+

Return true whether table includes the values, otherwise it returns false

+
+
+

+ array.index_of(obj:table,value:*):number +

+

Return the index at which value can be found or -1 in case value is not present

+
+
+

+ array.intersect(obj1:table,obj2:table):table +

+

Return a new table with the values that exist in both tables

+
+
+

+ array.is_array(obj:table):boolean +

+

Verify if table object works as an array

+
+
+

+ array.is_empty(obj:table):boolean +

+

Check if parameter is an empty table

+
+
+

+ array.last(obj:table):* +

+

Return last element from the table

+
+
+

+ array.map(obj:table,callback:function):* +

+

Create a new table of values by mapping each value in table through a transformation function

+
+
+

+ array.max(obj:table):* +

+

Return maximum value from the table

+
+
+

+ array.min(obj:table):* +

+

Return minimum value from the table

+
+
+

+ array.reduce(obj:table,callback:function,memo:table):* +

+

Applies a function against an accumulator and each value of the table to reduce it to a single value

+
+
+

+ array.reduce_right(obj:table,callback:function,memo:table):* +

+

This function is like reduce except that it interates over table's elements from right to left

+
+
+

+ array.remove(obj:table,callback:function):table +

+

and return a new table with the removed items

+
+
+

+ array.reverse(obj:table):table +

+

Create a new table with reverse values

+
+
+

+ array.reverse_each(obj:table,callback:function):void +

+

Executes callback once for each table element in reverse order

+
+
+

+ array.shallow_copy(obj:table):table +

+

return a shallow copy of the table passed as parameter

+
+
+

+ array.slice(obj:table,start:number,finish:number):boolean +

+

Return a shallow copy of a portion of a table into a new table

+
+
+

+ array.some(obj:table,callback:function):boolean +

+

Tests if at least one element in the table passes the test implemented by the callback

+
+
+

+ array.sum(obj:table,callback:function):number +

+

Return the sum of the values in table

+
+
+

+ array.uniq(obj:table):table +

+

Create a new table, removing duplicates values

+
+
+

+ array.without(obj:table,values:table):table +

+

Return a copy of the table with all instances of the values removed

+
+
+

+ array.zip(obj:table,obj2:table):table +

+

Return a table of the two supplied by pairing up equally-positioned elements from both tables

+
+
+
+ + From 01c36eae3a84e7b9d84787623c7eb590e675aee0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Evandro=20Leopoldino=20Gon=C3=A7alves?= Date: Sat, 13 Jun 2020 02:20:51 +0200 Subject: [PATCH 02/14] update title --- index.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.html b/index.html index 44835fb..52f91a2 100644 --- a/index.html +++ b/index.html @@ -3,7 +3,7 @@ - array + array.lua @@ -269,6 +283,8 @@

array.lua

+

array.lua

+

array.concat(obj:table,obj2:table):table From f66040a32b6f84474358b62625c097de9fcb7e1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Evandro=20Leopoldino=20Gon=C3=A7alves?= Date: Mon, 22 Jun 2020 23:10:15 +0200 Subject: [PATCH 05/14] update gh page --- index.html | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/index.html b/index.html index 65b2a81..f7895fe 100644 --- a/index.html +++ b/index.html @@ -14,6 +14,10 @@ box-sizing: border-box; } + html { + font-size: 16px; + } + body { overflow: hidden; margin: 0; @@ -35,7 +39,6 @@ list-style: none; } - h1 { font-size: 1.8rem; } From d248e488261890d7f8a00418bcec5787b1058884 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Evandro=20Leopoldino=20Gon=C3=A7alves?= Date: Mon, 3 Aug 2020 22:00:20 +0200 Subject: [PATCH 06/14] update doc --- index.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.html b/index.html index f7895fe..0e0e11d 100644 --- a/index.html +++ b/index.html @@ -322,7 +322,7 @@

array.every(obj:table,obj2:table):table

-

Return a table of the two supplied by pairing up equally-positioned elements from both tables

+

Run a predicate on each value. If the predicate evaluates to any false value, this function will immediately return false; otherwise, it returns true.

From 0d5ef2b28fedd25cd3d65381b868bc4606a667ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Evandro=20Leopoldino=20Gon=C3=A7alves?= Date: Wed, 14 Oct 2020 20:21:51 +0200 Subject: [PATCH 07/14] remove unnecessary files --- Makefile | 19 --- README.md | 107 ------------ tasks/create_rockspec.sh | 11 -- tasks/default_rockspec | 26 --- tasks/upload_rockspec.sh | 10 -- test.lua | 360 --------------------------------------- 6 files changed, 533 deletions(-) delete mode 100644 Makefile delete mode 100644 README.md delete mode 100644 tasks/create_rockspec.sh delete mode 100644 tasks/default_rockspec delete mode 100644 tasks/upload_rockspec.sh delete mode 100644 test.lua diff --git a/Makefile b/Makefile deleted file mode 100644 index e8ddd6b..0000000 --- a/Makefile +++ /dev/null @@ -1,19 +0,0 @@ -task_folder := ./tasks/ - -.SILENT: - -install_dependencies: - luarocks install simple_test - -test: - LUA_PATH="./src/?.lua;./src/?/init.lua;./src/array/?.lua;;" lua test.lua - -rockspec: - bash $(task_folder)create_rockspec.sh $(version) - -upload_rockspec: - bash $(task_folder)upload_rockspec.sh $(version) - -clean_rockspec: - rm -rf *.rockspec - rm -rf *.rock diff --git a/README.md b/README.md deleted file mode 100644 index 8aa540e..0000000 --- a/README.md +++ /dev/null @@ -1,107 +0,0 @@ -# array.lua -A small library with useful methods to handle Lua's table when it's working like an Array - -[![Build -Status](https://travis-ci.org/EvandroLG/array.lua.svg?branch=master)](https://travis-ci.org/EvandroLG/array.lua) - -## Installation -To install array, run: -```sh -$ luarocks install array -``` -Or simply copy the array.lua file and paste in your project. - -## Methods -* array.is_array(object:table):boolean
-Checks if table is an Array - -* array.is_empty(object:table):boolean
-Checks if table is empty - -* array.slice(object:table, start:number, end:number):table
-Returns a shallow copy of a portion of a table into a new table - -* array.index_of(object:table, value:*):number
-Returns the index at which value can be found or -1 if value is not present - -* array.reverse(object:table):table
-Creates a new table with reverse values - -* array.first(object:table):*
-Returns the first value in a table - -* array.last(object:table):*
-Returns the last value in a table - -* array.max(object:table):*
-Returns the maximum value in a table - -* array.min(object:table):*
-Returns the minimum value in a table - -* array.map(object:table, callback:function):table
-Creates a new table of values by mapping each value in list through a transformation function - -* array.filter(object:table, callback:function):table
-Produces a new table containing all elements that pass truth test - -* array.reduce(object:table, callback:function [, memo]):*
-Applies a function against an accumulator and each value of the table to reduce it to a single value - -* array.reduce_right(object:table, callback:function [, memo]):*
-Works like `reduce` except that it iterates over table's elements from right to left - -* array.sum(object:table):number
-Returns the sum of the values of the table passed by parameter - -* array.concat(object:table, object:table):table
-Returns a new table by joining all values from the two tables - -* array.uniq(object:table):table
-Returns a new table by removing duplicates values - -* array.without(object:table, object:table):table
-Returns a copy of the table with all instances of the values removed - -* array.some(object:table, callback:function):boolean
-Tests whether at least one element in the table passes the test implemented by the callback - -* array.every(object:table, callback:function):boolean
-Tests whether all elements in the table passes the test implemented by the callback - -* array.zip(object:table, object:table):table
-Returns a table of the two supplied by pairing up equally-positioned elements from both tables - -* array.shallow_copy(object:table):table
-Returns a shallow copy of the table passed as parameter - -* array.deep_copy(object:table):table
-Returns a deep copy of the table passed as parameter - -* array.diff(object:table, object:table):table
-Returns a new table with the items which exist only in the first table - -* array.flat(object:table):table
-Creates a new table with the sub-table elements concatenated into it - -* array.fill(value:*, [start:number], end:number):table
-Creates a table filling all the elements from a start index (default -one) to an end index with a default value passed by parameter. - -* array.remove(object:table, callback:function):table
-Removes all elements from table that `callback` returns truthy for and returns a new table with the removed elements - -* array.counter(object:table):table
-Returns a new table in hash structure, where keys represent each array value - -* array.intersect(object:table, object:table):table
-Returns a new table with the values that exist in both tables - -* array.from_pairs(object:table, object:table):table
-Returns a table composed from key-value pairs - -* array.each(object:table, callback:function):void
-Executes `callback` once for each table element - -* array.reverse_each(object:table, callback:function):void
-Executes `callback` once for each table element in reverse order diff --git a/tasks/create_rockspec.sh b/tasks/create_rockspec.sh deleted file mode 100644 index 1f48f4a..0000000 --- a/tasks/create_rockspec.sh +++ /dev/null @@ -1,11 +0,0 @@ -#!/usr/bin/env bash - -if [ -n "$1" ]; then - filename="array-$1-0.rockspec" - touch $filename - cat tasks/default_rockspec >> $filename - sed -i '' "s/{version}/$1/g" $filename -else - echo You should pass a new version - exit 1 -fi diff --git a/tasks/default_rockspec b/tasks/default_rockspec deleted file mode 100644 index 46e57fa..0000000 --- a/tasks/default_rockspec +++ /dev/null @@ -1,26 +0,0 @@ -package = 'array' -version = '{version}-0' - -source = { - url = 'git://github.com/evandrolg/array.lua.git', - tag = 'v{version}' -} - -description = { - summary = "A small library with useful methods to handle Lua's table when it's working like an Array", - homepage = 'https://github.com/EvandroLG/array.lua', - maintainer = 'Evandro Leopoldino Gonçalves (@evandrolg) ', - license = 'MIT ' -} - -dependencies = { - "lua >= 5.1" -} - -build = { - type = "builtin", - modules = { - ["array"] = "src/array/init.lua", - ["array.utils"] = "src/array/utils.lua" - } -} diff --git a/tasks/upload_rockspec.sh b/tasks/upload_rockspec.sh deleted file mode 100644 index 527917c..0000000 --- a/tasks/upload_rockspec.sh +++ /dev/null @@ -1,10 +0,0 @@ -#!/usr/bin/env bash - -if [ -n "$1" ]; then - filename="array-$1-0.rockspec" - key=$(cat .luarocks_key) - luarocks upload $filename --api-key=$key -else - echo You should pass a new version - exit 1 -fi diff --git a/test.lua b/test.lua deleted file mode 100644 index 2bdcbae..0000000 --- a/test.lua +++ /dev/null @@ -1,360 +0,0 @@ -local test = require 'simple_test' -local array = require 'array' - -test('meta infos', function(a) - a.equal(array.__VERSION, '1.3.1') - a.equal(array.__DESCRIPTION, - "A small library with useful methods to handle Lua's table when it's working like an Array") -end) - -test('is_array', function(a) - a.not_ok(array.is_array('lua')) - a.not_ok(array.is_array({ language='lua' })) - a.ok(array.is_array({})) - a.ok(array.is_array({ 'a', 'b', 'c', 'd' })) -end) - -test('is_empty', function(a) - a.not_ok(array.is_empty({ 'a' })) - a.ok(array.is_empty({})) -end) - -test('first', function(a) - a.equal(array.first({ 'a', 'b', 'c', 'd' }), 'a') -end) - -test('last', function(a) - a.equal(array.last({ 'a', 'b', 'c', 'd' }), 'd') -end) - -test('slice', function(a) - a.equal(#array.slice({}, 1, 2), 0) - - a.deep_equal( - array.slice({ 'lua', 'javascript', 'python', 'ruby', 'c' }, 2, 4), - { 'javascript', 'python', 'ruby' } - ) - - a.deep_equal( - array.slice({ 'lua', 'javascript', 'python', 'ruby', 'c' }, 2), - { 'javascript', 'python', 'ruby', 'c' } - ) -end) - -test('reverse', function(a) - a.deep_equal( - array.reverse({ 'lua', 'javascript', 'python' }), - { 'python', 'javascript', 'lua' } - ) -end) - -test('map should return a table with 2, 4, 6 values', function(a) - a.deep_equal( - array.map({ 1, 2, 3 }, function(value) - return value * 2 - end), - { 2, 4, 6 } - ) -end) - -test('filter should return a table with 10, 15, 20 values', function(a) - a.deep_equal( - array.filter({ 15, 10, 5, 3, 20 }, function(value) - return value >= 10 - end), - { 15, 10, 20 } - ) -end) - -test('max should return the biggest value from a table', function(a) - a.equal(array.max({ 20, 22, 1, 3, 30, 42 }), 42) -end) - -test('max should return nil when table is empty', function(a) - a.equal(array.max({}), nil) -end) - -test('min should return the smallest value from a table', function(a) - a.equal(array.min({ 20, 22, 1, 3, 30, 42 }), 1) -end) - -test('min should return nil when table is empty', function(a) - a.equal(array.min({}), nil) -end) - -test('reduce should return 90', function(a) - a.equal( - array.reduce({ 20, 30, 40 }, function(memo, value) - return memo + value - end), - 90 - ) -end) - -test('reduce should return 100', function(a) - a.equal( - array.reduce({ 20, 30, 40 }, function(memo, value) - return memo + value - end, 10), - 100) -end) - -test('reduce should return first item', function(a) - local result = - - a.equal( - array.reduce({ 20 }, function(memo, value) - return memo + value - end), - 20) -end) - -test('reduce should concatenate all items', function(a) - a.equal( - array.reduce({ 'a', 'b', 'c', 'd', 'e' }, function(memo, value) - return memo .. value - end), - 'abcde' - ) -end) - -test('reduce_right', function(a) - a.equal( - array.reduce_right({ 20, 30, 40 }, function(memo, value) - return memo + value - end), - 90 - ) - - a.equal( - array.reduce_right({ 'a', 'b', 'c', 'd', 'e' }, function(memo, value) - return memo .. value - end), - 'edcba' - ) -end) - -test('sum', function(a) - a.equal( - array.sum({10, 20, 30, 40, 50}), - 150 - ) -end) - -test('index_of', function(a) - a.equal( - array.index_of({ 20, 30, 40, 50 }, 40), - 3 - ) - - a.equal( - array.index_of({ 20, 30, 40 }, 50), - -1 - ) -end) - -test('concat', function(a) - a.deep_equal( - array.concat({ 1, 2, 3 }, { 4, 5, 6 }), - { 1, 2, 3, 4, 5, 6 } - ) -end) - -test('uniq', function(a) - a.deep_equal( - array.uniq({ 'a', 'b', 'a', 'b', 'c', 'd' }), - { 'a', 'b', 'c', 'd' } - ) -end) - -test('without', function(a) - a.deep_equal( - array.without({ 10, 20, 30, 10, 4 }, { 10, 4 }), - { 20, 30 } - ) -end) - -test('some', function(a) - a.ok( - array.some({'a', 'b', 'c'}, function(element) - if (element == 'b') then return true end - end) - ) - - a.not_ok( - array.some({'a', 'b', 'c'}, function(element) - if (element == 'd') then return true end - end) - ) -end) - -test('zip', function(a) - a.deep_equal( - array.zip({ 'a', 'b' }, { 'A', 'B', 'C' }), - { - { 'a', 'A' }, - { 'b', 'B' } - } - ) - - a.deep_equal( - array.zip({ 'a', 'b' }, { 'A', 'B' }), - { - { 'a', 'A' }, - { 'b', 'B' } - } - ) -end) - -test('every', function(a) - a.not_ok( - array.every({ 10, 20, 30 }, function(value) - return value > 10 - end) - ) - - a.ok( - array.every({ 10, 20, 30 }, function(value) - return value >= 10 - end) - ) -end) - -test('shallow_copy', function(a) - local obj = { - { 'a' }, - { 'b' } - } - - a.deep_equal( - array.shallow_copy(obj), - obj - ) -end) - -test('deep_copy', function(a) - local obj = { - { 'a' }, - { 'b' } - } - - local result = array.deep_copy(obj) - - a.equal(#result, #obj) - a.equal(result[1][1], obj[1][1]) - a.equal(result[2][1], obj[2][1]) - - obj[1][1] = 'c' - a.equal(result[1][1], 'a') - - obj[1] = 'c' - a.equal(type(result[1]), 'table') -end) - -test('diff', function(a) - local result = array.diff( - { 'a', 'b', 'g', 'z', 'h' }, - { 'a', 'c', 'd', 'e', 'f', 'h', 'x' } - ) - - a.deep_equal(result, { 'b', 'g', 'z' }) -end) - -test('flat', function(a) - a.deep_equal( - array.flat({ 'a', 'b', 'c', { 'd', 'e', 'f', { 'g', 'h' } }, { 'i' }, 'j' }), - { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j' } - ) -end) - -test('fill', function(a) - local value = 'Lua' - local result = array.fill(value, 3, 4) - a.equal(result[3], value) - a.equal(result[4], value) - - a.deep_equal( - array.fill(value, 3), - { value, value, value } - ) -end) - -test('remove', function(a) - local list = { 1, 2, 3, 4 } - local result = array.remove(list, function(value) - return math.fmod(value, 2) == 0 - end) - - a.deep_equal( - list, - { 1, 3 } - ) - - a.deep_equal( - result, - { 2, 4 } - ) -end) - -test('counter', function(a) - a.deep_equal( - array.counter({ 'a', 'b', 'a', 'a', 'c', 'b' }), - { a = 3, b = 2, c = 1 } - ) -end) - -test('intersect', function(a) - local first_list = { 'a', 'b', 'a', 'c', 'e', 'f', 'a' } - local second_list = { 'b', 'a', 'd', 'a' } - - a.equal( - #array.intersect(first_list, second_list), - 3 - ) -end) - -test('from_pairs', function(a) - a.deep_equal( - array.from_pairs({ {'a', 1}, {'b', 2} }), - { a = 1, b = 2 } - ) -end) - -test('includes', function(a) - local list = { 'a', 'b', 'c' } - - a.ok( - array.includes(list, 'c') - ) - - a.not_ok( - array.includes(list, 'd') - ) -end) - -test('each', function(a) - local list = { 'a', 'b', 'c' } - local values = {} - local keys = {} - - array.each(list, function(v, k) - table.insert(values, v) - table.insert(keys, k) - end) - - a.deep_equal(list, values) - a.deep_equal(keys, { 1, 2, 3 }) -end) - -test('reverse_each', function(a) - local values = {} - local keys = {} - - array.reverse_each({ 'a', 'b', 'c' }, function(v, k) - table.insert(values, v) - table.insert(keys, k) - end) - - a.deep_equal(values, { 'c', 'b', 'a' }) - a.deep_equal(keys, { 3, 2, 1 }) -end) From e50a9a026180765dc4cc5aede63ca7d2225a423d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Evandro=20Leopoldino=20Gon=C3=A7alves?= Date: Wed, 14 Oct 2020 20:50:24 +0200 Subject: [PATCH 08/14] update documentation --- index.html | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/index.html b/index.html index 0e0e11d..09fd44c 100644 --- a/index.html +++ b/index.html @@ -177,6 +177,11 @@

array.lua

array.from_pairs +
  • + + array.group_by + +
  • array.includes @@ -222,6 +227,16 @@

    array.lua

    array.min
  • +
  • + + array.permutation + +
  • +
  • + + array.random + +
  • array.reduce @@ -354,6 +369,12 @@

    Return a table composed from key-value pairs

  • +
    +

    + array.group_by(obj:table,callback:function):table +

    +

    Returns a new table composed by keys created from the results of running each element through `callback`

    +

    array.includes(obj:table,value:*):boolean @@ -408,6 +429,18 @@

    Return minimum value from the table

    +
    +

    + array.permutation(obj:table):table +

    +

    Creates a new table returning all permutations of the length of the elements of the given table

    +
    +
    +

    + array.random(obj:table):* +

    +

    Returns a value from a random key of the given array

    +

    array.reduce(obj:table,callback:function,memo:table):* From 838b41781e9b7a4dc6b0df03d08aeca786399249 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Evandro=20Leopoldino=20Gon=C3=A7alves?= Date: Thu, 10 Dec 2020 16:54:00 +0100 Subject: [PATCH 09/14] remove unnecessary files from gh-pages branch --- .travis.yml | 23 -- src/array/init.lua | 559 -------------------------------------------- src/array/utils.lua | 50 ---- 3 files changed, 632 deletions(-) delete mode 100644 .travis.yml delete mode 100644 src/array/init.lua delete mode 100644 src/array/utils.lua diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 75d3ce0..0000000 --- a/.travis.yml +++ /dev/null @@ -1,23 +0,0 @@ -language: python -sudo: false - -env: - - LUA="lua=5.1" - - LUA="lua=5.2" - - LUA="lua=5.3" - -before_install: - - pip install hererocks - - hererocks lua_install -r^ --$LUA - - export PATH=$PATH:$PWD/lua_install/bin - -install: - - luarocks install simple_test - -script: - - make test - -notifications: - email: - on_success: change - on_failure: always diff --git a/src/array/init.lua b/src/array/init.lua deleted file mode 100644 index 7f9d822..0000000 --- a/src/array/init.lua +++ /dev/null @@ -1,559 +0,0 @@ -local utils = require('array.utils') - -local array - -array = { - __VERSION = '1.3.1', - __DESCRIPTION = "A small library with useful methods to handle Lua's table when it's working like an Array", - __LICENSE = [[ - The MIT License (MIT) - Copyright (c) 2017 Evandro Leopoldino Gonçalves - Permission is hereby granted, free of charge, to any person obtaining a copy - of this software and associated documentation files (the "Software"), to deal - in the Software without restriction, including without limitation the rights - to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - copies of the Software, and to permit persons to whom the Software is - furnished to do so, subject to the following conditions: - The above copyright notice and this permission notice shall be included in all - copies or substantial portions of the Software. - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - SOFTWARE. - ]], - - -- Verify if table object works as an array - -- @param obj {table} - -- @return {boolean} - is_array = function(obj) - if not utils.is_table(obj) then return false end - - local i = 0 - for _ in pairs(obj) do - i = i + 1 - if obj[i] == nil then return false end - end - - return true - end, - - -- Check if parameter is an empty table - -- @param obj {table} - -- @return {boolean} - is_empty = function(obj) - return array.is_array(obj) and #obj == 0 - end, - - -- Return a shallow copy of a portion of a table into a new table - -- @param obj {table} - -- @param start {number} start value - -- @param finish {number} end value - -- @return {boolean} - slice = function(obj, start, finish) - utils.raises_error(array, obj, 'slice') - - if array.is_empty(obj) then return {} end - - local _start = start or 1 - - local output = {} - for i=_start, finish or #obj do - table.insert(output, obj[i]) - end - - return output - end, - - -- Return the index at which value can be found or -1 in case value is not present - -- @param obj {table} - -- @param value {*} - -- @return {number} - index_of = function(obj, value) - utils.raises_error(array, obj, 'index_of') - - for i=1, #obj do - if obj[i] == value then - return i - end - end - - return -1 - end, - - -- Return true whether table includes the values, otherwise it returns false - -- @param obj {table} - -- @param value {*} - -- @return {boolean} - includes = function(obj, value) - utils.raises_error(array, obj, 'includes') - - local index = array.index_of(obj, value) - - if index == -1 then return false end - return true - end, - - -- Create a new table with reverse values - -- @param obj {table} - -- @return {table} - reverse = function(obj) - utils.raises_error(array, obj, 'reverse') - - local output = {} - - for i=#obj, 1, -1 do - table.insert(output, obj[i]) - end - - return output - end, - - -- Return first element from the table - -- @param obj {table} - -- @return {*} - first = function(obj) - utils.raises_error(array, obj, 'first') - return obj[1] - end, - - -- Return last element from the table - -- @param obj {table} - -- @return {*} - last = function(obj) - utils.raises_error(array, obj, 'last') - return obj[#obj] - end, - - -- Return maximum value from the table - -- @param obj {table} - -- @return {*} - max = function(obj) - utils.raises_error(array, obj, 'max') - - local max = obj[1] - - for i=2, #obj do - if obj[i] > max then - max = obj[i] - end - end - - return max - end, - - -- Return minimum value from the table - -- @param obj {table} - -- @return {*} - min = function(obj) - utils.raises_error(array, obj, 'min') - - local min = obj[1] - - for i=2, #obj do - if obj[i] < min then - min = obj[i] - end - end - - return min - end, - - -- Create a new table of values by mapping each value in table through a transformation function - -- @param obj {table} - -- @param callback {function} - -- @return {*} - map = function(obj, callback) - utils.raises_error(array, obj, 'map') - - local initial_value = {} - local reducer = function(accumulator, current, i) - table.insert(accumulator, callback(current, i)) - return accumulator - end - - return array.reduce(obj, reducer, initial_value) - end, - - -- Create a new table containing all elements that pass truth test - -- @param obj {table} - -- @param callback {function} - -- @return {*} - filter = function(obj, callback) - utils.raises_error(array, obj, 'filter') - - local initial_value = {} - local reducer = function(accumulator, current) - if callback(current) then - table.insert(accumulator, current) - end - - return accumulator - end - - return array.reduce(obj, reducer, initial_value) - end, - - -- Applies a function against an accumulator and each value of the table to reduce it to a single value - -- @param obj {table} - -- @param callback {function} - -- @param memo {table} - -- @return {*} - reduce = function(obj, callback, memo) - utils.raises_error(array, obj, 'reduce') - - local initialIndex = 1 - local _memo = memo - - if _memo == nil then - initialIndex = 2 - _memo = obj[1] - end - - for i=initialIndex, #obj do - _memo = callback(_memo, obj[i], i) - end - - return _memo - end, - - -- This function is like reduce except that it interates over table's elements from right to left - -- @param obj {table} - -- @param callback {function} - -- @param memo {table} - -- @return {*} - reduce_right = function(obj, callback, memo) - utils.raises_error(array, obj, 'reduce_right') - - local initialIndex = #obj - local _memo = memo - - if _memo == nil then - initialIndex = initialIndex - 1 - _memo = obj[#obj] - end - - for i=initialIndex, 1, -1 do - _memo = callback(_memo, obj[i], i) - end - - return _memo - end, - - -- Return the sum of the values in table - -- @param obj {table} - -- @param callback {function} - -- @return {number} - sum = function(obj) - utils.raises_error(array, obj, 'sum') - - return array.reduce(obj, function(memo, value) - return memo + value - end) - end, - - -- Return a new table joining all values from the two tables passed by parameter - -- @param obj {table} - -- @param obj2 {table} - -- @return {table} - concat = function(obj, obj2) - utils.raises_error(array, obj, 'concat') - utils.raises_error(array, obj2, 'concat') - - local output = {} - - for i=1, #obj do - table.insert(output, obj[i]) - end - - for i=1, #obj2 do - table.insert(output, obj2[i]) - end - - return output - end, - - -- Create a new table, removing duplicates values - -- @param obj {table} - -- @return {table} - uniq = function(obj) - utils.raises_error(array, obj, 'uniq') - - local output = {} - local seen = {} - - for i=1, #obj do - local value = obj[i] - if not seen[value] then - seen[value] = true - table.insert(output, value) - end - end - - return output - end, - - -- Return a copy of the table with all instances of the values removed - -- @param obj {table} - -- @param values {table} - -- @return {table} - without = function(obj, values) - utils.raises_error(array, obj, 'without') - - local initial_value = {} - local reducer = function(accumulator, current) - if (array.includes(values, current) == false) then - table.insert(accumulator, current) - end - - return accumulator - end - - return array.reduce(obj, reducer, initial_value) - end, - - -- Tests if at least one element in the table passes the test implemented by the callback - -- @param obj {table} - -- @param callback {function} - -- @return {boolean} - some = function(obj, callback) - utils.raises_error(array, obj, 'some') - - for i=1, #obj do - if callback(obj[i], i) then - return true - end - end - - return false - end, - - -- Return a table of the two supplied by pairing up equally-positioned elements from both tables - -- @param obj {table} - -- @param obj2 {table} - -- @return {table} - zip = function(obj1, obj2) - utils.raises_error(array, obj1, 'zip') - utils.raises_error(array, obj2, 'zip') - - local output = {} - local size = #obj1 > #obj2 and #obj2 or #obj1 - - for i=1, size do - table.insert(output, { obj1[i], obj2[i] }) - end - - return output - end, - - -- Return a table of the two supplied by pairing up equally-positioned elements from both tables - -- @param obj {table} - -- @param obj2 {table} - -- @return {table} - every = function(obj, callback) - utils.raises_error(array, obj, 'every') - - for i=1, #obj do - if not callback(obj[i], i) then - return false - end - end - - return true - end, - - -- return a shallow copy of the table passed as parameter - -- @param obj {table} - -- @return {table} - shallow_copy = function(obj) - utils.raises_error(array, obj, 'shallow_copy') - - local output = {} - - for i=1, #obj do - table.insert(output, obj[i]) - end - - return output - end, - - -- Return a deep copy of the table passed as parameter - -- @param value {*} - -- @return {table} - deep_copy = function(value) - local output = value - - if utils.is_table(value) then - output = {} - - for i=1, #value do - table.insert(output, array.deep_copy(value[i])) - end - end - - return output - end, - - -- Return a new table with the items which exist only in the first table - -- @param obj {table} - -- @param obj2 {table} - -- @return {table} - diff = function(obj1, obj2) - utils.raises_error(array, obj1, 'diff') - utils.raises_error(array, obj2, 'diff') - - local output = {} - local hash = utils.convert_to_hash(obj2) - - for i=1, #obj1 do - local value = obj1[i] - - if not hash[value] then - table.insert(output, value) - end - end - - return output - end, - - -- Create a new table with the sub-table elements concatenated into it - -- @param obj {table} - -- @return {table} - flat = function(obj) - return array.reduce(obj, function(acc, item) - if array.is_array(item) then - return array.concat( - acc, - array.flat(item) - ) - else - table.insert(acc, item) - return acc - end - end, {}); - end, - - -- Creates a table filling all the elements from a start index (default one) to an end index with a default value - -- @param value {*} - -- @param start_or_finish {number} - -- @param finish {number} - -- @return {table} - fill = function(value, start_or_finish, finish) - local output = {} - local item = value - local start = start_or_finish - local size = finish - - if finish == nil then - start = 1 - size = start_or_finish - end - - for i=start, size do - output[i] = item - end - - return output - end, - - -- Remove all elements from table that @callback return thruthy for - -- and return a new table with the removed items - -- @param obj {table} - -- @param callback {function} - -- @return {table} - remove = function(obj, callback) - local output = {} - local copy = array.deep_copy(obj) - - for i=1, #copy do - local value = copy[i] - - if callback(value, i) then - table.insert(output, value) - local index = array.index_of(obj, value) - table.remove(obj, index) - end - end - - return output - end, - - -- Return a new table in hash structure, where keys represent each array value - -- and value represent the number of times the same item was found in the list - -- @param obj {table} - -- @return {table} - counter = function(obj) - local output = {} - - for i=1, #obj do - local value = obj[i] - output[value] = (output[value] and output[value] or 0) + 1 - end - - return output - end, - - -- Return a new table with the values that exist in both tables - -- @param obj1 {table} - -- @param obj2 {table} - -- @return {table} - intersect = function(obj1, obj2) - local output = {} - local count1 = array.counter(obj1) - local count2 = array.counter(obj2) - - for k, v in pairs(count1) do - if count2[k] then - local new_array = array.fill( - k, - utils.lowest_value(v, count2[k]) - ) - - utils.multiple_inserts(output, new_array) - end - end - - return output - end, - - -- Return a table composed from key-value pairs - -- @param obj1 {table} - -- @param obj2 {table} - -- @return {table} - from_pairs = function(obj) - utils.raises_error(array, obj, 'from_pairs') - - local output = {} - - for i=1, #obj do - local item = obj[i] - output[item[1]] = item[2] - end - - return output - end, - - -- Executes callback once for each table element - -- @param obj {table} - -- @param callback {function} - -- @return {void} - each = function(obj, callback) - for i=1, #obj do - callback(obj[i], i) - end - end, - - -- Executes callback once for each table element in reverse order - -- @param obj {table} - -- @param callback {function} - -- @return {void} - reverse_each = function(obj, callback) - for i = #obj, 1, -1 do - callback(obj[i], i) - end - end -} - -return array diff --git a/src/array/utils.lua b/src/array/utils.lua deleted file mode 100644 index b3ab4e4..0000000 --- a/src/array/utils.lua +++ /dev/null @@ -1,50 +0,0 @@ -return { - -- Helper function to check if value passed by parameter is a table - -- @obj {table} - -- @returns {boolean} - is_table = function(obj) - return type(obj) == 'table' - end, - - -- Raises error if @param is not an array - -- @obj {table} - -- @param {table} - -- @method {string} - -- @returns {void} - raises_error = function(obj, param, method) - assert(obj.is_array(param), string.format('%s expects an array', method)) - end, - - -- Returns lowest value between two values - -- @a {number} - -- @b {number} - -- @returns number - lowest_value = function(a, b) - return a < b and a or b - end, - - -- Makes multiple inserts in a table (array-like) - -- @obj {table} - -- @values {table} - -- @returns {void} - multiple_inserts = function(obj, values) - for i=1, #values do - local value = values[i] - table.insert(obj, value) - end - end, - - -- Convert array to hash table - -- @obj {table} - -- @returns {table} - convert_to_hash = function(obj) - local output = {} - - for i=1, #obj do - local value = obj[i] - output[value] = true - end - - return output - end -} From ed8d54d947f80d353189b782caec31788455d80b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Evandro=20Leopoldino=20Gon=C3=A7alves?= Date: Thu, 10 Dec 2020 17:01:05 +0100 Subject: [PATCH 10/14] update documentation --- index.html | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/index.html b/index.html index 09fd44c..b1a56df 100644 --- a/index.html +++ b/index.html @@ -122,6 +122,11 @@