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

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

问题描述

我不认为我还喜欢咖喱。我明白它的作用,以及如何去做。我只是想不出我会使用它的情况。



您在JavaScript中使用currying的地方(或者使用它的主库在哪里)? DOM操作或一般应用程序开发示例欢迎。

其中一个答案提到了动画。像 slideUp fadeIn 这样的函数将一个元素作为参数,通常是一个curried函数,返回高阶函数内置默认的动画功能。为什么比仅仅应用更高级的函数和一些默认值更好?



使用它有什么缺点?

正如这里所要求的,有一些关于JavaScript currying的好资源: com / javascript-curry /rel =noreferrer> http://www.dustindiaz.com/javascript-curry/

  • Crockford,Douglas(2008) JavaScript:The Good Parts

  • http://www.svendtofte.com/code/curried_javascript/
    (绕过ML,所以跳过ML中的速成课程中的整个部分,然后从如何编写curried JavaScript)

  • http://blog.morrisjohns.com / javascript_closures_for_dummies

  • JavaScript关闭如何工作?

  • http://ejohn.org/blog/partial-functions-in-javascript 按照常规付款)

  • http://benalman.com/news/2010/09/partial-application-in-javascript/



  • 我会在评论中增加更多。




    因此,根据如果你经常通过调用相同的配置来精炼一个高级函数,那么你可以将咖喱(curry)和部分应用或者使用Resig的partial)更高层次的函数来创建简单的,简洁的帮助器方法。

    @Hank Gay



    针对EmbiggensTheMind的评论:

    我无法想到一个实例,其中 currying - 本身 - 在JavaScript中很有用;它是一种将具有多个参数的函数调用转换为函数调用链的技术,每个调用都有一个参数,但JavaScript在单个函数调用中支持多个参数。

    在JavaScript中 - 我假定大多数其他实际语言(不是lambda微积分),但它通常与部分应用程序相关联。 John Resig 更好地解释它,但其要点是有一些逻辑这将被应用于两个或更多的参数,你只知道一些参数的值。

    您可以使用部分应用程序/ currying来修复这些已知值,并返回一个只接受未知数的函数,稍后在实际拥有您希望的值时调用它通过。这提供了一种很好的方式来避免重复自己,当你一直在调用相同的JavaScript内置插件时,所有的值都是一样的。窃取John的例子:

      String.prototype.csv = String.prototype.split.partial(/,\s * / ); 
    var results =John,Resig,Boston.csv();
    alert((results [1] ==Resig)+文本值被正确分割);


    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.

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

    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?

    Are there any drawbacks to using it?

    As requested here are some good resources on JavaScript currying:

    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.

    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

    In response to EmbiggensTheMind's comment:

    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.

    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.

    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咖喱:实际应用有哪些?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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