4 ms·
For comparison, the original Ruby version: def foo (n) lambda {|i| n += i } end It's five characters longer, but approximately 350% more readable. Of
by mechanical_fish 19y ago
For comparison, the original Ruby version:
def foo (n)
lambda {|i| n += i } end
It's five characters longer, but approximately 350% more readable.
Of course, a lot of that is because I'm more familiar with standard Ruby syntax than I am with this hack... but doesn't that tend to prove my point?
- mdemare 19y agoWell, by definition it's more readable, since nobody is familiar with the new notation yet. But objectively it has problems: unnecessary def and lambda keywords, using () and || for argument lists, using def .. end and {} for scopes. There's room for improvement. Personally, I like many things from Haskell's syntax. I wish I could say this in Ruby: foo = n -> x -> n += x
- Goladus 19y agoOther measures of brevity: The original ruby definition is 15 tokens, the .to_proc version is 13 tokens. Orthogonality: to_proc 3 variables (foo, n, x) 1 method call (to_proc) 1 type of separator (') 2 assignment ops (=, +=) 2 binary operators (->, .) 9 different kinds of tokens, in 5 (arbitrary) categories. Original: 3 keywords (def, lambda, end) 3 variables (foo, n, i) 5 Separators (|, {, }, (, )) 1 assignment (+=) 12 different kinds of tokens, in 4 categories. So the only way the original ruby version comes out ahead by these measures is that it has "fewer categories" of token types that are required. Also I think that with the 'def foo' declaration, 'foo' is not technically a variable. I don't really know ruby well enough. But you get the idea.