请为什么我的代码不起作用,我需要安装任何库

发布于 2025-01-29 00:22:28 字数 324 浏览 3 评论 0原文

最近,我开始使用功能编程和管道的所有解释,并使用我看到的减少构成非常粗略的。


const x = 4

const add2 = x + 2

const multiplyBy5 = x * 5

const subtract1 = x - 1



pipe = (...functions) =>
(x) => functions.reduce((v, function) => function(v), x)

const result = pipe(add2, multiplyBy5, subtract1)(4)
console.log(result)

Recently started functional programming and all explanations of the pipe and compose using reduce which I have seen are very sketchy.


const x = 4

const add2 = x + 2

const multiplyBy5 = x * 5

const subtract1 = x - 1



pipe = (...functions) =>
(x) => functions.reduce((v, function) => function(v), x)

const result = pipe(add2, multiplyBy5, subtract1)(4)
console.log(result)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

场罚期间 2025-02-05 00:22:28

有2个错误。

  1. 第一个是xadd2pultiplyby5 and 减去1不是功能,而是仅仅定义。
  2. 另一个是,您将变量(使用参数)命名为一个“保留”单词(例如“函数”)的名称确实打破了语法解析器。
const x = (x) => x
const add2 = (x) => x+2
const multiplyBy5 = (x) => x*5
const subtract1 = (x) => x-1

const pipe = (...functions) => (x) => functions.reduce((v,fn)=>fn(v),x)
const result = pipe(
  add2,
  multiplyBy5,
  subtract1,
)(4);
console.log(result)

There were 2 errors.

  1. The first one was that the x, add2, multiplyBy5 and subtract1 were not functions, but mere definitions.
  2. The other was that you naming a variable (using the arguments) to a name that is a "reserved" word such as "function" did break the syntax parser.

const x = (x) => x
const add2 = (x) => x+2
const multiplyBy5 = (x) => x*5
const subtract1 = (x) => x-1

const pipe = (...functions) => (x) => functions.reduce((v,fn)=>fn(v),x)
const result = pipe(
  add2,
  multiplyBy5,
  subtract1,
)(4);
console.log(result)

初相遇 2025-02-05 00:22:28

我认为应该这样做:

const add2 = (x) => x+2
    
const multiplyBy5 = (x) => x*5
    
const subtract1 = (x) => x-1
    
const pipe=(...functions) => (x) => functions.reduce((v, functionA) => functionA(v), x)
    
const result=pipe(add2, multiplyBy5, subtract1)(4)
    
console.log(result)

I think it should be done like this:

const add2 = (x) => x+2
    
const multiplyBy5 = (x) => x*5
    
const subtract1 = (x) => x-1
    
const pipe=(...functions) => (x) => functions.reduce((v, functionA) => functionA(v), x)
    
const result=pipe(add2, multiplyBy5, subtract1)(4)
    
console.log(result)

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