Skip to content

Partial

Let you create a function by partially applying arguments to another function.

๐ŸŽฌ Usage

<script>
    import {partial} from "@sveu/shared/curry"

    const add = (a, b) => a + b

    const sum = partial(add, 1)

    const sub = (a, b, c) => a - b - c

    const subtract = partial(sub, 10, 5)
</script>

<h1>Result: {sum(2)}</h1>

<h1>Result: {subtract(2)}</h1>

๐Ÿ‘ฉโ€๐Ÿ’ปAPI

๐Ÿ‘ป Arguments

Name Description Type Required
fn The function to partially apply arguments to FunctionArgs yes
args The arguments to partially apply any[] yes

โ†ฉ๏ธ Returns

A partial function that takes the same arguments as the original function, minus the arguments that were partially applied.

๐Ÿงช Playground

Source Code ๐Ÿ‘€

Source Code
import type { FunctionArgs } from "../../utils"

/**
 * Create a function by partially applying arguments to another function.
 *
 * @param fn The function to partially apply arguments to.
 *
 * @param args The arguments to partially apply to the function.
 *
 * @returns A function that takes the remaining arguments to the function.
 */
export function partial(fn: FunctionArgs, ...args: any[]) {
    return (...rest: any[]) => fn(...args, ...rest)
}

Last update: 2023-03-07