What does the &: do in Ruby?
Actually, it's &(:+) not &:(+). The & takes its argument and converts from a Proc to a block. :+ is a symbol that implements the method #to_proc to give you a block that sums its receiver and its argument.
(1..100).inject(&:+)
41–50 of 66 posts
Re: (1..100).inject(&:+)
#42The VB.NET equivalent is Enumerable.Range(1, 100).Sum, which seems clearer to me. Why is this so impressive? If Visual Basic comes close to your beloved code snippet, it's probably not that big of a deal. In real life, the answer is to know some math and realize that 1 + ... + n = n(n+1)/2, so the entire thing is pointless anyway. (And if you want to add methods to a given class ad-hoc, you can do it statically with…
Re: (1..100).inject(&:+)
#43Earlier quoted context omitted.
That it's obsessed with obfuscatory, slow language constructs?
Let me ask you a question: you arrived at the top of the article with a preconceived idea about Ruby. Did reading the article give you any insight, did you learn something about Ruby or about yourself by reading the post? If not, please downmod it, I have failed.
At the risk of getting even more downmods for saying true things here, can I just point out that it's a little arrogant to pretend that a blog post about Ruby is going to move someone towards self-actualization?
Anyway, have you failed? No. You wanted to show us your favorite line of Ruby code, and you did.
As for me, I don't really have a favorite "one line" of code, but I fondly remember when I was a kid typing in Nibble Magazine's "one and two-liner" programs. Actually, the "lines" were lines as defined by the Apple II's command line buffer limit. Here's an example:
http://www.apple2.org.za/mirrors/ground.icaen.uiowa.edu/appl...
Maybe I'm not a sufficiently abstract person. My favorite two lines of code was an honest-to-god graphical car driving program in two lines of APPLESOFT BASIC [1]. Two lines of code in a language so crappy it's basically a joke. And yet...a full-on-graphical-car-racing program. I'm strongly tempted to think that APPLESOFT BASIC is superior to Ruby, based on the evidence I've seen.
Point is: The next time you show a feature of a langauge, maybe make it solve a problem, or create something real, rather than just showing how few characters it takes to do something banal. Adding numbers together? Seriously, it's a solved problem.
[1] Sorry for the capital letters, but I'm in Apple mode now:
Re: (1..100).inject(&:+)
#44Re: (1..100).inject(&:+)
#45Earlier quoted context omitted.
If you're saying something that's actually true, you don't need to use all uppercase.
ONE PLUS ONE EQUALS TWO. God, I hope I didn't just rip open the space/time continuum.
Re: (1..100).inject(&:+)
#46So just out of curiosity .. what do you think is the most beautiful way to right the equivalent code in arc?
For Arc, right now, practically every line is an equivalent.
Re: (1..100).inject(&:+)
#47Earlier quoted context omitted.
Let me ask you a question: you arrived at the top of the article with a preconceived idea about Ruby. Did reading the article give you any insight, did you learn something about Ruby or about yourself by reading the post? If not, please downmod it, I have failed.
or about yourself At the risk of getting even more downmods for saying true things here, can I just point out that it's a little arrogant to pretend that a blog post about Ruby is going to move someone towards self-actualization? Anyway, have you failed? No. You wanted to show us your favorite line of Ruby code, and you did. As for me, I don't really have a favorite "one line" of code, but I fondly remember when I wa…
Actually, I wanted to discuss how Ruby's open classes promote language innovation from the fringe, not to try to impress you with how clever that line of code is.
"The next time you show a feature of a langauge, maybe make it solve a problem, or create something real, rather than just showing how few characters it takes to do something banal."
And there you have why my article failed you: you think it is about how useful that line of code is, or how clever it is, or what problem it solves, when I think it is an example of how Ruby is different from centrally planned language.
Is it great code? Of course not. Symbol#to_proc may even be an evolutionary dead end, AFAICT it's just a way to make OO code pretend that it's functional code.
Your criticisms point out that I need to work harder on establishing and communicating my theses, thank you.
Re: (1..100).inject(&:+)
#48Earlier quoted context omitted.
Sums the first 100 integers. You are basically injecting a function (plus in this case) between each element and evaluating it (to describe in very non-technical sense).
The same code in k: +/!100 4950 Or if you insist on 1-indexing: +/1+!100 5050
Re: (1..100).inject(&:+)
#49So just out of curiosity .. what do you think is the most beautiful way to right the equivalent code in arc?
In this case, the "equivalent" code isn't a line that produces the same result, but is instead a line that illustrates the dynamic culture of the given language's evolution. For Arc, right now, practically every line is an equivalent.
Re: (1..100).inject(&:+)
#50Earlier quoted context omitted.
The way to write this in ruby without extending the symbol class is (1..100).inject { |acc, n| acc + n } inject is a left fold or a reduce- it applies the function {acc + n} to each element in a list (or array) where acc is the result of the last function application. For such a simple function, all we need is the knowledge that we are adding, which is contained in the symbol '+', representing the addition method. So…
"After learning more languages, Ruby is starting to become pretty ugly to me, but somehow people keep managing to ignore all its faults, make little hacks like these, and be very productive with it." When I first saw it, I wondered why anyone would want this irregular little language when they could just use Smalltalk or Scheme. An early rant asked why I have to know the difference between a block and a Proc: why the…
http://blog.rubyenrails.nl/articles/category/daily_method
Here we create a to_proc method for Hash: http://blog.rubyenrails.nl/articles/2008/02/26/a-hash-is-a-f...