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
Authors: Mohamed-Kaizen