MCPcopy Create free account
hub / github.com/Effect-TS/effect / takeN

Function takeN

packages/effect/src/MutableList.ts:418–445  ·  view source on GitHub ↗
(self: MutableList<A>, n: number)

Source from the content-addressed store, hash-verified

416 * @since 4.0.0
417 */
418export const takeN = <A>(self: MutableList<A>, n: number): Array<A> => {
419 if (n <= 0 || !self.head) return []
420 n = Math.min(n, self.length)
421 if (n === self.length && self.head?.offset === 0 && !self.head.next) {
422 const array = self.head.array
423 clear(self)
424 return array
425 }
426 const array = new Array<A>(n)
427 let index = 0
428 let chunk: MutableList.Bucket<A> | undefined = self.head
429 while (chunk) {
430 while (chunk.offset < chunk.array.length) {
431 array[index++] = chunk.array[chunk.offset]
432 if (chunk.mutable) chunk.array[chunk.offset] = undefined as any
433 chunk.offset++
434 if (index === n) {
435 self.head = chunk
436 self.length -= n
437 if (self.length === 0) clear(self)
438 return array
439 }
440 }
441 chunk = chunk.next
442 }
443 clear(self)
444 return array
445}
446
447/**
448 * Removes up to `n` elements from the beginning of the `MutableList` without

Callers 1

takeAllFunction · 0.70

Calls 1

clearFunction · 0.70

Tested by

no test coverage detected