[英]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: 更普遍:
,
) 选择一个分离器串S(例如,
) 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.