简体   繁体   English

哪些JavaScript函数f满足:f(x,y)= f(a,b)IFF x = a和y = b?

[英]Which javascript functions f satisfy: f(x,y) = f(a,b) IFF x = a and y = b?

I'm using SlickGrid and the related DataView's groupBy functionality. 我正在使用SlickGrid和相关的DataView的groupBy功能。 I would like to group by a pair of columns, but groupBy only allows grouping by a single value. 我想按一对列分组,但groupBy仅允许按单个值分组。

To get around that (in the AFAICT supported and intended way), I want to pass DataView a function h(row) {return TUPLE(row.x, row.y)} , where TUPLE corresponds to the f in this question's title: it should return an object which behaves with respect to equality as if it was a two-tuple of TUPLE 's first and second argument: [Then DataView would group the rows by h(row) ] 为了解决该问题(以AFAICT支持和预期的方式),我想向DataView传递一个函数h(row) {return TUPLE(row.x, row.y)} ,其中TUPLE对应于此问题标题中的f :它应该返回一个表现相等的对象,就好像它是TUPLE的第一个和第二个参数的二元组TUPLE :[然后,DataView将按h(row)对行进行分组]

My best solution is something like 我最好的解决方案是

x.toString().replace("," by "") + "," + y.toString().replace("," by "")

More generally: 更普遍:

  1. Choose a separator string S (eg , ) 选择一个分离器串S(例如,
  2. Convert each argument to a string 将每个参数转换为字符串
  3. Remove all occurrences of S from each such string 从每个这样的字符串中删除所有出现的S
  4. Insert S between all the strings (a la return theStrings.join(S) ) 在所有字符串之间插入S( return theStrings.join(S)

My only complaint against this solution is that it feels incredibly icky. 我对此解决方案的唯一抱怨是,它感觉到令人难以置信的邪恶。 That, however, is sufficient to come here and ask if I'm missing some kind of language idiom, best practice or design pattern. 但是,这足以来这里询问我是否缺少某种语言习语,最佳实践或设计模式。

You can use a pairing function if both the two arguments are numbers(or can be converted to numbers). 如果两个参数都是数字(或可以转换为数字),则可以使用配对功能。

f(x, y) = (x + y) * (x + y + 1) / 2 + y f(x,y)=(x + y)*(x + y + 1)/ 2 + y

See more: http://en.wikipedia.org/wiki/Pairing_function 查看更多: http : //en.wikipedia.org/wiki/Pairing_function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使函数可以使用多个参数进行调用,因此可以代替f(x)(y)来执行f(x,y) - How to make functions callable with multiple parameters, so instead of f(x)(y) you can do f(x,y) [a,b] .reduce(f,x)代码使用基于换能器/ CPS的功能参考[a,b] .reduce(f)? - [a,b].reduce(f,x) code to [a,b].reduce(f) using transducer /CPS based functional references? 如何验证要在我的 function 中使用的序列 F(x)=F(x+y)+F(xz)? - How can I validate this sequence F(x)=F(x+y)+F(x-z) to be used in my function? 在JavaScript中使用f(x)= 1 / x - Using f(x) = 1/x in JavaScript g(f(x)(y)) 的无点版本 - Point-free version of g(f(x)(y)) 在Javascript中,为什么不能用f(f)替换x =&gt; f(f)(x)? - In Javascript why can't I replace x => f(f)(x) by f(f)? (function (x,y){…})(a,b); 在 JavaScript 中是什么意思? - What does (function (x,y){…})(a,b); mean in JavaScript? Javascript Regexp:匹配以X开头(不包括Y),以A或B结尾的字符串 - Javascript Regexp: match string beginning with X, excluding Y, ending with A or B 如果一个<x<b and C<y<d and x> y 返回值 - If a<x<b and C<y<d and x>y return value 设置CDO.Message选项时使用的f(x)= y JScript惯用语的JavaScript友好替代方法 - JavaScript-friendly alternative to the f(x) = y JScript idiom that's used when setting CDO.Message options
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM