Skip to main content
Module

x/fun/mod.ts>array.partitionMap

A collection of algebraic data types, lenses, and schemables based on a light weight higher kinded type implementation. Written for deno.
Latest
function array.partitionMap
import { array } from "https://deno.land/x/fun@v2.0.0/mod.ts";
const { partitionMap } = array;

Partition and map over a ReadonlyArray in the same loop. Given a predicate A => Either<J, I>, this function passes each element in an array into the predicate. If the predicate returns Right then the inner I is pushed into the first array in a pair. If the predicate returns Left then the inner J is pushed into the second array in a pair.

Examples

Example 1

import * as A from "./array.ts";
import * as E from "./either.ts";
import { pipe } from "./fn.ts";

const result = pipe(
  A.range(10, 1), // [1, 2, 3, ..., 10]
  A.partitionMap(n => n % 2 === 0 ? E.right(n * 100) : E.left(n / 10)),
); // Pair<[200, 400, 600, 800, 1000], [0.1, 0.3, 0.5, 0.7, 0.9]>

Parameters

predicate: (a: A, index: number) => Either<J, I>

Returns

(ua: ReadonlyArray<A>) => Pair<ReadonlyArray<I>, ReadonlyArray<J>>