Hyperlinkv0.8.0-beta.28

Array

Array.splitAtconsteffect/Array.ts:2667
(n: number): <A>(
  self: Iterable<A>
) => [beforeIndex: Array<A>, fromIndex: Array<A>]
<A>(self: Iterable<A>, n: number): [
  beforeIndex: Array<A>,
  fromIndex: Array<A>
]

Splits an iterable into two arrays at the given index.

When to use

Use to divide an array into a prefix and suffix at a specific position.

Details

n can be 0, in which case all elements are placed in the second array. The index is floored to an integer.

Example (Splitting at an index)

import { Array } from "effect"

console.log(Array.splitAt([1, 2, 3, 4, 5], 3)) // [[1, 2, 3], [4, 5]]
Source effect/Array.ts:266714 lines
export const splitAt: {
  (n: number): <A>(self: Iterable<A>) => [beforeIndex: Array<A>, fromIndex: Array<A>]
  <A>(self: Iterable<A>, n: number): [beforeIndex: Array<A>, fromIndex: Array<A>]
} = dual(2, <A>(self: Iterable<A>, n: number): [Array<A>, Array<A>] => {
  const input = Array.from(self)
  const _n = Math.floor(n)
  if (isReadonlyArrayNonEmpty(input)) {
    if (_n >= 1) {
      return splitAtNonEmpty(input, _n)
    }
    return [[], input]
  }
  return [input, []]
})
Referenced by 1 symbols