JavaScript curry:有哪些实际应用? [英] JavaScript curry: what are the practical applications?

查看:28
本文介绍了JavaScript curry:有哪些实际应用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我还没搞懂咖喱.我明白它的作用,以及如何去做.我只是想不出我会使用它的情况.

I don’t think I’ve grokked currying yet. I understand what it does, and how to do it. I just can’t think of a situation I would use it.

你在 JavaScript 中哪里使用了柯里化(或者主要库在哪里使用它)?欢迎提供 DOM 操作或一般应用程序开发示例.

Where are you using currying in JavaScript (or where are the main libraries using it)? DOM manipulation or general application development examples welcome.

其中一个答案提到了动画.slideUpfadeIn 等函数将一个元素作为参数,通常是一个柯里化函数,返回带有默认动画函数"内置的高阶函数.为什么这比仅应用具有某些默认值的上级函数更好?

One of the answers mentions animation. Functions like slideUp, fadeIn take an element as an arguments and are normally a curried function returning the high order function with the default "animation function" built-in. Why is that better than just applying the higher-up function with some defaults?

使用它有什么缺点吗?

这里有一些关于 JavaScript 柯里化的好资源:

As requested here are some good resources on JavaScript currying:

  • http://www.dustindiaz.com/javascript-curry/
  • Crockford, Douglas (2008) JavaScript: The Good Parts
  • http://www.svendtofte.com/code/curried_javascript/ (Takes a detour into ML so skip the whole section from "A crash course in ML" and start again at "How to write curried JavaScript")
  • http://web.archive.org/web/20111217011630/http://blog.morrisjohns.com:80/javascript_closures_for_dummies
  • How do JavaScript closures work?
  • http://ejohn.org/blog/partial-functions-in-javascript (Mr. Resig on the money as per usual)
  • http://benalman.com/news/2010/09/partial-application-in-javascript/

我会在评论中添加更多内容.

I’ll add more as they crop up in the comments.

所以,根据答案,柯里化和部分应用通常是方便的技术.

So, according to the answers, currying and partial application in general are convenience techniques.

如果您经常通过使用相同的配置调用一个高级函数来提炼"它,您可以咖喱(或使用 Resig 的部分)高级函数来创建简单、简洁的辅助方法.

If you are frequently "refining" a high-level function by calling it with same configuration, you can curry (or use Resig’s partial) the higher-level function to create simple, concise helper methods.

推荐答案

@Hank Gay

回应 EmbiggensTheMind 的评论:

In response to EmbiggensTheMind's comment:

我想不出一个例子,其中 currying 本身在 JavaScript 中很有用;它是一种将具有多个参数的函数调用转换为每个调用具有单个参数的函数调用链的技术,但 JavaScript 支持在单个函数调用中使用多个参数.

I can't think of an instance where currying—by itself—is useful in JavaScript; it is a technique for converting function calls with multiple arguments into chains of function calls with a single argument for each call, but JavaScript supports multiple arguments in a single function call.

在 JavaScript 中——我假设大多数其他实际语言(不是 lambda 演算)——但它通常与部分应用程序相关联.John Resig 解释得更好,但要点是有一些逻辑这将应用于两个或多个参数,并且您只知道其中一些参数的值.

In JavaScript—and I assume most other actual languages (not lambda calculus)—it is commonly associated with partial application, though. John Resig explains it better, but the gist is that have some logic that will be applied to two or more arguments, and you only know the value(s) for some of those arguments.

您可以使用部分应用程序/柯里化来修复那些已知值并返回一个仅接受未知数的函数,以便稍后在您真正拥有要传递的值时调用.这提供了一种很好的方法来避免重复自己,当您一遍又一遍地使用所有相同的值调用相同的 JavaScript 内置函数时.窃取约翰的例子:

You can use partial application/currying to fix those known values and return a function that only accepts the unknowns, to be invoked later when you actually have the values you wish to pass. This provides a nifty way to avoid repeating yourself when you would have been calling the same JavaScript built-ins over and over with all the same values but one. To steal John's example:

String.prototype.csv = String.prototype.split.partial(/,s*/);
var results = "John, Resig, Boston".csv();
alert( (results[1] == "Resig") + " The text values were split properly" );

这篇关于JavaScript curry:有哪些实际应用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆