Skip to content

Commit 985de09

Browse files
fubhytim-smart
andauthored
Keep Chunk.take and Chunk.drop valid for fractional counts (#6899)
Co-authored-by: Tim Smart <[email protected]>
1 parent 4686265 commit 985de09

3 files changed

Lines changed: 15 additions & 2 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"effect": patch
3+
---
4+
5+
Ensure `Chunk.take` and `Chunk.drop` produce valid chunks for fractional counts.

‎packages/effect/src/Chunk.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,8 @@ export const prepend: {
728728
export const take: {
729729
(n: number): <A>(self: Chunk<A>) => Chunk<A>
730730
<A>(self: Chunk<A>, n: number): Chunk<A>
731-
} = dual(2, <A>(self: Chunk<A>, n: number): Chunk<A> => {
731+
} = dual(2, <A>(self: Chunk<A>, _n: number): Chunk<A> => {
732+
const n = Math.floor(_n)
732733
if (n <= 0) {
733734
return _empty
734735
} else if (n >= self.length) {
@@ -784,7 +785,8 @@ export const take: {
784785
export const drop: {
785786
(n: number): <A>(self: Chunk<A>) => Chunk<A>
786787
<A>(self: Chunk<A>, n: number): Chunk<A>
787-
} = dual(2, <A>(self: Chunk<A>, n: number): Chunk<A> => {
788+
} = dual(2, <A>(self: Chunk<A>, _n: number): Chunk<A> => {
789+
const n = Math.floor(_n)
788790
if (n <= 0) {
789791
return self
790792
} else if (n >= self.length) {

‎packages/effect/test/Chunk.test.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,12 @@ describe("Chunk", () => {
388388
})
389389

390390
describe("take", () => {
391+
it("produces valid chunks for fractional counts", () => {
392+
const chunk = Chunk.make(1, 2, 3)
393+
deepStrictEqual(Chunk.toArray(Chunk.take(chunk, 1.5)), [1])
394+
deepStrictEqual(Chunk.toArray(Chunk.drop(chunk, 1.5)), [2, 3])
395+
})
396+
391397
describe("Given a Chunk with more elements than the amount taken", () => {
392398
it("should return the subset", () => {
393399
assertEquals(pipe(Chunk.fromArrayUnsafe([1, 2, 3]), Chunk.take(2)), Chunk.fromArrayUnsafe([1, 2]))

0 commit comments

Comments
 (0)