LonghronShen
【趣味编程】实现add(2)(3)这样的表达式
>>
完整版
大家可以用自己熟悉的语言实现一段和如下JavaScript代码等价的程序。
亮点在于实现add(2)(3)(4)(5)这种诡异的语法……<script>function add(x){var c = 0;return function(x){c = c + x;arguments.callee.toString = function(){return c;};return arguments.callee;}(x);};alert(add(2)(3)(4)(5));</script>
LonghronShen
冥殇
zw蓝魔
LonghronShen
LonghronShen
这方案我也想到了
展开Biu
romanosovsky 发表于 2012-4-20 23:37转代码。c++的
http://okmij.org/ftp/cpp-digest/Lambda-CPP-more.html
template
这方案我也想到了,这应该是比较好的方案之一,思路和我那个方案差不多。
[查看全文]
romanosovsky
本帖最后由
展开Biu
本帖最后由 romanosovsky 于 2012-4-20 23:40 编辑
转代码。c++的
http://okmij.org/ftp/cpp-digest/Lambda-CPP-more.html
template<typename T>
class CurryAdd {
T a;
public:
CurryAdd(const T a) { this->a = a; }
CurryAdd *本站禁止HTML标签噢* operator() (const T b) { return CurryAdd *本站禁止HTML标签噢* (a + b); }
operator T(void) { return a; }
};
template<typename T>
CurryAdd *本站禁止HTML标签噢* cadd(const T a) { return CurryAdd *本站禁止HTML标签噢* (a); }
main() {
cout << cadd(1) << endl;
cout << cadd(1)(2) << endl;
cout << cadd(1)(2)(3) << endl;
cout << cadd(3.4)(4.5) << endl;
cout << cadd(1)(3.5) << endl; // careful !
}
<和>被转成lt gt了@62#
[查看全文]
LonghronShen
Flagalac
