Live data from Hacker News

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

weblog.raganwald.com

21–30 of 66 posts

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

#21
post #19

Earlier quoted context omitted.

"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.

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

That's entirely the point. If you are in favour of flexibility, you have to accept that people will use it for all sorts of things, not just ones you like.

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

#22

Earlier quoted context omitted.

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.

Yeah I didn't mean to blame you... I actually like it, so consider it a complement, but it was just the easiest way to describe what patch I was talking about, since you had it in your article.

Any idea who specifically wrote it initially?

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

#23
post #7

Earlier quoted context omitted.

I'm not a ruby programmer and it's not immediately obvious what the result would be. I know a pretty large number of languages to the point where, unless it's LISP, I can look at code in any language and have some idea of what it does. When a languages gets "so cool" that it's cryptic than I no longer see the benefit.

Would (1..100).fold(&:+) be more obvious? How about (1..100).reduce(&:+)? If either one of those makes sense, perhaps it is a question of Matz choosing Smalltalk's names for methods (collect, select, detect, reject, inject). Please let me know if changing the name of the method doesn't help.

A little explanation in your article would've been fine. The non-ruby people in the crowd have no clue what "&:+" does, so you lost us really early on when you neglected to explain your fancy one-liner.

'inject' makes sense (in retrospect), but until it was explained, I had no clue why injecting symbol soup into a range of integers was interesting.

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

#24
post #23

Earlier quoted context omitted.

Would (1..100).fold(&:+) be more obvious? How about (1..100).reduce(&:+)? If either one of those makes sense, perhaps it is a question of Matz choosing Smalltalk's names for methods (collect, select, detect, reject, inject). Please let me know if changing the name of the method doesn't help.

A little explanation in your article would've been fine. The non-ruby people in the crowd have no clue what "&:+" does, so you lost us really early on when you neglected to explain your fancy one-liner. 'inject' makes sense (in retrospect), but until it was explained, I had no clue why injecting symbol soup into a range of integers was interesting.

"I had no clue why injecting symbol soup into a range of integers was interesting."

Lisp flavours?

"A little explanation in your article would've been fine."

Done, thank you.

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

#25
post #16

Earlier 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

What does '/' mean? Also, why does '+' seem to do different things in the first and second expressions? I suppose '!n' means "array of integers ending at n" which seems like an odd choice since it's neither 'not' nor factorial.

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

#26
Actually he is wrong about Java: people did add features to it without changing Java itself, for example AspectJ, and CLIB (I think is the name) that generates Java Code on the fly. Hibernate uses it, for example, to change the behaviour of the mapped Objects behind the scene.

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

#28
post #26

Actually he is wrong about Java: people did add features to it without changing Java itself, for example AspectJ, and CLIB (I think is the name) that generates Java Code on the fly. Hibernate uses it, for example, to change the behaviour of the mapped Objects behind the scene.

You would be amazed what people will do to Java. Just the other day I was reading something here about writing Java code using an Excel spread sheet. I'm pretty sure that wasn't in any of the JSRs.

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

#29

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.

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

#30

What does the &: do in Ruby?

Tokens beginning with a colon are literal Ruby symbols.

Prepending an ampersand before the last argument of a method call means that argument should be converted into a Proc and sent as a callback block to that method.

Post reply on HN