Live data from Hacker News

(1..100).inject(&:+)

weblog.raganwald.com

11–20 of 66 posts

Re: (1..100).inject(&:+)

#11
post #9
post #2

Why I hate ruby.

Since I don't use ruby either...I tried a web based interpreter. >> (1..100).inject(&:+) TypeError: wrong argument type Symbol (expected Proc) from (irb):1 I also am not a fan of doing things a thousand different obscure ways. I like doing things one way, and having that way highly optimized. Typing 10 characters or 20 characters doesn't impact my programming speed, I spend far more time thinking about how to do thin…

It won't work unless your web-based interpreter runs Ruby 1.9, or you also paste in his monkeypatch to the Symbol class.

Re: (1..100).inject(&:+)

#12
post #9
post #2

Why I hate ruby.

Since I don't use ruby either...I tried a web based interpreter. >> (1..100).inject(&:+) TypeError: wrong argument type Symbol (expected Proc) from (irb):1 I also am not a fan of doing things a thousand different obscure ways. I like doing things one way, and having that way highly optimized. Typing 10 characters or 20 characters doesn't impact my programming speed, I spend far more time thinking about how to do thin…

[deleted]

Re: (1..100).inject(&:+)

#13
post #9

Earlier quoted context omitted.

Since I don't use ruby either...I tried a web based interpreter. >> (1..100).inject(&:+) TypeError: wrong argument type Symbol (expected Proc) from (irb):1 I also am not a fan of doing things a thousand different obscure ways. I like doing things one way, and having that way highly optimized. Typing 10 characters or 20 characters doesn't impact my programming speed, I spend far more time thinking about how to do thin…

It won't work unless your web-based interpreter runs Ruby 1.9, or you also paste in his monkeypatch to the Symbol class.

I refuse to be blamed for Symbol#to_proc, it's a part of Rails and also Ruby Facets.

Re: (1..100).inject(&:+)

#14
The 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 extension methods; Sum is an extension method.)

Re: (1..100).inject(&:+)

#15
post #9
post #2

Why I hate ruby.

Since I don't use ruby either...I tried a web based interpreter. >> (1..100).inject(&:+) TypeError: wrong argument type Symbol (expected Proc) from (irb):1 I also am not a fan of doing things a thousand different obscure ways. I like doing things one way, and having that way highly optimized. Typing 10 characters or 20 characters doesn't impact my programming speed, I spend far more time thinking about how to do thin…

"I fail to see why this is a good thing."

Why what is a good thing? Why that line of code is a good thing? maybe it isn't, maybe arrays need a sum method, maybe you could just write:

(1..100).inject { |acc, n| acc + n }

...and get the same result. But the article has nothing to do with why that line of code is a good thing, it is about the fact that being able to modify the Symbol class in this manner is what makes a langauge like Ruby evolve from the bottom up.

So... do you think that is a good thing? Or a bad thing?

Re: (1..100).inject(&:+)

#16
post #6

Anyone want to explain that code to someone who doesn't know Ruby?

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(&:+)

#17
post #14

The 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…

The article does not claim that this is a good thing! It claims that the line of code says a lot about Ruby the language.

Re: (1..100).inject(&:+)

#18
post #14

The 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…

The article does not claim that this is a good thing! It claims that the line of code says a lot about Ruby the language.

That it's obsessed with obfuscatory, slow language constructs?

Re: (1..100).inject(&:+)

#19
post #9

Earlier quoted context omitted.

Since I don't use ruby either...I tried a web based interpreter. >> (1..100).inject(&:+) TypeError: wrong argument type Symbol (expected Proc) from (irb):1 I also am not a fan of doing things a thousand different obscure ways. I like doing things one way, and having that way highly optimized. Typing 10 characters or 20 characters doesn't impact my programming speed, I spend far more time thinking about how to do thin…

"I fail to see why this is a good thing." Why what is a good thing? Why that line of code is a good thing? maybe it isn't, maybe arrays need a sum method, maybe you could just write: (1..100).inject { |acc, n| acc + n } ...and get the same result. But the article has nothing to do with why that line of code is a good thing, it is about the fact that being able to modify the Symbol class in this manner is what makes a…

I think that style of code is rather hard to read and really doesn't add any functionality that couldn't be done in a much more readable way in the same number of characters.

So I am wondering why the author thinks its so fantastic.

As for developing the language, I am usually for flexibility and allowing anything to be modified, however this type of change strikes me as not very useful.

Re: (1..100).inject(&:+)

#20
post #18

Earlier quoted context omitted.

The article does not claim that this is a good thing! It claims that the line of code says a lot about Ruby the language.

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.

Post reply on HN