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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Functional Eye for the Ruby Guy

26 点作者 rjsamson超过 12 年前

3 条评论

pjungwir超过 12 年前
I suppose this is a tangent, but a lot of this refactoring strikes me as obfuscation. Defining methods named `multiple_of_3` and `multiple_of_5` just to encapsulate a mod? I'd rather just read `x % 3 == 0` and `x % 5 == 0` than look up a method definition.<p>And then he makes it worse by throwing those out, defining `multiple_of`, and using method_missing to make `multiple_of_{3,5}` still work. A big part of reading unfamiliar code is looking up the implementation of things, and this is just making it more laborious and confusing.
评论 #5117021 未加载
评论 #5116726 未加载
gkop超过 12 年前
Not sure what version of Ruby 2 the author is using here, but sadly the latest release candidate defines the using method only in the main scope; it's not available in a class definition, where he calls it in his example.
gfunk911超过 12 年前
This doesn't seem to be really utilizing the value of lazy enumerators.<p>If this was correctly using lazy enumerators, it wouldn't require a range to be passed in, and you would take values from the resulting lazily evaluated enumerators until you found one &#62;= 1000. All the laziness does here is prevent the select from being evaluated until the sum is called.<p>I don't have 2.0 installed, so this probably has bugs/errors/etc, but I believe this is what he was going for.<p><pre><code> module TakeUntil refine Enumerator::Lazy do def take_until(&#38;b) next_obj = take(1).first if yield(next_obj) [] else [next_obj] + take_until(&#38;b) end end end end class FunctionalRuby using TakeUntil def sum_of_multiples_under(max) (1..99999999).lazy.select { |x| (x % 3 == 0) || (x % 5 == 0) }.take_until { |x| x &#62; 1000 }.sum end end FunctionalRuby.new.sum_of_multiples_under(1000)</code></pre>