@abdelrahman-elkady/partial-function-builder 中文文档教程

发布于 5年前 浏览 15 更新于 3年前

Partial Function Builder

部分函数生成器是一个简单的实用程序,它允许使用对象解构将部分参数应用于其命名参数的函数。

注意

此实用程序仅支持 -直到现在- 向原始函数接受的一个参数 提供命名 部分参数. 您可以检查 lodash 的 partial 以将部分参数应用于位置参数。

有关详细信息,请参阅用法

Installation

npm i @abdelrahman-elkady/partial-function-builder

Usage

Simple Usage

 const { toPartialBuilder } = require('@abdelrahman-elkady/partial-function-builder');

 const addAndMultiply = ({ n1, n2, n3 }) => (n1 + n2) * n3;
 const addPartialBuilder = toPartialBuilder(addAndMultiply);
 const multiplyWithThree = addPartialBuilder({ n3: 3 });
 multiplyWithThree({ n1: 1, n2: 1 }); // 6 .. expression evaluated is (1 + 1) * 3

Composition

 const { toPartialBuilder } = require('@abdelrahman-elkady/partial-function-builder');

 const add = ({ n1, n2, n3 }) => n1 + n2 + n3;

 const addPBuilder = toPartialBuilder(add);
 const partialOne = addPBuilder({ n1: 5 })

 partialOne({ n2: 2, n3: 3 }); // 10

 // Create a partial builder for the partial function `partialOne`
 const partialOnePBuilder = toPartialBuilder(partialOne);
 const partialTwo = partialOnePBuilder({ n2: 3 });
 partialTwo({ n3: 1 }); // 9

Overriding

 const { toPartialBuilder } = require('@abdelrahman-elkady/partial-function-builder');

 const add = ({ n1, n2, n3 }) => n1 + n2 + n3;

 const addPBuilder = toPartialBuilder(add);
 const partialOne = addPBuilder({ n1: 5 })

 partialOne({ n2: 2, n3: 3, n1: 10 }); // 15

致谢:font awesome

Partial Function Builder

Partial function builder is a simple utility that allows having functions with partially applied arguments to their named arguments using object destructuring.

Note

This utility only supports -till now- supplying named partial arguments to one single argument that the original function accepts. You can check lodash's partial to apply partial arguments to positional arguments.

See usage for more details.

Installation

npm i @abdelrahman-elkady/partial-function-builder

Usage

Simple Usage

 const { toPartialBuilder } = require('@abdelrahman-elkady/partial-function-builder');

 const addAndMultiply = ({ n1, n2, n3 }) => (n1 + n2) * n3;
 const addPartialBuilder = toPartialBuilder(addAndMultiply);
 const multiplyWithThree = addPartialBuilder({ n3: 3 });
 multiplyWithThree({ n1: 1, n2: 1 }); // 6 .. expression evaluated is (1 + 1) * 3

Composition

 const { toPartialBuilder } = require('@abdelrahman-elkady/partial-function-builder');

 const add = ({ n1, n2, n3 }) => n1 + n2 + n3;

 const addPBuilder = toPartialBuilder(add);
 const partialOne = addPBuilder({ n1: 5 })

 partialOne({ n2: 2, n3: 3 }); // 10

 // Create a partial builder for the partial function `partialOne`
 const partialOnePBuilder = toPartialBuilder(partialOne);
 const partialTwo = partialOnePBuilder({ n2: 3 });
 partialTwo({ n3: 1 }); // 9

Overriding

 const { toPartialBuilder } = require('@abdelrahman-elkady/partial-function-builder');

 const add = ({ n1, n2, n3 }) => n1 + n2 + n3;

 const addPBuilder = toPartialBuilder(add);
 const partialOne = addPBuilder({ n1: 5 })

 partialOne({ n2: 2, n3: 3, n1: 10 }); // 15

Credits: font awesome

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文