Commit 76228660 authored by Mo Zhou's avatar Mo Zhou

Update upstream source from tag 'upstream/2.1.0'

Update to upstream version '2.1.0'
with Debian dir b6eb886bcea386f260defa60dd7f1a8ad25ffcb1
parents 90966232 51da10ea
# Version history
## 2.1.0 (09/12/2018)
### Breaking changes
* Renamed `toArray` to `pack`
* Renamed `reduceby` to `reduceBy`
* Removed `skip` as alias to `last`
* Changed prototype : `each(t, f, ...)` is now `each(t, f)`
* Changed prototype : `eachi(t, f, ...)` is now `eachi(t, f)`
* Changed prototype : `adjust(t, key, f, ...)` is now `adjust(t, key, f)`
* Changed prototype : `countf(t, f, ...)` is now `countf(t, f)`
* Changed prototype : `map(t, f, ...)` is now `map(t, f)`
* Changed prototype : `reduceBy(t, f, pred, state, ...)` is now `reduceBy(t, f, pred, state)`
* Changed prototype : `select(t, f, ...)` is now `select(t, f)`
* Changed prototype : `reject(t, f, ...)` is now `reject(t, f)`
* Changed prototype : `all(t, f, ...)` is now `all(t, f)`
* Changed prototype : `invoke(t, method, ...)` is now `invoke(t, method)`
* Changed prototype : `min(t, transform, ...)` is now `min(t, transform)`
* Changed prototype : `max(t, transform, ...)` is now `max(t, transform)`
* Changed prototype : `countBy(t, iter, ...)` is now `countBy(t, iter)`
* Changed prototype : `groupBy(t, iter, ...)` is now `groupBy(t, iter)`
* Changed prototype : `selectWhile(array, f, ...)` is now `selectWhile(array, f)`
* Changed prototype : `dropWhile(array, f, ...)` is now `dropWhile(array, f)`
* Changed prototype : `findIndex(array, pred, ...)` is now `findIndex(array, pred)`
* Changed prototype : `findLastIndex(array, pred, ...)` is now `findLastIndex(array, pred)`
* Changed prototype : `chunk(array, f, ...)` is now `chunk(array, f)`
* Changed prototype : `times(iter, n, ...)` is now `times(iter, n)`
* Changed prototype : `template(id, ...)` is now `template(id)`
* Changed prototype : `tap(obj, f, ...)` is now `tap(obj, f)`
* Changed prototype : `result(obj, method, ...)` is now `result(obj, method)`
* Changed prototype : `intersection(array, ...)` is now `intersection(array)`
### Other changes
* Renamed `array` to `tabulate`, with no alias
* Moved `invert` to object functions
### Additions
* Added `best` in table functions
* Added `allEqual` in table functions
* Added `sortedk` iterator in array functions
* Added `sortedv` iterator in array functions
* Added `disjoint` in array functions
* Added `nsorted` in array functions
* Added `duplicates` in array functions
* Added `xpairs` in array functions
* Added `xpairsRight` in array functions
* Added `call` in utility functions
* Added `type` in object functions
* Added `spreadPath` in object functions
* Added `flattenPath` in object functions
* Added `thread` in utility functions
* Added `threadRight` in utility functions
* Added `iterlen` in utility functions
* Added `skip` in utility functions
* Added `both` in utility functions
* Added `either` in utility functions
* Added `neither` in utility functions
* Added `dispatch` in utility functions
* Added `noarg` in utility functions
## 2.0.0 (08/23/2018)
### Breaking changes
* library functions now accept iterators prototyped as `f(v, k, ...)` instead of `f(k, v, ...)`.
......
[![Build Status](https://travis-ci.org/Yonaba/Moses.png)](https://travis-ci.org/Yonaba/Moses)
[![Latest Stable](https://img.shields.io/badge/Latest_Stable-2.1.0-blue.svg)](https://github.com/Yonaba/Moses/releases/tag/Moses-2.1.0-1)
[![License](http://img.shields.io/badge/Licence-MIT-brightgreen.svg)](LICENSE)
[![Lua](https://img.shields.io/badge/Lua-5.1%2C%205.2%2C%205.3%2C%20JIT-blue.svg)]()
......@@ -47,7 +48,7 @@ Feel free to download and try it on your own!
## Download
### Archive
* __2.0.0__ *(latest stable)*: [zip](http://github.com/Yonaba/Moses/archive/Moses-2.0.0-1.zip) | [tar.gz](http://github.com/Yonaba/Moses/archive/Moses-2.0.0-1.tar.gz)
* __2.1.0__ *(latest stable)*: [zip](http://github.com/Yonaba/Moses/archive/Moses-2.1.0-1.zip) | [tar.gz](http://github.com/Yonaba/Moses/archive/Moses-2.1.0-1.tar.gz)
* __Previous versions__ : [tags](http://github.com/Yonaba/Moses/tags)
### Bash
......@@ -74,7 +75,7 @@ moonrocks install moses
local M = require "moses"
````
*Note:* the full source [moses.lua](https://github.com/Yonaba/Moses/blob/master/moses.lua) is quite heavy (~83 kiB, 2780 LOC). You can alternatively use the [minified version](https://github.com/Yonaba/Moses/blob/master/moses_min.lua) (~32 kiB, 521 LOC).
*Note:* the full source [moses.lua](https://github.com/Yonaba/Moses/blob/master/moses.lua) is quite heavy (~92 kiB, 3115 LOC). You can alternatively use the [minified version](https://github.com/Yonaba/Moses/blob/master/moses_min.lua) (~35 kiB, 561 LOC).
## Tutorial
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
package = "moses"
version = "2.1.0-1"
source = {
url = "https://github.com/Yonaba/Moses/archive/Moses-2.1.0-1.tar.gz",
dir = "Moses-Moses-2.1.0-1"
}
description = {
summary = "Utility-belt library for functional programming in Lua",
detailed = [[
A utility-belt library for functional programming, which complements the built-in
Lua table library, making easier operations on arrays, lists, collections.
]],
homepage = "http://yonaba.github.com/Moses/",
license = "MIT <http://www.opensource.org/licenses/mit-license.php>"
}
dependencies = {
"lua >= 5.1, < 5.4"
}
build = {
type = "builtin",
modules = {
["moses"] = "moses.lua",
["moses_min"] = "moses_min.lua",
},
copy_directories = {"doc","spec"}
}
\ No newline at end of file
......@@ -45,6 +45,18 @@ describe('Array functions specs', function()
end)
end)
describe('nsorted', function()
it('returns the top n-values from an array', function()
local array = M.range(1,20)
assert.is_true(M.isEqual(M.nsorted(array,5),{1,2,3,4,5}))
local function comp(a,b) return a > b end
assert.is_true(M.isEqual(M.nsorted(array,3,comp),{20,19,18}))
end)
end)
describe('shuffle', function()
......@@ -67,15 +79,15 @@ describe('Array functions specs', function()
end)
describe('toArray', function()
describe('pack', function()
it('converts a vararg list to an array', function()
assert.is_true(M.isArray(M.toArray(1,2,3,4)))
assert.is_true(M.isEqual(M.toArray(1,2,8,'d','a',0),{1,2,8,'d','a',0}))
assert.is_true(M.isArray(M.pack(1,2,3,4)))
assert.is_true(M.isEqual(M.pack(1,2,8,'d','a',0),{1,2,8,'d','a',0}))
end)
it('preserves input order', function()
local args = M.toArray(1,2,3,4,5)
local args = M.pack(1,2,3,4,5)
for i = 1, 5 do assert.equal(args[i], i) end
end)
......@@ -533,6 +545,20 @@ describe('Array functions specs', function()
end)
describe('disjoint',function()
it('checks if all passed-in arrays are disjoint', function()
local A = {'a'}
local B = {'a',1,3}
local C = {3,10,2}
assert.is_false(M.disjoint(A,B))
assert.is_true(M.disjoint(A,C))
assert.is_false(M.disjoint(B,C))
end)
end)
describe('symmetricDifference',function()
it('returns the symmetric difference from two arrays', function()
......@@ -561,6 +587,13 @@ describe('Array functions specs', function()
end)
describe('duplicates',function()
it('returns a list of all duplicates in array', function()
assert.is_true(M.isEqual(M.duplicates({1,2,3,3,8,8,3,2,4}),{2,3,8}))
assert.is_true(M.isEqual(M.duplicates({true, false, true, 1, '5', '1', '5'}),{true, '5'}))
end)
end)
describe('zip',function()
it('zips together values from different arrays sharing the same index', function()
local names = {'Bob','Alice','James'}; local ages = {22, 23}
......@@ -786,16 +819,7 @@ describe('Array functions specs', function()
assert.is_true(#perm == 0)
end)
end)
describe('invert',function()
it('switches key-values pairs', function()
assert.is_true(M.isEqual(M.invert({1,2,3}),{1,2,3}))
assert.is_true(M.isEqual(M.invert({'a','b','c'}),{a = 1,b = 2,c = 3}))
end)
end)
end)
describe('concat',function()
......@@ -835,6 +859,28 @@ describe('Array functions specs', function()
end)
describe('xpairs',function()
it('create pairs by prepending value to array values', function()
local r = M.xpairs(1,{1,2,3})
assert.is_true(M.isEqual(r[1],{1,1}))
assert.is_true(M.isEqual(r[2],{1,2}))
assert.is_true(M.isEqual(r[3],{1,3}))
end)
end)
describe('xpairsRight',function()
it('create pairs by appending value to array values', function()
local r = M.xpairsRight(1,{1,2,3})
assert.is_true(M.isEqual(r[1],{1,1}))
assert.is_true(M.isEqual(r[2],{2,1}))
assert.is_true(M.isEqual(r[3],{3,1}))
end)
end)
describe('sum',function()
it('returns the sum of array values', function()
......
......@@ -27,13 +27,12 @@ describe('Utility functions specs', function()
end)
describe('constant', function()
describe('call', function()
it('creates a constant function',function()
local gravity = M.constant(9.81)
assert.equal(gravity(),9.81)
assert.equal(gravity(10), 9.81)
assert.equal(gravity(nil), 9.81)
it('calls f(...) and returns the results',function()
assert.equal(M.call(math.pow, 2, 3), 8)
assert.equal(M.call(string.len, 'hello' ), 5)
assert.equal(M.call(table.concat, {1,2,3,4,5}, ',', 2, 4),"2,3,4")
end)
end)
......@@ -59,6 +58,64 @@ describe('Utility functions specs', function()
end)
describe('thread', function()
it('threads a value through functions',function()
local function inc(x) return x + 1 end
local function double(x) return 2 * x end
local function square(x) return x * x end
assert.equal(M.thread(2, inc, double, square), 36)
assert.equal(M.thread(3, double, inc, square), 49)
assert.equal(M.thread(4, square, double, inc), 33)
assert.equal(M.thread(5, square, inc, double), 52)
end)
it('accepts funcs taking more than one arg',function()
local function inc(x) return x + 1 end
local function add(x, y) return x + y end
local function pow(x, y) return x ^ y end
assert.equal(M.thread(2, inc, {add, 3}, {pow, 2}), 36)
assert.equal(M.thread(2, {add, 4}, inc, {pow, 2}), 49)
end)
end)
describe('threadRight', function()
it('threads a value through functions',function()
local function inc(x) return x + 1 end
local function double(x) return 2 * x end
local function square(x) return x * x end
assert.equal(M.threadRight(2, inc, double, square), 36)
assert.equal(M.threadRight(3, double, inc, square), 49)
assert.equal(M.threadRight(4, square, double, inc), 33)
assert.equal(M.threadRight(5, square, inc, double), 52)
end)
it('accepts funcs taking more than one arg',function()
local function inc(x) return x + 1 end
local function add(x, y) return x + y end
local function pow(x, y) return x ^ y end
assert.equal(M.threadRight(2, inc, {add, 3}, {pow, 2}), 64)
assert.equal(M.threadRight(2, {add, 4}, inc, {pow, 2}), 128)
end)
end)
describe('dispatch', function()
it('produces a dispatch function',function()
local f = M.dispatch(
function() return nil end,
function (v) return v+1 end,
function (v) return 2*v end
)
assert.equal(f(5),6)
assert.equal(f(7),8)
end)
end)
describe('memoize', function()
local fib_time, fib_value, mfib_time, mfib_value
......@@ -301,6 +358,48 @@ describe('Utility functions specs', function()
end)
describe('both', function()
it('returns a truthy func when all funcs returns true',function()
local f = M.both(
function(x) return x > 0 end,
function(x) return x < 10 end,
function(x) return x % 2 == 0 end
)
assert.is_true(f(2))
assert.is_true(f(8))
assert.is_false(f(9))
end)
end)
describe('either', function()
it('returns a truthy func when at least one of its funcs returns true',function()
local f = M.either(
function(x) return x > 0 end,
function(x) return x % 2 == 0 end
)
assert.is_true(f(0))
assert.is_false(f(-3))
end)
end)
describe('neither', function()
it('returns a truthy func when neither of its funcs returns true',function()
local f = M.neither(
function(x) return x > 10 end,
function(x) return x % 2 == 0 end
)
assert.is_false(f(12))
assert.is_false(f(8))
assert.is_true(f(7))
end)
end)
describe('uniqueId', function()
it('returns an unique (for the current session) integer Id',function()
......@@ -355,19 +454,56 @@ describe('Utility functions specs', function()
end)
end)
describe('skip', function()
it('consumes the first n values of an iterator',function()
local w = "hello"
local char = string.gmatch(w,'.')
local iter = M.skip(char, 3)
assert.equal(iter(), 'l')
assert.equal(iter(), 'o')
end)
it('consumes the first n values of an iterator',function()
local w = "lua"
local char = string.gmatch(w,'.')
local iter = M.skip(char)
assert.equal(iter(), 'u')
assert.equal(iter(), 'a')
end)
end)
describe('array', function()
describe('tabulate', function()
it('iterates a given iterator and returns its values in an array',function()
local letters = M.array(('Lua'):gmatch('.'))
local letters = M.tabulate(('Lua'):gmatch('.'))
assert.is_true(M.isEqual(letters,{'L','u','a'}))
local numbers = M.array(pairs(M.range(1,10)))
local numbers = M.tabulate(pairs(M.range(1,10)))
assert.is_true(M.isEqual(numbers,M.range(1,10)))
end)
end)
describe('iterlen', function()
it('returns the iterator length',function()
local text = 'letters'
local chars = string.gmatch(text, '.')
assert.equal(M.iterlen(chars),7)
end)
it('it consumes the iterator',function()
local text = 'lua'
local chars = string.gmatch(text, '.')
assert.equal(M.iterlen(chars),3)
assert.is_nil(chars())
end)
end)
describe('castArray', function()
it('converts value to an array',function()
......@@ -439,7 +575,18 @@ describe('Utility functions specs', function()
end)
describe('rearg', function()
describe('noarg', function()
it('returns a function with an arity of 0',function()
local f = M.noarg(function (x) return x or 'default' end)
assert.equal(f(1), 'default')
assert.equal(f(function() end, 3), 'default')
assert.equal(f(nil), 'default')
end)
end)
describe('rearg', function()
it('creates a function with args reordered',function()
local f = M.rearg(function(...) return ... end, {3,2,1})
......
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment