Hyperlinkv0.8.0-beta.28

Chunk

Chunk.takeconsteffect/Chunk.ts:742
(n: number): <A>(self: Chunk<A>) => Chunk<A>
<A>(self: Chunk<A>, n: number): Chunk<A>

Takes the first up to n elements from the chunk.

Example (Taking elements from the start)

import { Chunk } from "effect"

const chunk = Chunk.make(1, 2, 3, 4, 5)
const result = Chunk.take(chunk, 3)
console.log(Chunk.toArray(result)) // [1, 2, 3]
elements
Source effect/Chunk.ts:74240 lines
export const take: {
  (n: number): <A>(self: Chunk<A>) => Chunk<A>
  <A>(self: Chunk<A>, n: number): Chunk<A>
} = dual(2, <A>(self: Chunk<A>, n: number): Chunk<A> => {
  if (n <= 0) {
    return _empty
  } else if (n >= self.length) {
    return self
  } else {
    switch (self.backing._tag) {
      case "ISlice": {
        return makeChunk({
          _tag: "ISlice",
          chunk: self.backing.chunk,
          length: n,
          offset: self.backing.offset
        })
      }
      case "IConcat": {
        if (n > self.left.length) {
          return makeChunk({
            _tag: "IConcat",
            left: self.left,
            right: take(self.right, n - self.left.length)
          })
        }

        return take(self.left, n)
      }
      default: {
        return makeChunk({
          _tag: "ISlice",
          chunk: self,
          offset: 0,
          length: n
        })
      }
    }
  }
})
Referenced by 9 symbols