我们在函数式编程中不使用for loop
,而是使用higher order functions
,例如map,filter,reduce等。这些都适合迭代数组。
但是,我想知道如何进行简单的计数器循环。
let i = 0;
for( i; i < 10; i++) {
console.log( "functional programming is a religion")
};
那么,如何在函数式编程中做到这一点?
答案 0 :(得分:13)
一种功能性的方法是编写一个HOF来创建一个函数,该函数调用基础函数 n 次:
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();
答案 1 :(得分:3)
请勿使用“ while”或“ for”来控制命令式编程不起作用的流程。
Array(10).fill("functional programming is not a religion")
.map((msg) => {
console.log(msg);
return msg;
});
答案 2 :(得分: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的使用进行单元测试。
答案 3 :(得分:0)
使用简单的递归函数
function counter(value) {
var i = value;
if(i<10){
console.log( "functional programming is a religion");
}else{
return;
}
counter(++i);
}
counter(0);
答案 4 :(得分: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")}
);
输出: 函数式编程是一种宗教
函数式编程是一种宗教
函数式编程是一种宗教
函数式编程是一种宗教
函数式编程是一种宗教
函数式编程是一种宗教
函数式编程是一种宗教
函数式编程是一种宗教
函数式编程是一种宗教
函数式编程是一种宗教
请告诉我您对此答案的看法。
答案 5 :(得分:0)
为什么不为Numbers建立高阶函数。
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>'); });
答案 6 :(得分:0)
该函数调用 callbackFn count 次。
const times = (count, callbackFn) => {
if (count === 0) {return}
callbackFn();
times(count-1, callbackFn);
}
times(10, () => console.log("Functional Programming is a Religion"));
这个函数就像一个 for 循环
const forLoop = (initialValues, conditionFn, newValsFn, bodyFn) => {
if (!conditionFn(initialValues)) {return}
bodyFn(initialValues);
forLoop(newValsFn(initialValues), conditionFn, newValsFn, bodyFn);
}
forLoop({i: 0}, ({i}) => i < 10, ({i}) => ({i: i+1}), ({i}) => {
console.log(i, "Functional Programming is a Religion.");
});
这里,上面的函数用于打印斐波那契数列的前 n 项
const forLoop = (initialValues, conditionFn, newValsFn, bodyFn) => {
if (!conditionFn(initialValues)) {return}
bodyFn(initialValues);
forLoop(newValsFn(initialValues), conditionFn, newValsFn, bodyFn);
}
const fibPrint = (n) => {
let n1 = 0, n2 = 1, nextTerm;
forLoop({i: 1}, ({i}) => i <= n, ({i}) => ({i: i+1}), () => {
console.log(n1);
nextTerm = n1 + n2;
n1 = n2;
n2 = nextTerm;
});
}
fibPrint(10);
答案 7 :(得分:0)
当迭代次数非常大时,在函数中调用相同的函数需要大量内存。进一步的cpu时间也增加了。像intel和arm这样的公司 会喜欢这种方法,因为他们正在鼓动软件公司推出 资源匮乏的程序
无论如何,现在我们处于人工智能时代,需要猛犸象来解决问题,我认为这不是问题。我在教微处理器和微控制器,可能我的担忧是由于这个。 没有
答案 8 :(得分:-1)
那么,如何在函数式编程中做到这一点?
实际上做得不多,你仍然可以forEach
使用workaround
Array.apply(null, Array(5)).forEach(function(){
console.log( "funtional programming is a religion")
});
5是您想要迭代的次数。