Skip to content

Commit

Permalink
fix chunksOf implementation, fix #897
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti committed Jul 5, 2019
1 parent 181e23f commit dbe79bd
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 36 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ high state of flux, you're at risk of it changing without notice.

- **Bug Fix**
- fix `PipeableBifunctor` definition (@gcanti)
- fix `chunksOf` implementation, fix #897 (@gcanti)

# 1.19.3

Expand Down
2 changes: 1 addition & 1 deletion src/Array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1375,7 +1375,7 @@ export const split = <A>(n: number, as: Array<A>): [Array<A>, Array<A>] => {

function _chunksOf<A>(as: Array<A>, n: number): Array<Array<A>> {
// tslint:disable-next-line: deprecation
return isOutOfBound(n - 1, as) ? [as] : chop(as, as => split(n, as))
return as.length === 0 ? empty : isOutOfBound(n - 1, as) ? [as] : chop(as, as => split(n, as))
}

/**
Expand Down
90 changes: 56 additions & 34 deletions test/Array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ import { fold as foldMonoid, monoidString, monoidSum } from '../src/Monoid'
import { fromPredicate, getEq as getOptionEq, none, option, Option, some } from '../src/Option'
import { ord, ordNumber, ordString } from '../src/Ord'
import { showString } from '../src/Show'
import { isDeepStrictEqual } from 'util'

const p = (n: number) => n > 2

Expand Down Expand Up @@ -510,6 +511,7 @@ describe('Array', () => {
})

it('foldMap', () => {
// tslint:disable-next-line: deprecation
const old = F.foldMap(array, monoidString)
const foldMap = array.foldMap(monoidString)
const x1 = ['a', 'b', 'c']
Expand All @@ -522,6 +524,7 @@ describe('Array', () => {
})

it('foldr', () => {
// tslint:disable-next-line: deprecation
const old = F.foldr(array)
const foldr = array.foldr
const x1 = ['a', 'b', 'c']
Expand Down Expand Up @@ -760,41 +763,60 @@ describe('Array', () => {
assert.deepStrictEqual(splitAt(3)([1, 2]), [[1, 2], []])
})

it('chunksOf', () => {
// tslint:disable-next-line: deprecation
assert.deepStrictEqual(chunksOf([1, 2, 3, 4, 5], 2), [[1, 2], [3, 4], [5]])
// tslint:disable-next-line: deprecation
assert.deepStrictEqual(chunksOf([1, 2, 3, 4, 5, 6], 2), [[1, 2], [3, 4], [5, 6]])
// tslint:disable-next-line: deprecation
assert.deepStrictEqual(chunksOf([1, 2, 3, 4, 5], 5), [[1, 2, 3, 4, 5]])
// tslint:disable-next-line: deprecation
assert.deepStrictEqual(chunksOf([1, 2, 3, 4, 5], 6), [[1, 2, 3, 4, 5]])
// tslint:disable-next-line: deprecation
assert.deepStrictEqual(chunksOf([1, 2, 3, 4, 5], 1), [[1], [2], [3], [4], [5]])
// tslint:disable-next-line: deprecation
assert.deepStrictEqual(chunksOf([], 1), [[]])
// tslint:disable-next-line: deprecation
assert.deepStrictEqual(chunksOf([], 2), [[]])
// tslint:disable-next-line: deprecation
assert.deepStrictEqual(chunksOf([], 0), [[]])
// tslint:disable-next-line: deprecation
assert.deepStrictEqual(chunksOf([1, 2], 0), [[1, 2]])
// tslint:disable-next-line: deprecation
assert.deepStrictEqual(chunksOf([1, 2], 10), [[1, 2]])
// tslint:disable-next-line: deprecation
assert.deepStrictEqual(chunksOf([1, 2], -1), [[1, 2]])
describe('chunksOf', () => {
it('should split an array into length-n pieces', () => {
// tslint:disable-next-line: deprecation
assert.deepStrictEqual(chunksOf([1, 2, 3, 4, 5], 2), [[1, 2], [3, 4], [5]])
// tslint:disable-next-line: deprecation
assert.deepStrictEqual(chunksOf([1, 2, 3, 4, 5, 6], 2), [[1, 2], [3, 4], [5, 6]])
// tslint:disable-next-line: deprecation
assert.deepStrictEqual(chunksOf([1, 2, 3, 4, 5], 5), [[1, 2, 3, 4, 5]])
// tslint:disable-next-line: deprecation
assert.deepStrictEqual(chunksOf([1, 2, 3, 4, 5], 6), [[1, 2, 3, 4, 5]])
// tslint:disable-next-line: deprecation
assert.deepStrictEqual(chunksOf([1, 2, 3, 4, 5], 1), [[1], [2], [3], [4], [5]])
// tslint:disable-next-line: deprecation
assert.deepStrictEqual(chunksOf([1, 2], 0), [[1, 2]])
// tslint:disable-next-line: deprecation
assert.deepStrictEqual(chunksOf([1, 2], 10), [[1, 2]])
// tslint:disable-next-line: deprecation
assert.deepStrictEqual(chunksOf([1, 2], -1), [[1, 2]])

assert.deepStrictEqual(chunksOf(2)([1, 2, 3, 4, 5]), [[1, 2], [3, 4], [5]])
assert.deepStrictEqual(chunksOf(2)([1, 2, 3, 4, 5, 6]), [[1, 2], [3, 4], [5, 6]])
assert.deepStrictEqual(chunksOf(5)([1, 2, 3, 4, 5]), [[1, 2, 3, 4, 5]])
assert.deepStrictEqual(chunksOf(6)([1, 2, 3, 4, 5]), [[1, 2, 3, 4, 5]])
assert.deepStrictEqual(chunksOf(1)([1, 2, 3, 4, 5]), [[1], [2], [3], [4], [5]])
assert.deepStrictEqual(chunksOf(0)([1, 2]), [[1, 2]])
assert.deepStrictEqual(chunksOf(10)([1, 2]), [[1, 2]])
assert.deepStrictEqual(chunksOf(-1)([1, 2]), [[1, 2]])
})

// #897
it('returns an empty array if provided an empty array', () => {
assert.deepStrictEqual(chunksOf(1)([]), [])
assert.deepStrictEqual(chunksOf(2)([]), [])
assert.deepStrictEqual(chunksOf(0)([]), [])
})

assert.deepStrictEqual(chunksOf(2)([1, 2, 3, 4, 5]), [[1, 2], [3, 4], [5]])
assert.deepStrictEqual(chunksOf(2)([1, 2, 3, 4, 5, 6]), [[1, 2], [3, 4], [5, 6]])
assert.deepStrictEqual(chunksOf(5)([1, 2, 3, 4, 5]), [[1, 2, 3, 4, 5]])
assert.deepStrictEqual(chunksOf(6)([1, 2, 3, 4, 5]), [[1, 2, 3, 4, 5]])
assert.deepStrictEqual(chunksOf(1)([1, 2, 3, 4, 5]), [[1], [2], [3], [4], [5]])
assert.deepStrictEqual(chunksOf(1)([]), [[]])
assert.deepStrictEqual(chunksOf(2)([]), [[]])
assert.deepStrictEqual(chunksOf(0)([]), [[]])
assert.deepStrictEqual(chunksOf(0)([1, 2]), [[1, 2]])
assert.deepStrictEqual(chunksOf(10)([1, 2]), [[1, 2]])
assert.deepStrictEqual(chunksOf(-1)([1, 2]), [[1, 2]])
// #897
it('should respect the law: chunksOf(n)(xs).concat(chunksOf(n)(ys)) == chunksOf(n)(xs.concat(ys)))', () => {
const xs: Array<number> = []
const ys = [1, 2]
assert.deepStrictEqual(chunksOf(2)(xs).concat(chunksOf(2)(ys)), chunksOf(2)(xs.concat(ys)))
fc.assert(
fc.property(
fc.array(fc.integer()).filter(xs => xs.length % 2 === 0), // Ensures `xs.length` is even
fc.array(fc.integer()),
fc.integer(1, 1).map(x => x * 2), // Generates `n` to be even so that it evenly divides `xs`
(xs, ys, n) => {
const as = chunksOf(n)(xs).concat(chunksOf(n)(ys))
const bs = chunksOf(n)(xs.concat(ys))
isDeepStrictEqual(as, bs)
}
)
)
})
})

it('makeBy', () => {
Expand Down
4 changes: 3 additions & 1 deletion test/NonEmptyArray2v.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import * as C from '../src/Const'
import { eqNumber } from '../src/Eq'
import { showString } from '../src/Show'

describe.only('NonEmptyArray2v', () => {
describe('NonEmptyArray2v', () => {
it('make', () => {
// tslint:disable-next-line: deprecation
assert.deepStrictEqual(make(1, [2]), fromNonEmptyArray([1, 2]))
Expand Down Expand Up @@ -137,6 +137,7 @@ describe.only('NonEmptyArray2v', () => {
})

it('foldMap', () => {
// tslint:disable-next-line: deprecation
const old = F.foldMap(nonEmptyArray, monoidString)
const foldMap = nonEmptyArray.foldMap(monoidString)
const x1 = fromNonEmptyArray(['a', 'b', 'c'])
Expand All @@ -146,6 +147,7 @@ describe.only('NonEmptyArray2v', () => {
})

it('foldr', () => {
// tslint:disable-next-line: deprecation
const old = F.foldr(nonEmptyArray)
const foldr = nonEmptyArray.foldr
const x1 = fromNonEmptyArray(['a', 'b', 'c'])
Expand Down

0 comments on commit dbe79bd

Please sign in to comment.