TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Iterators: Signs of Weakness in Object-Oriented Languages

29 点作者 pcof大约 15 年前

7 条评论

scott_s大约 15 年前
While C++0x will have proper lamdbas and closures (I've said this how many times in the past few weeks?), I think it's worth noting the "proper" way to emulate a closure in C++ is with a function object. Example:<p><pre><code> struct Summation { int&#38; sum; Summation(int&#38; s): sum(s) {} void operator()(int n) { sum += n; } }; vector&#60;int&#62; numbers; int sum = 0; // give numbers some interesting values for_each(numbers.begin(), numbers.end(), Summation(sum)); cout &#60;&#60; "sum of all numbers: " &#60;&#60; sum &#60;&#60; endl; </code></pre> C++0x lambdas will generate code very similar to this. The need to explicitly define a named struct/class and function object is an obvious weakness of this technique.<p>Although I do agree with his overall point, which is that the need to use iterators for all sequences is a code smell. It forces the programmer to work at a lower level when they often just want to say "Perform this operation on my data."
评论 #1319335 未加载
malbs大约 15 年前
Conveniently ignored Smalltalk, which at the time, would have been seeing a lot more use than it does now.<p>I missed the date of the article at first, it wasn't until I looked at the C code and had to double take it, because it wasn't ANSI C, and my initial reaction was "hang on wait a minute" then checked the date on the article.... yeah.
pcof大约 15 年前
It's worth reading the Abstract Heresies' entry that turned my attention to this paper: <a href="http://funcall.blogspot.com/2010/05/c-vs-lisp.html" rel="nofollow">http://funcall.blogspot.com/2010/05/c-vs-lisp.html</a> (After giving it a thought I posted this entry to HN too, as I probably should have done in the first place... :))<p>The date is interesting, 1992 - it would still take years for Java to implement it...wrong.
util大约 15 年前
It seems odd that he complains about lack of local (modifiable) state while at the same time talking up the advantages of pure functional programming. It's painful to have to explicitly capture state instead of depending on a closure to do it, but you can do it, and somewhat nicely using objects in C++.<p>Also, is it not right that iterators were introduced in C++ largely to allow generic functions across arrays and other data structures?<p>Finally, do iterators necessarily imply statefulness? Doesn't seem like it. Taking + 1 as "successor": double sum(const double* start, const double* end) { if (start == end) { return 0.0; } else { return *start + sum(start + 1, end); } }
huhtenberg大约 15 年前
PDF version - <a href="http://citeseerx.ist.psu.edu/viewdoc/download;jsessionid=D0759C8E59B0FC3EF23C2E6ED17A88F3?doi=10.1.1.25.1018&#38;rep=rep1&#38;type=pdf" rel="nofollow">http://citeseerx.ist.psu.edu/viewdoc/download;jsessionid=D07...</a>
aaronbrethorst大约 15 年前
Something about the writing style in the Introduction section reminds me of this classic Dilbert strip: <a href="http://tomayko.com/writings/that-dilbert-cartoon" rel="nofollow">http://tomayko.com/writings/that-dilbert-cartoon</a>
ct4ul4u大约 15 年前
It seems these are lessons we must perpetually relearn.