// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.

/**
 * Returns a tuple of two arrays with the first one containing all elements in
 * the given array that match the given predicate and the second one containing
 * all that do not.
 *
 * @template T The type of the elements in the array.
 *
 * @param array The array to partition.
 * @param predicate The predicate function to determine which array an element
 * belongs to.
 *
 * @returns A tuple of two arrays. The first array contains all elements that
 * match the predicate, the second contains all elements that do not.
 *
 * @example Basic usage
 * ```ts
 * import { partition } from "@std/collections/partition";
 * import { assertEquals } from "@std/assert/assert-equals";
 *
 * const numbers = [5, 6, 7, 8, 9];
 * const [even, odd] = partition(numbers, (it) => it % 2 === 0);
 *
 * assertEquals(even, [6, 8]);
 * assertEquals(odd, [5, 7, 9]);
 * ```
 */
export function partition<T>(
  array: Iterable<T>,
  predicate: (el: T) => boolean,
): [T[], T[]];
/**
 * Returns a tuple of two arrays with the first one containing all elements in
 * the given array that match the given predicate and the second one containing
 * all that do not.
 *
 * This version of the function is a type-guard version of the function. It
 * allows you to specify a type-guard predicate function that narrows the type
 * of the elements in the array.
 *
 * @template T The type of the elements in the array.
 * @template U The type of the elements that match the predicate.
 *
 * @param array The array to partition.
 * @param predicate The type-guard predicate function to determine which array
 * an element belongs to.
 *
 * @returns A tuple of two arrays. The first array contains all elements that
 * match the predicate, the second contains all elements that do not.
 *
 * @example Basic usage
 * ```ts
 * import { partition } from "@std/collections/partition";
 * import { assertEquals } from "@std/assert/assert-equals";
 *
 * const numbers = [5, 6, 7, 8, 9];
 * const [even, odd] = partition(numbers, (it) => it % 2 === 0);
 *
 * assertEquals(even, [6, 8]);
 * assertEquals(odd, [5, 7, 9]);
 * ```
 */
export function partition<T, U extends T>(
  array: Iterable<T>,
  predicate: (el: T) => el is U,
): [U[], Exclude<T, U>[]];
export function partition(
  array: Iterable<unknown>,
  predicate: (el: unknown) => boolean,
): [unknown[], unknown[]] {
  const matches: Array<unknown> = [];
  const rest: Array<unknown> = [];

  for (const element of array) {
    if (predicate(element)) {
      matches.push(element);
    } else {
      rest.push(element);
    }
  }

  return [matches, rest];
}

// denoCacheMetadata={"headers":{"content-type":"text/typescript","x-robots-tag":"noindex","cross-origin-resource-policy":"cross-origin","date":"Tue, 28 Jul 2026 12:31:15 GMT","set-cookie":"__cf_bm=hjEZxXWiQ48xgwHBVx3EkhWDW1TNRyww_XZx_bAz7vs-1785241874.9619758-1.0.1.1-_SMLfxvRxELM8Qm.GELvqrgFugOh.EVj_9GsFW6oKoVDqb5xsE.meKGepzEEExNqRegR.g6DBdIoAlBbRYJGiHGWjZFCeyuQeKgw0o28TD1KVg1GagzpDxdlhJwqpIIy; HttpOnly; SameSite=None; Secure; Path=/; Domain=jsr.io; Expires=Tue, 28 Jul 2026 13:01:15 GMT","nel":"{\"report_to\":\"cf-nel\",\"success_fraction\":0.0,\"max_age\":604800}","server":"cloudflare","alt-svc":"h3=\":443\"; ma=86400","report-to":"{\"group\":\"cf-nel\",\"max_age\":604800,\"endpoints\":[{\"url\":\"https://a.nel.cloudflare.com/report/v4?s=iog8xEm0eOGLUCeXQo7tElobrnf8CzEiJ8Ii1ZEXuELhgPcnm26QD%2FqEEbYRBRkhVLyFEMyZNcnY37vnpIgqQ5bElezdCDJaAgZ9x3DQrBINSqlaw%2F4gAiM%3D\"}]}","cf-ray":"a223e6564f287db6-LAX","vary":"Origin","access-control-expose-headers":"*","etag":"\"6492b8aa9de885a12ba6ba2a7b8657de\"","access-control-allow-origin":"*","content-security-policy":"default-src 'none'; script-src 'none'; style-src 'none'; img-src 'none'; font-src 'none'; connect-src 'none'; frame-src 'none'; object-src 'none'; frame-ancestors 'none'; sandbox; form-action 'none';","x-jsr-backend":"modules","content-length":"2705","cache-control":"public, max-age=31536000, immutable","x-content-type-options":"nosniff"},"url":"https://jsr.io/@std/collections/0.224.2/partition.ts","time":1785241875}