TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Ask HN: Autocurrying Syntax Sugar?

2 点作者 danhab99将近 3 年前
Full disclosure: I&#x27;m not a computer scientist, I&#x27;m just a programmer who uses these tools. Also I mainly use javascript and go, I need to spread out more but these two tools fulfill 99% of all my needs.<p>I had a 2am idea and when I looked it up I couldn&#x27;t find any mention of someone else thinking of it. Here it is:<p>Function currying is a method for saving arguments on the callstack so they don&#x27;t have to be copied in memory between calls. Here&#x27;s a javascript example of currying:<p>```js const add = (leftOp) =&gt; (rightOp) =&gt; leftOp + rightOp; const addThree = add(3); console.log(addThree(4)); &#x2F;&#x2F; 7 console.log(addThree(-2)); &#x2F;&#x2F; 1 ```<p>I think all PL that support the &quot;putting-functions-in-variables&quot; paradime can do this. BUT. If you try to call a normal function that requires all of its arguments up front without all the arguments it will throw an error. So what if it didn&#x27;t? What if instead of throwing an error, the function call returns a curried function with the first arguments filled and the unfilled arguments left to be inputted.<p>```js &#x2F;&#x2F; with autocurrying const add = (leftOp, rightOp) =&gt; leftOp + rightOp; const addFive = add(5); &#x2F;&#x2F; addFive is defined like addFive = (rightOp) =&gt; 5 + rightOp console.log(addFive(3)); &#x2F;&#x2F; 8 ```<p>This could work in Javascript(&#x2F;Typescript), Python, Go and similar languages. By encouraging currying you could potentaily get memory saving, readibility, and performance. But I won&#x27;t ever try to sell you a stack of pros without a foundation of cons: any syntax sugar has the potential to cause logic bugs, reference bugs, and missuse.<p>What do you guys think?

1 comment

icsa将近 3 年前
You should check out the K language and Haskell as well.<p>Both provide currying as part of the language.