Skip to content

Commit

Permalink
rename: cartesian -> product #11
Browse files Browse the repository at this point in the history
  • Loading branch information
xgbuils committed May 20, 2017
1 parent f388eec commit 01539c6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 28 deletions.
4 changes: 2 additions & 2 deletions src/gen/cartesian.js → src/gen/product.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function* cartesian (...iterables) {
function* product (...iterables) {
const {length} = iterables
const iterators = iterables
.map(iterable => iterable[Symbol.iterator]())
Expand Down Expand Up @@ -43,4 +43,4 @@ function cacheToItem (cache, steps) {
return cache.map((c, i) => c[steps[i]])
}

module.exports = cartesian
module.exports = product
10 changes: 5 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const factory = require('./factory')
const Iterable = require('./core/iterable')

const cartesian = require('./gen/cartesian')
const concat = require('./gen/concat')
const drop = require('./gen/drop')
const dropWhile = require('./gen/dropWhile')
Expand All @@ -12,6 +11,7 @@ const groupBy = require('./gen/groupBy')
const map = require('./gen/map')
const padEnd = require('./gen/padEnd')
const permutations = require('./gen/permutations')
const product = require('./gen/product')
const range = require('./gen/range')
const repeat = require('./gen/repeat')
const slice = require('./gen/slice')
Expand Down Expand Up @@ -50,10 +50,6 @@ const Iterum = factory({
}
},
methods: {
cartesian: {
gen: cartesian,
validation: infiniteIterablesValidation
},
concat: {
gen: concat,
validation: infiniteIterablesValidation
Expand Down Expand Up @@ -132,6 +128,10 @@ const Iterum = factory({
permutations: {
gen: permutations
},
product: {
gen: product,
validation: infiniteIterablesValidation
},
reduce: {
fn: reduce
},
Expand Down
42 changes: 21 additions & 21 deletions test/fn/cartesian_test.js → test/fn/product_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@ const {expect} = require('chai')
const Iterum = require('../../src/index.js')
const {range} = Iterum

describe('Iterum.cartesian', function () {
describe('Iterum.product', function () {
describe('given 1 list', function () {
it('cartesian product of one iterable with one element', function () {
const iterable = Iterum([1]).cartesian()
const iterable = Iterum([1]).product()
expect([...iterable].map(e => [...e]))
.to.be.deep.equal([[1]])
})

it('cartesian product of one iterable with one element', function () {
const iterable = Iterum([1, 2, 3]).cartesian()
const iterable = Iterum([1, 2, 3]).product()
expect([...iterable].map(e => [...e]))
.to.be.deep.equal([[1], [2], [3]])
})
})
describe('given 2 lists, it makes cartesian product of these lists', function () {
it('2 no empty lists', function () {
const iterable = Iterum([1, 2]).cartesian([3, 4])
const iterable = Iterum([1, 2]).product([3, 4])
expect([...iterable].map(e => [...e]))
.to.be.deep.equal([
[1, 3],
Expand All @@ -29,25 +29,25 @@ describe('Iterum.cartesian', function () {
})

it('first list is empty', function () {
const iterable = Iterum([]).cartesian([1, 2, 3, 4])
const iterable = Iterum([]).product([1, 2, 3, 4])
expect([...iterable].map(e => [...e]))
.to.be.deep.equal([])
})

it('second list is empty', function () {
const iterable = Iterum([1, 2, 3, 4]).cartesian([])
const iterable = Iterum([1, 2, 3, 4]).product([])
expect([...iterable].map(e => [...e]))
.to.be.deep.equal([])
})

it('2 lists are empty', function () {
const iterable = Iterum([]).cartesian([])
const iterable = Iterum([]).product([])
expect([...iterable].map(e => [...e]))
.to.be.deep.equal([])
})

it('first list has one element', function () {
const iterable = Iterum([0]).cartesian([1, 2, 3, 4])
const iterable = Iterum([0]).product([1, 2, 3, 4])
expect([...iterable].map(e => [...e]))
.to.be.deep.equal([
[0, 1],
Expand All @@ -60,21 +60,21 @@ describe('Iterum.cartesian', function () {

describe('0 parameters', function () {
it('no empty list', function () {
const iterable = Iterum([1, 2, 3, 4]).cartesian()
const iterable = Iterum([1, 2, 3, 4]).product()
expect([...iterable].map(e => [...e]))
.to.be.deep.equal([[1], [2], [3], [4]])
})

it('empty list', function () {
const iterable = Iterum([]).cartesian()
const iterable = Iterum([]).product()
expect([...iterable].map(e => [...e]))
.to.be.deep.equal([])
})
})

describe('more than 2 lists', function () {
it('3 no empty lists with the same length', function () {
const iterable = Iterum([1, 2]).cartesian([3, 4], [5, 6])
const iterable = Iterum([1, 2]).product([3, 4], [5, 6])
expect([...iterable].map(e => [...e]))
.to.be.deep.equal([
[1, 3, 5],
Expand All @@ -89,7 +89,7 @@ describe('Iterum.cartesian', function () {
})

it('3 no empty lists with different length', function () {
const iterable = Iterum([1, 2]).cartesian([3], [4, 5, 6])
const iterable = Iterum([1, 2]).product([3], [4, 5, 6])
expect([...iterable].map(e => [...e]))
.to.be.deep.equal([
[1, 3, 4],
Expand All @@ -102,7 +102,7 @@ describe('Iterum.cartesian', function () {
})

it('there is an empty list', function () {
const iterable = Iterum([1, 2, 3]).cartesian(new Set([3, 4, 5, 3, 2, 4]), [], [4, 5, 6])
const iterable = Iterum([1, 2, 3]).product(new Set([3, 4, 5, 3, 2, 4]), [], [4, 5, 6])
expect([...iterable].map(e => [...e]))
.to.be.deep.equal([])
})
Expand All @@ -111,7 +111,7 @@ describe('Iterum.cartesian', function () {
it('product of potentially infinite iterables', function () {
const naturals = range(0, Infinity)
const iterable = Iterum(naturals)
.cartesian(naturals)
.product(naturals)
.take(5)
expect([...iterable].map(e => [...e]))
.to.be.deep.equal([
Expand All @@ -126,15 +126,15 @@ describe('Iterum.cartesian', function () {
describe('bad arguments', function () {
it('throws an exception when it is passed no iterable in 1st argument', function () {
function foo () {
Iterum([1, 2, 3]).cartesian(23)
Iterum([1, 2, 3]).product(23)
}
expect(foo).to.throw(TypeError,
/^23 is not an Iterable instance$/)
})

it('arguments are optional but they must be iterables', function () {
function foo () {
Iterum([23]).cartesian([], [1, 3], 'foo', null, [1])
Iterum([23]).product([], [1, 3], 'foo', null, [1])
}
expect(foo).to.throw(TypeError,
/^null is not an Iterable instance$/)
Expand All @@ -143,17 +143,17 @@ describe('Iterum.cartesian', function () {

describe('converting iterum instance to array', function () {
it('returns the same as converting [Symbol.iterator]() iterator to array', function () {
const iterable = Iterum([1, 3]).cartesian([6, 10])
const iterable = Iterum([1, 3]).product([6, 10])
const iterator = iterable[Symbol.iterator]()
expect([...iterator].map(e => [...e]))
.to.be.deep.equal([...iterable].map(e => [...e]))
})
})

describe('If cartesian instance is passed as param of Iterum', function () {
describe('If cartesian product instance is passed as param of Iterum', function () {
it('returns the same reference', function () {
const iterable = [1, 5]
const a = Iterum(iterable).cartesian()
const a = Iterum(iterable).product()
const b = Iterum(a)
expect(a).to.be.not.equal(b)
expect([...a].map(e => [...e]))
Expand All @@ -163,7 +163,7 @@ describe('Iterum.cartesian', function () {

describe('static method', function () {
it('normal behaviour', function () {
const iterable = Iterum.cartesian(new Set([3, 8, 5]), 'ac')
const iterable = Iterum.product(new Set([3, 8, 5]), 'ac')
expect([...iterable].map(e => [...e]))
.to.be.deep.equal([
[3, 'a'],
Expand All @@ -176,7 +176,7 @@ describe('Iterum.cartesian', function () {
})

it('replaces first parameter by empty iterable when is not an iterable', function () {
const iterable = Iterum.cartesian({}, 'ac')
const iterable = Iterum.product({}, 'ac')
expect([...iterable].map(e => [...e]))
.to.be.deep.equal([])
})
Expand Down

0 comments on commit 01539c6

Please sign in to comment.