将根据值执行功能的功能.功能编程 [英] Function that will execute function depending on value. Functional programming

查看:44
本文介绍了将根据值执行功能的功能.功能编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个函数,它们根据if语句执行.例如:

I have two functions and they are executed depending of if statement. E.g.:

if(value) {
    doA()
} else {
    doB()
}

如何编写将获取结果并决定是否执行每个功能的函数或对象.我想收到这样的东西:

How to write function or object that will take the result and decide whether or not execute each function. I want to receive something like this:

exists(result).doA()
nothing(result).doB()

我想学习一些JavaScrit中的函数式编程,因此,我很感激可以从中学习FP的任何资源.

I want to learn some functional programming in JavaScrit so I woud appreciate any source from which I can learn FP in JavaScript.

推荐答案

您可以编写如下内容:

function exists(value) {
    return function (func) {
        if (value !== undefined) {
            return func(value);
        }
        return null;
    }
}

function nothing(value) {
    return function (func) {
        if (value === undefined) {
            return func();
        }
        return null;
    }
}

function doA(value) {
    console.log('doing A', value);
}

function doB() {
    console.log('doing B');
}

const foo = 'fool';
const bar = undefined;

exists(foo)(doA);
nothing(bar)(doB);

exists 函数获取一个值并返回另一个函数.返回的函数将获得另一个函数作为参数,如果定义了传递的值,则执行.

The exists function gets a value and returns another function. The function that is returned gets yet another function as argument, which is executed if the value passed is defined.

在上面的示例中,我使用了老式"匿名功能,以使其更易于理解.使用ES6箭头功能,可以更简洁地编写 exists nothing 函数,如下所示:

I've used "old school" anonymous functions in the example above to make it easier to understand. With ES6 arrow functions, you can write the exists and nothing functions more consisely, like this:

function exists(value) {
    return func => value !== undefined ? func(value) : null;
}

function nothing(value) {
    return func => value === undefined ? func(value) : null;
}

当您意识到可以通过将通用代码放在另一个函数中来重构这两个函数时,功能编程乐趣"便真正开始了,然后再使用它来创建这两个函数,如下所示:

The "functional programming fun" really starts when you realize you can refactor these two functions by putting the common code in another function, that is then used to create the two functions, like this:

function executeWithCondition(predicate) {
    return value => func => predicate(value) ? func(value) : null;
}

const exists = executeWithCondition(value => value !== undefined);
const nothing = executeWithCondition(value => value === undefined);

此技术称为 currying .

这些功能的用法仍然相同:

Usage of these functions is still the same:

exists(foo)(doA);
nothing(bar)(doB);

这是完整的可运行代码示例:

Here's the complete runnable code example:

function executeWithCondition(predicate) {
    return value => func => predicate(value) ? func(value) : null;
}

const exists = executeWithCondition(value => value !== undefined);
const nothing = executeWithCondition(value => value === undefined);

function doA(value) {
    console.log('doing A', value);
}

function doB() {
    console.log('doing B');
}

const foo = 'fool';
const bar = undefined;

exists(foo)(doA);
nothing(bar)(doB);

这篇关于将根据值执行功能的功能.功能编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆