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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Haskell style list comprehensions in Ruby

87 点作者 ldubinets大约 12 年前

8 条评论

wedesoft大约 12 年前
Somewhat related: I've developed a library for operations involving multi-dimensional arrays [1, 2]. When possible it uses GCC for jit-compilation to achieve higher performance.<p><pre><code> require 'multiarray' include Hornetseye # Object lazy(4) { |i| i + 2 } # Sequence(INT): # [ 2, 3, 4, 5 ] lazy(3, 2) { |x, y| x } # MultiArray(INT,2): # [ [ 0, 1, 2 ], # [ 0, 1, 2 ] ] lazy(3, 2) { |x, y| x + 1 } # MultiArray(INT,2): # [ [ 1, 2, 3 ], # [ 1, 2, 3 ] ] lazy(3, 3) { |x, y| y + 4 } # MultiArray(INT,2): # [ [ 4, 4, 4 ], # [ 5, 5, 5 ], # [ 6, 6, 6 ] ] lazy(3, 3) { |x, y| (x + 1) * (y + 4) } # MultiArray(INT,2): # [ [ 4, 8, 12 ], # [ 5, 10, 15 ], # [ 6, 12, 18 ] ] lazy { |x,y| Sequence['n', 'p', 'r', 't'][x] + Sequence['a', 'i', 'u', 'e', 'o'][y] } # MultiArray(OBJECT,2): # [ [ "na", "pa", "ra", "ta" ], # [ "ni", "pi", "ri", "ti" ], # [ "nu", "pu", "ru", "tu" ], # [ "ne", "pe", "re", "te" ], # [ "no", "po", "ro", "to" ] ] s = lazy(33) { |i| 3 * (i+1) } # Sequence(INT): # [ 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, .... ] s.mask((s % 2).eq(0)).collect { |i| i ** 2 / 3 } # Sequence(INT): # [ 12, 48, 108, 192, 300, 432, 588, 768, 972, 1200, 1452, 1728, .... ] </code></pre> [1] <a href="https://github.com/wedesoft/multiarray" rel="nofollow">https://github.com/wedesoft/multiarray</a> [2] <a href="http://www.wedesoft.de/hornetseye-api/" rel="nofollow">http://www.wedesoft.de/hornetseye-api/</a>
tome大约 12 年前
It's neat that Ruby has programmable syntax like this, but Haskell's comprehensions are one of its worst syntactic features IMHO. They're inherently non-composable.
评论 #5694561 未加载
VeejayRampay大约 12 年前
Cue to Haskell people lamenting the fact that this is an abomination and not by any means "Haskell-style list comprehensions".<p>Still very nice though.
评论 #5693834 未加载
andkerosine大约 12 年前
This has no business being at the top of HN.
评论 #5693734 未加载
评论 #5693739 未加载
egonschiele大约 12 年前
Oh wow, that is so wrong and so beautiful.
dopamean大约 12 年前
As someone who is still learning Ruby and would like to learn Haskell I have no idea what is going on here. Would someone be kind enough to explain? Thanks.
评论 #5694695 未加载
评论 #5694054 未加载
gbog大约 12 年前
Side question, rant. I love the syntactic side of list or dict comprehension (in python) and often use them but as soon as I have to debug or expand the functionality, I have to slice them into for loop. And then I hate myself for being lazy/clever and more and more, when I start typing a = [, I hear an internal voice: wait, aren't you being wrongfully clever one again?<p>I am the only one?
评论 #5694457 未加载
评论 #5695264 未加载
stiff大约 12 年前
Wow, that's some nontrivial Ruby, some explanation of this code would be nice, I program in Ruby for some 6 years now and I still had to do a lot of head-scratching to more or less figure this out. I had no idea Ruby allows overloading of prefix operators, for example.
评论 #5695268 未加载