Skip to content

Commit

Permalink
change(permute) rename function to include type
Browse files Browse the repository at this point in the history
permutation type is based on order of elements
renamed iter to order_iter
renamed table to order_table

kept backward compatibility in place
  • Loading branch information
Tieske committed Oct 9, 2020
1 parent b86bfb4 commit 5bbc1f9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
24 changes: 21 additions & 3 deletions lua/pl/permute.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ local assert_arg = utils.assert_arg
local permute = {}


--- an iterator over all permutations of the elements of a list.
--- an iterator over all order-permutations of the elements of a list.
-- Please note that the same list is returned each time, so do not keep references!
-- @param a list-like table
-- @return an iterator which provides the next permutation as a list
function permute.iter(a)
function permute.order_iter(a)
assert_arg(1,a,'table')

local t = #a
Expand Down Expand Up @@ -69,7 +69,7 @@ end
-- @param a list-like table
-- @return a table of tables
-- @usage permute.table {1,2,3} --> {{2,3,1},{3,2,1},{3,1,2},{1,3,2},{2,1,3},{1,2,3}}
function permute.table (a)
function permute.order_table (a)
assert_arg(1,a,'table')
local res = {}
for t in permute.iter(a) do
Expand All @@ -78,4 +78,22 @@ function permute.table (a)
return res
end



-- backward compat, to be deprecated

--- deprecated.
-- @see permute.order_iter
function permute.iter(...)
--TODO: add deprecation warning here
return permute.order_iter(...)
end

--- deprecated.
-- @see permute.order_iter
function permute.table(...)
--TODO: add deprecation warning here
return permute.order_table(...)
end

return permute
12 changes: 6 additions & 6 deletions spec/permute_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ local tcopy = require("pl.tablex").copy

describe("pl.permute", function()

describe("iter", function()
describe("order_iter", function()

it("returns all order combinations", function()
local result = {}
for list in permute.iter({"one", "two", "three"}) do
for list in permute.order_iter({"one", "two", "three"}) do
result[#result+1] = tcopy(list)
end
assert.same({
Expand Down Expand Up @@ -40,7 +40,7 @@ describe("pl.permute", function()

it("returns nil on empty list", function()
local result = {}
for list in permute.iter({}) do
for list in permute.order_iter({}) do
result[#result+1] = tcopy(list)
end
assert.equal(0, #result)
Expand All @@ -50,10 +50,10 @@ describe("pl.permute", function()



describe("table", function()
describe("order_table", function()

it("returns all order combinations", function()
local result = permute.table({"one", "two", "three"})
local result = permute.order_table({"one", "two", "three"})
assert.same({
[1] = {
[1] = 'two',
Expand Down Expand Up @@ -83,7 +83,7 @@ describe("pl.permute", function()


it("returns empty table on empty input list", function()
local result = permute.table({})
local result = permute.order_table({})
assert.same({}, result)
end)

Expand Down

0 comments on commit 5bbc1f9

Please sign in to comment.