2016-05-02 102 views
1

我们不使用for loop函数式编程,而是使用higher order functions,如map,filter,reduce等。这些都适用于遍历数组。函数式编程 - 简单循环递增计数器

但是,我不知道如何做一个简单的计数器循环。

let i = 0; 
for(i; i < 10; i++) { 
    console.log("functional programming is a religion") 
}; 

那么,如何在功能性编程中做到这一点?

回答

9

的功能的方法是写一个创建它要求最基本的功能ñ次功能上的HOF:

function repeatTimes(fn, n) { 
    return function() { 
    while (n--) fn(...arguments); 
    }; 
} 

现在你会打电话给你的功能如下:

function myFunc() { console.log("functional programming is a religion"); } 

const tentimes = repeatTimes(myFunc, 10); 
tentimes(); 

这种方法可以通过推广继续重复呼叫的条件来扩展。我们将传递一个函数来决定何时停止,而不是固定的数字n。我们将通过该功能的迭代次数:

function repeatWhile(fn, cond) { 
    return function() { 
    var count = 0; 
    while (cond(count++)) fn(...arguments); 
    }; 
} 

现在我们把这个作为

const tentimes = repeatWhile(myFunc, i => i < 10); 
tentimes(); 

我们可以通过它创造条件功能,我们称之为lessThan功能进一步简化这一点:

function lessThan(n) { return i => i < n; } 

现在调用可以写成

const tentimes = repeatWhile(myFunc, lessThan(10)); 
tentimes(); 
+0

我删除了我的,你的代码更优雅。 –

+3

值得注意的是,“纯”功能解决方案涉及递归,而不是“while”。然而,在这个时候,'while'的性能比递归好得多。 –

0

那么,如何在功能性编程中做到这一点?

它没有做多少实际,你仍然可以使用forEach一点点workaround

Array.apply(null, Array(5)).forEach(function(){ 
console.log("funtional programming is a religion") 
}); 

5是要重复的次数。

0

使用简单的递归函数

function counter(value) { 
    var i = value; 
    if(i<10){ 
     console.log("functional programming is a religion"); 
    }else{ 
     return; 
    } 
     counter(++i);  
} 
    counter(0); 
0
如何

样?

/*forLoop takes 4 parameters 
1: val: starting value. 
2: condition: This is an anonymous function. It is passed the current value. 
3: incr: This is also an anonymous function. It is passed the current value. 
4: loopingCode: Code to execute at each iteration. It is passed the current value. 
*/ 

var forLoop = function(val, condition, incr, loopingCode){ 
    var loop = function(val, condition, incr){ 
    if(condition(val)){ 
     loopingCode(val); 
     loop(incr(val), condition, incr); 
    } 
    }; 
    loop(val, condition, incr); 
} 

然后调用循环如下:

forLoop(0, 
     function(x){return x<10}, 
     function(x){return ++x;}, 
     function(x){console.log("functional programming is a religion")} 
    ); 

输出: 功能的编程是一种宗教

功能的编程是一种宗教

功能的编程是一种宗教

功能编程NG是一种宗教

函数式编程是一种宗教

函数式编程是一种宗教

函数式编程是一种宗教

函数式编程是一种宗教

函数式编程是一种宗教

函数式编程是一种宗教

请让我知道您对此答案的看法。

0

为什么不建立一个高阶函数为数字。

Number.prototype.repeat = function (fn) { 
 
    var i, 
 
    n = Math.abs(Math.floor(this)) || 0; 
 
    for (i = 0; i < n; i++) fn(i, this); 
 
}; 
 

 
(10).repeat(function (i, n) { document.write(i + ' of ' + n + ': your claim<br>'); }); 
 
(NaN).repeat(function (i, n) { document.write(i + ' of ' + n + ': your claim<br>'); });

1

整个问题就是让大部分代码都可以测试。对于你的例子,我想最好的是创建文本而不打印它。

function unFold(fnStopPredicate, fnTerm, fnGenerate, aSeed) { 
    var arr = []; 
    while(! fnStopPredicate(aSeed)){ 
     arr.push(fnTerm(aSeed)); 
     aSeed = fnGenerate(aSeed); 
    } 
    return arr; 
} 

你可能会说这不起作用,这是真的,但它有一个功能接口。它不改变它的参数,返回的值总是它的初始参数的直接结果。

var strValues = unFold(x => x > 10, 
         x => "functional programming is a religion", 
         x => x+1, 
         0).join("\n"); 

// Real side effect goes here 
console.log(strValues); 

这里的要点是,只要你提供的函数本身没有副作用,你可以单元测试unFold的使用。