Functional programming library for TypeScript with higher-kinded types, algebraic data types, and type classes
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Core function manipulation and composition utilities providing the foundation for functional programming patterns in fp-ts.
Essential utility functions for functional programming.
/**
* Identity function - returns input unchanged
* @param a - Input value
* @returns Same value
*/
function identity<A>(a: A): A;
/**
* Constant function - always returns the same value
* @param a - Value to return
* @returns Function that always returns a
*/
function constant<A>(a: A): Lazy<A>;
/**
* Unsafe type coercion (use with caution)
* @param a - Value to coerce
* @returns Value with different type
*/
function unsafeCoerce<A, B>(a: A): B;Usage Examples:
import { identity, constant } from "fp-ts/lib/function";
// Identity function
const value = identity(42); // 42
const same = [1, 2, 3].map(identity); // [1, 2, 3]
// Constant function
const alwaysFive = constant(5);
const five1 = alwaysFive(); // 5
const five2 = alwaysFive(); // 5
// Use in array operations
const allFives = [1, 2, 3].map(() => alwaysFive()); // [5, 5, 5]Type definitions for common function patterns.
/**
* Lazy computation type - function with no parameters
*/
type Lazy<A> = () => A;
/**
* N-ary function type
*/
type FunctionN<A extends Array<unknown>, B> = (...args: A) => B;
/**
* Predicate function type
*/
type Predicate<A> = (a: A) => boolean;
/**
* Type refinement predicate
*/
type Refinement<A, B extends A> = (a: A) => a is B;
/**
* Endomorphism - function from type to itself
*/
type Endomorphism<A> = (a: A) => A;Functions for working with predicates.
/**
* Logical NOT for predicates
* @param predicate - Predicate to negate
* @returns Negated predicate
*/
function not<A>(predicate: Predicate<A>): Predicate<A>;Usage Examples:
import { not } from "fp-ts/lib/function";
// Create predicate
const isEven = (n: number) => n % 2 === 0;
const isOdd = not(isEven);
// Use predicates
const evenNumbers = [1, 2, 3, 4, 5].filter(isEven); // [2, 4]
const oddNumbers = [1, 2, 3, 4, 5].filter(isOdd); // [1, 3, 5]Functions that generate constant values.
/**
* Function that always returns true
* @returns true
*/
function constTrue(): boolean;
/**
* Function that always returns false
* @returns false
*/
function constFalse(): boolean;
/**
* Function that always returns null
* @returns null
*/
function constNull(): null;
/**
* Function that always returns undefined
* @returns undefined
*/
function constUndefined(): undefined;
/**
* Function that always returns void (undefined)
* @returns undefined
*/
function constVoid(): void;Usage Examples:
import { constTrue, constFalse, constNull } from "fp-ts/lib/function";
// Use as default values or fallbacks
const alwaysTrue = constTrue(); // true
const alwaysFalse = constFalse(); // false
const alwaysNull = constNull(); // null
// Use in array operations
const flags = [1, 2, 3].map(constTrue); // [true, true, true]
const nulls = [1, 2, 3].map(constNull); // [null, null, null]Compose functions together (note: some composition utilities are in pipeable).
/**
* Function composition - (b -> c) -> (a -> b) -> (a -> c)
* @param bc - Function from B to C
* @param ab - Function from A to B
* @returns Composed function from A to C
*/
function compose<A, B, C>(bc: (b: B) => C, ab: (a: A) => B): (a: A) => C;
/**
* Reverse function composition - (a -> b) -> (b -> c) -> (a -> c)
* @param ab - Function from A to B
* @param bc - Function from B to C
* @returns Composed function from A to C
*/
function pipe<A, B, C>(ab: (a: A) => B, bc: (b: B) => C): (a: A) => C;Functions for working with tuples.
/**
* Create tuple from two values
* @param a - First value
* @param b - Second value
* @returns Tuple [a, b]
*/
function tuple<A, B>(a: A, b: B): [A, B];
/**
* Curry a two-argument function
* @param f - Function taking two arguments
* @returns Curried function
*/
function curry<A, B, C>(f: (a: A, b: B) => C): (a: A) => (b: B) => C;
/**
* Uncurry a curried function
* @param f - Curried function
* @returns Function taking two arguments
*/
function uncurry<A, B, C>(f: (a: A) => (b: B) => C): (a: A, b: B) => C;Usage Examples:
import { tuple, curry, uncurry } from "fp-ts/lib/function";
// Create tuples
const pair = tuple(1, "hello"); // [1, "hello"]
const coordinates = tuple(10, 20); // [10, 20]
// Curry functions
const add = (x: number, y: number) => x + y;
const curriedAdd = curry(add);
const addFive = curriedAdd(5);
const result = addFive(10); // 15
// Uncurry functions
const curriedMultiply = (x: number) => (y: number) => x * y;
const multiply = uncurry(curriedMultiply);
const product = multiply(3, 4); // 12Manipulate function argument order and application.
/**
* Flip arguments of binary function
* @param f - Binary function
* @returns Function with flipped arguments
*/
function flip<A, B, C>(f: (a: A) => (b: B) => C): (b: B) => (a: A) => C;
/**
* Apply function to argument (reverse application)
* @param a - Argument
* @param f - Function to apply
* @returns Result of applying f to a
*/
function apply<A, B>(a: A): (f: (a: A) => B) => B;Usage Examples:
import { flip, apply } from "fp-ts/lib/function";
// Flip arguments
const subtract = (x: number) => (y: number) => x - y;
const flippedSubtract = flip(subtract);
const result1 = subtract(10)(3); // 7 (10 - 3)
const result2 = flippedSubtract(10)(3); // -7 (3 - 10)
// Apply function
const double = (n: number) => n * 2;
const applyToFive = apply(5);
const result3 = applyToFive(double); // 10Special utilities for working with arrays and concatenation.
/**
* Concatenate arrays
* @param xs - First array
* @param ys - Second array
* @returns Concatenated array
*/
function concat<A>(xs: Array<A>, ys: Array<A>): Array<A>;
/**
* Increment a number
* @param n - Number to increment
* @returns n + 1
*/
function increment(n: number): number;
/**
* Decrement a number
* @param n - Number to decrement
* @returns n - 1
*/
function decrement(n: number): number;
/**
* Absolute value
* @param n - Number
* @returns Absolute value of n
*/
function absurd<A>(a: never): A;Function flow utilities (modern composition patterns).
/**
* Flow functions left to right (pipe alternative)
*/
function flow<A, B>(ab: (a: A) => B): (a: A) => B;
function flow<A, B, C>(ab: (a: A) => B, bc: (b: B) => C): (a: A) => C;
function flow<A, B, C, D>(ab: (a: A) => B, bc: (b: B) => C, cd: (c: C) => D): (a: A) => D;
// ... up to flow with 9 functions
/**
* Pipe value through functions
*/
function pipe<A>(a: A): A;
function pipe<A, B>(a: A, ab: (a: A) => B): B;
function pipe<A, B, C>(a: A, ab: (a: A) => B, bc: (b: B) => C): C;
// ... up to pipe with 20 functionsUsage Examples:
import { flow, pipe } from "fp-ts/lib/function";
// Flow composition
const processString = flow(
(s: string) => s.trim(),
(s: string) => s.toLowerCase(),
(s: string) => s.split(' '),
(arr: string[]) => arr.length
);
const wordCount = processString(" Hello World "); // 2
// Pipe values
const result = pipe(
" HELLO WORLD ",
(s: string) => s.trim(),
(s: string) => s.toLowerCase(),
(s: string) => s.split(' '),
(arr: string[]) => arr.join('-')
); // "hello-world"These types are deprecated in favor of more modern alternatives:
// Deprecated function types
type Function1<A, B> = (a: A) => B;
type Function2<A, B, C> = (a: A, b: B) => C;
// ... up to Function9
// Deprecated curried types
type Curried2<A, B, C> = (a: A) => (b: B) => C;
type Curried3<A, B, C, D> = (a: A) => (b: B) => (c: C) => D;
// ... up to Curried9
// Deprecated operation types
type BinaryOperation<A, B> = (a1: A, a2: A) => B;
type Kleisli<F, A, B> = (a: A) => HKT<F, B>;
type Cokleisli<F, A, B> = (fa: HKT<F, A>) => B;Use FunctionN, modern composition with flow/pipe, and specific type class operations instead of these deprecated types.