相关疑难解决方法(0)

多个箭头函数在javascript中意味着什么?

我一直在阅读一堆react代码,我看到这样的东西,我不明白:

handleChange = field => e => {
  e.preventDefault();
  /// Do something here
}
Run Code Online (Sandbox Code Playgroud)

javascript ecmascript-6 arrow-functions

401
推荐指数
6
解决办法
7万
查看次数

在递归函数中处理大数组时堆栈溢出

如果数组列表太大,为什么以下递归代码会导致堆栈溢出?我该如何解决这个问题仍然保留递归模式?

var list = readHugeList();

var nextListItem = function() {
    var item = list.pop();

    if (item) {
        // process the list item...
        nextListItem();
    }
};
Run Code Online (Sandbox Code Playgroud)

javascript

9
推荐指数
1
解决办法
1470
查看次数

如何使用Clojure语言的子集在lambda演算中实现递归函数?

我正在用Greg Michaelson的"通过Lambda微积分进行功能编程简介"一书中学习lambda演算.

我仅使用该语言的一个子集在Clojure中实现示例.我只允许:

  • 符号
  • one-arg lambda函数
  • 功能应用
  • var定义为方便起见.

到目前为止,我有这些功能:

(def identity (fn [x] x))
(def self-application (fn [s] (s s)))

(def select-first (fn [first] (fn [second] first)))
(def select-second (fn [first] (fn [second] second)))
(def make-pair (fn [first] (fn [second] (fn [func] ((func first) second)))))    ;; def make-pair =  ?first.?second.?func.((func first) second)

(def cond make-pair)
(def True select-first)
(def False select-second)

(def zero identity)
(def succ (fn [n-1] (fn [s] ((s False) n-1))))
(def one (succ zero))
(def zero? (fn …
Run Code Online (Sandbox Code Playgroud)

recursion clojure lambda-calculus

8
推荐指数
2
解决办法
601
查看次数

管道和monad如何在JavaScript中协同工作?

我看过类似的问题和答案,但没有找到直接解决我问题的答案.我在努力理解如何使用MaybeEitherMonads连同管道的功能.我想将函数连接在一起,但我希望管道停止并在任何步骤发生错误时返回错误.我正在尝试在node.js应用程序中实现函数式编程概念,这实际上是我对它们的第一次认真探索,所以没有答案会如此简单以至于侮辱我对这个主题的智慧.

我写了这样的管道函数:

const _pipe = (f, g) => async (...args) => await g( await f(...args))

module.exports = {arguments.
    pipeAsync: async (...fns) => {
        return await fns.reduce(_pipe)
    }, 
...
Run Code Online (Sandbox Code Playgroud)

我这样称呼它:

    const token = await utils.pipeAsync(makeACall, parseAuthenticatedUser, syncUserWithCore, managejwt.maketoken)(x, y)  
Run Code Online (Sandbox Code Playgroud)

javascript monads functional-programming pipe

8
推荐指数
1
解决办法
929
查看次数

如何以功能方式实现数组连接?

我有一个函数,它使用条件分隔符连接一个对象数组.

function getSegmentsLabel(segments) {
    var separator = '-';

    var segmentsLabel = '';
    var nextSeparator = '';
    _.forEach(segments, function(segment) {
        segmentsLabel += nextSeparator + segment.label;
        nextSeparator = segment.separatorUsed ? separator : ' ';
    });
    return segmentsLabel;
}
Run Code Online (Sandbox Code Playgroud)

用途:

var segments = [
    {label: 'First', separatorUsed: true},
    {label: 'Second', separatorUsed: false},
    {label: 'Third', separatorUsed: true},
    {label: 'Forth', separatorUsed: true}
];

getSegmentsLabel(segments); // Result: "First-Second Third-Forth"
Run Code Online (Sandbox Code Playgroud)

如何getSegmentsLabel在不改变变量的情况下以纯函数方式编写上述函数?我们可以使用lodash函数.

javascript functional-programming lodash

6
推荐指数
1
解决办法
139
查看次数

How to adapt trampolines to Continuation Passing Style?

Here is a naive implementation of a right fold:

const foldr = f => acc => ([x, ...xs]) =>
  x === undefined
    ? acc 
    : f(x) (foldkr(f) (acc) (xs));
Run Code Online (Sandbox Code Playgroud)

This is non-tail recursion and hence we cannot apply a trampoline. One approach would be to make the algorithm iterative and use a stack to mimick the function call stack.

Another approch would be to transform the recursion into CPS:

const Cont = k => ({runCont: k});

const foldkr = f …
Run Code Online (Sandbox Code Playgroud)

javascript continuations functional-programming trampolines continuation-passing

5
推荐指数
2
解决办法
139
查看次数

在递归中使用return的位置

在C这个代码工作,这里我没有使用return递归调用函数.它提供了正确的输出

int gcd(int a, int b)
{
    if(b == 0)
        return a;
    gcd(b, a % b);
}
Run Code Online (Sandbox Code Playgroud)

但是,如果我在python中编写相同的代码,则此代码返回None(我认为值应该从if语句中的return语句返回)

def gcd(a, b):
    if b == 0:
        return a
    gcd(b, a % b)
Run Code Online (Sandbox Code Playgroud)

为了使这段代码有效,我必须添加return

def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)
Run Code Online (Sandbox Code Playgroud)

但为什么?在C和Python中执行代码之间的差异是什么?如果我在递归调用时添加额外的返回,C中的代码也可以工作,为什么不抛出错误?

c python recursion greatest-common-divisor

3
推荐指数
1
解决办法
130
查看次数

Node.js:异步函数中的尾部调用是否有优化?

我正在使用节点 v8.10.0

上述问题解释了 Node.js 如何不再支持 TCO。我最近遇到了这样的函数问题:

async function processBatch(nthBatch) {
    // do a bunch of async work and build up variables
    await processBatch(nthBatch + 1);
}
Run Code Online (Sandbox Code Playgroud)

该代码存在内存泄漏,通过将其更改为:

async function processBatch(nthBatch) {
    // do a bunch of async work and build up variables
    return processBatch(nthBatch + 1);
}
Run Code Online (Sandbox Code Playgroud)

我很惊讶这实际上有效,因为在上面描述的问题中,它清楚地解释了 Node 8.x 不支持 TCO。那么,是否有什么特殊的事情可以实现 TCO?或者是因为它在引擎盖下使用了生成器,并且返回将生成器标记为已完成,因此可以丢弃堆栈?

recursion tail-recursion node.js

3
推荐指数
1
解决办法
2060
查看次数

收敛系列函数的最大调用堆栈大小超出错误

我试图在JavaScript中得到一个特定的融合系列函数:

function cnvg(sum,marker){
  if((marker--||1000)>=0){
    return cnvg(sum=(sum||0) + 1/Math.pow(-3,marker)/(2*marker+1), marker)
  } else {
    return sum;
  }
}
Run Code Online (Sandbox Code Playgroud)

我期待cnvg()回来相当于Math.PI/Math.sqrt(12)(见下面的图片),但我不断得到"超出最大调用堆栈大小"错误.我认为它可能是迭代次数,所以我把1000引用放到了100当时10最后1,但我似乎仍然收到错误.

在此输入图像描述

从理论上讲,一旦marker倒数到0并执行最后一个循环,它应该停止并返回值sum,但这似乎不是这样的情况......任何人都可以告诉我我做错了什么?

提前致谢.

javascript math recursion

0
推荐指数
1
解决办法
106
查看次数