Skip to content
stoic man

FP-TS partition

Partitioning an array of users based on a predicate using FP-TS

Partition.ts
import * as Array from 'fp-ts/Array';
import * as Separated from 'fp-ts/Separated';
import { pipe } from 'fp-ts/function';
 
type User = {
  name: string;
  isActive: boolean;
};
 
const users = [
  { name: 'Alice', isActive: true },
  { name: 'Bob', isActive: true },
  { name: 'Charlie', isActive: false },
];
 
function userIsActive(user: User) {
  return user.isActive;
}
 
const deactivatedUsers = pipe(
  users,
  Array.partition(userIsActive),
  Separated.left
); // [{name: 'Charlie', isActive: false}]

All snippets