- 浏览: 399733 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (309)
- xaml C# wpf (0)
- scala java inner clas (1)
- Tools UML Eclipse UML2 (1)
- Timer .NET Framework (1)
- perl (6)
- python function paramter (1)
- Python Docstring (1)
- Python how to compare types (1)
- Python (8)
- java (5)
- C# (76)
- C# WPF (0)
- p4 (0)
- WPF (46)
- .net (6)
- xaml (1)
- javascript (40)
- windows (10)
- scala (4)
- winform (1)
- c++ (48)
- tools (12)
- cmd (1)
- os (0)
- CI (0)
- shell (0)
- C (2)
- haskell (49)
- functional (1)
- tool (1)
- gnu (1)
- linux (1)
- kaskell (0)
- svn (0)
- wcf (3)
- android (1)
最新评论
partially applying a function returns a new function which you can call. Which could be very useful in a lots of situations.
Below is an exapmle that uses the partially applied functions, the code is as below.
String.prototype.csv = String.prototype.split.partial(/,\s*/); var results = ("John, Resig, Boston").csv(); assert(results[1] == "Resig", "The text value were split properly");
Below is the content of a js file called partials.js file.
/** *@Summary: this function allow curry some functions further */ Function.prototype.curry = function () { var fn = this, args = Array.prototype.slice.call(arguments); return function () { return fn.apply(this, args.concat(Array.prototype.slice.call(arguments))); }; }; /** * @Summary: e.g. String.prototype.csv = String.prototype.split.partial(/,\s* /); // or you can do with the following, by specifying the undefine explicitly String.prototype.csv = String.prototype.split.partial(/,\s* /, undefined); var results = ("John, Resig, Boston").csv(); assert(results[1] == "Resig", "The text values were split properly"); */ Function.prototype.partial = function () { var fn = this, args = Array.prototype.slice.call(arguments); // alert("length of arguments = " + args.length); return function () { var arg = 0; for (var i = 0; i < args.length && arg < arguments.length; i++) { if (args[i] == undefined) args[i] = arguments[arg++]; } return fn.apply(this, args); // return fn.apply(document.getElementById("bindbtn"), args); }; };
the code above has impl of partial function on the Function.prototype object.
And you have many application situation that you can apply the paritally applied functions. For one, you can have a partial time out function .
var delay = setTimeout.partial(undefined, 1000); test("delayed partial function", function () { delay(function () { assert(true, "A call to this function will be temporarily delayed."); }); });
Below is the content of the test file that test the functions of the partials.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <script type ="text/javascript" src="partials.js"></script> <script type="text/javascript" src="../unit.js"></script> <script type="text/javascript"> window.onload = function () { test("partials", function () { String.prototype.csv = String.prototype.split.partial(/,\s*/); // you can do with the line below as well. // String.prototype.csv = String.prototype.split.partial(/,\s*/); var results = ("John, Resig, Boston").csv(); assert(results[1] == "Resig", "The text value were split properly"); }); var delay = setTimeout.partial(undefined, 1000); test("delayed partial function", function () { delay(function () { assert(true, "A call to this function will be temporarily delayed."); }); }); // there is a bug in that addEventListener is not an object or is null; // the object that is bound to this is as follow. // 'this = [object DOMWindow]' // var bindclick = document.getElementById("bindbtn").addEventListener.partial("click", undefined, false); // test("curry bind function", function () { // bindclick(function() { // assert(true, "Click event bound via curried function."); // }); // }); }; </script> <style type="text/css" > #results li.pass { color: Green } #results li.fail { color: Red } </style> </head> <body> <ul id="results" /> <input type="button" id="bindbtn" value="click me" /> </body> </html>
However, from the Javascript ninja, there it allege that you can do the partial on the addEventListener, well, according to my test, it does not work .
the reason is (as stated in the comment, addEventListener is not a function? if you add something like alert('this = ' + this) in the partial function, you will find that it print out something like
'this = [object DOMWindow]'
发表评论
-
javascript - trick to cross browser DOM ready event
2012-08-24 08:23 928the "ready" event ... -
javascript - trick to simulate mouseenter and mouseleave
2012-08-23 08:31 2254Previously we discussed javasc ... -
javascript - trick to simulate the change event
2012-08-22 08:51 1659In the previous discussion a ... -
javascript - trick to simulate bubbling submit event
2012-08-22 08:03 906In the previous discussion abou ... -
javascript - trick to implement bubbling submit event
2012-08-23 07:55 700Following up to the javascrip ... -
javascript - trick to detect bubbling supportability
2012-08-20 22:22 972Event delegation is oe of the b ... -
javascript - trigger event and custom events
2012-08-20 21:58 2078In the previous post - javascri ... -
javascript - trick to handlers management
2012-08-20 08:19 1024We have discussed "javascr ... -
javascript - trick to centralized store
2012-08-20 07:52 819For a number of reasons it's ... -
javascript - trick to fix the event object
2012-08-20 07:47 881Many browsers, especially In ... -
javascript - tricks to deal with colors
2012-08-15 08:34 766There are a couple of ways to r ... -
javascript - trick to manipulate the opacity
2012-08-15 08:26 766All other browsre may have supp ... -
javascript - trick to test visibility of an element
2012-08-15 08:15 522though there is a visible prope ... -
javascript - trick to get and set height and width
2012-08-15 08:05 549when looking at properties t ... -
javascript - trick to set/get attributes that expects px values
2012-08-16 11:00 517When setting a number into a ... -
javascript - trick to get and set CSS style
2012-08-16 11:00 747while it will not be so much tr ... -
javascript - trick to normalize href for IE
2012-08-16 10:59 533IE is again the only browser th ... -
javascript - trick IE form and its expando attribute
2012-08-16 10:59 1042there is a known issue that if ... -
javascript expando and attributes
2012-08-14 08:15 1039expando is something like this ... -
javascript - trick to getText and setText
2012-08-14 07:40 1144it is not as simple as you thin ...
相关推荐
A novel reconfigurable partially reflective surface (PRS) antenna is presented in this paper. The beam scanning ability is realized by employing a reconfigurable PRS structure and a phased array as ...
书中会详细介绍如何定义和使用高阶函数,以及如何通过柯里化(Currying)和部分应用(Partially Applied Function)来构造更灵活的函数。 其次,函数式编程中的纯函数(Pure Function)是无副作用的,输入与输出...
柯里化的本质是部分应用(Partially Applied Function),即将函数的部分参数提前应用,得到一个新的只差剩余参数的函数。这样可以简化复杂函数的调用,提高代码的可读性和复用性。 在`main.js`中,我们可以预期...
### 如何在ZEMAX中建模部分反射与部分散射表面 在光学设计领域,理解和模拟复杂的光行为是至关重要的。特别是在非顺序光学(Non-Sequential)领域,建模部分反射与部分散射的表面变得尤为关键。...
标题中的“PV_MPPT_Partially.slx带有粒子群算法”指的是一个Simulink模型,该模型用于太阳能光伏(PV)系统的最大功率点跟踪(Maximum Power Point Tracking, MPPT)并应用了粒子群优化(Particle Swarm ...
4. 离散结构:在组合学中,离散结构通常指的是图论(graph theory)和偏序集(partially ordered sets,简写为posets)。图论是研究顶点和边构成的图形的学科,它在解决网络设计、电路设计等问题时极为重要;偏序集...
图像去模糊的代码,可以直接应用。Deblurring Shaken and Partially Saturated Images
这对于部分应用(Partially Applied Function)非常有用: ```javascript function greet(name, greeting) { console.log(greeting + ', ' + name); } let sayHelloToAlice = greet.bind(null, 'Alice', '...
偏序幺半群是数学中的一个重要概念,它涉及抽象代数、序结构以及泛代数等多个数学分支。在这篇论文中,刘鹏远、杨义川和Muhammad Zafrullah三人探讨了偏序幺半群中的一些重要性质,并对这些性质进行了深入的研究。...
咖喱化,或称为部分应用(Partially Applied Function),是函数式编程中的一个重要特性,尤其在JavaScript这样的语言中,它可以将接受多个参数的函数转换为一系列只接受一个参数的函数。这样做的好处是可以提前固定...
the azimuthal order and radial order) of a partially coherent LGpl beam (i.e., a partially coherent vortex beam) based on the measurement of the cross-correlation function (CCF) and the double ...
- CLSU.m : function performing the standard partially constrained least squared unmixing. - SCLSU.m: function performing a scaled version of CLSU, which follows a particular case of the ELMM - soft.m:...
### 排序学习与部分标记数据 #### 一、引言 在信息检索(Information Retrieval,IR)领域,排序算法扮演着至关重要的角色。这类算法的目标是适当地对一组对象或文档进行排序,以便更好地满足用户的查询需求。...
Based on the Rytov approximation and the cross-spectral density approximation for the mutual coherence function of the partially coherent field, the propagation properties of the partially coherent ...
在处理数据分析时,部分线性模型是一种重要的统计模型,它结合了线性和非线性因素,在模型的某些变量上使用线性回归,在其他变量上使用非参数方法。本文由王凯平和林路撰写,研究了针对部分线性模型非参数部分的一种...
本文研究了段和有负平方的定向域之间的关系。为了深入理解这一主题,我们需要介绍几个数学领域的概念。 首先,线性序域(Linearly Ordered Field)是一个数学上的结构,其元素可以排序,并且满足完全有序域的特性。...
《部分相干检测在快速时间衰变信道》的中文翻译主要探讨了在快速衰变信道中,如何通过部分相干检测提高通信系统的性能。这部分内容是针对无线通信领域,特别是那些需要在快衰环境中保持稳定服务的应用,如高速无线...