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 Ideally we would like to write
(1..100).inject( :+ )
The semicolon is how you tell ruby you are using a symbol.
Yes, you can write a simple sum function, but the point is that this code is more flexible in a very powerful way- we can use other functions then inject and add.
Method calling in Ruby is actually message passing. So instead of calling
1.+ 2 # => 3
You can say
1.send( :+, 2 ) # => 3
So we are not trying to convert a symbol to a proc. What we are trying to do is continually send the message ':+' to inject's built in accumulator, with every member of the list (or array) as an argument.
In ruby, the brackets are like a function, but it is actually a closure of class Proc. You can convert a different class to a proc (if it supports it) by calling to_proc.
Ruby has a syntactic shortcut, '&' for converting to a proc. Normally it actually makes the code clearer because it is also used when passing Procs onto other methods, and is a nice type annotation. So we can hack this syntactic shortcut to create
(1..100).inject( &:+ )
So by defining how '&' works by defining to_proc, we can make it create a closure (or Proc) which sends the ':+' symbol, along with any arguments that are yielded to the Proc. In this case, every value from (2..100) is yielded to the Proc. (1 will be the starting value of the accumulator.)
You can accomplish a lot of things with a closure, including this hack. But to me this shows that ruby was not designed as a functional programming language, and how by using Ruby's dangerous flexibility you can manage to warp it towards your needs.
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.
"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 &^(%*)(& do I have to know when to use a block, when to use a Proc, and how to convert between them???
For those who think the above is pretty tame and have been using it for years, we've got a series on our blog where we introduce and discuss a new method every day.
The reason I choose to continue with Ruby instead of moving to a language like Scheme has a lot to do with the community. There is so much activity in the Ruby and Rails communities that you can almost always find help with what you're trying to do, a library to make something easier, and great blogs with lots of little tricks. I just haven't seen that same level of community with languages like Scheme.
I'm not saying that you should choose your language based solely on the size of it's community (otherwise I would be pulling my hair out with Java) but the advanced features of Ruby, the expressiveness, and the community together make it a great place to be as a programmer.
actually, brackets represent a block. A block is converted to class Proc with '&'. If the object that '&' is used on is not a block, Ruby will first attempt to convert it into a Proc by calling to_proc
A Ruby block is always associated with a method, and cannot be captured in a variable without converting it back into a proc. This is done by 'removing' the '&'
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.
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).
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.
'/' in this case is the adverb 'over'. When applied to a function it causes that function to fold over a list. This is the same as inject as far as I know.
You are correct in supposing that !n generates an array of integers from 0 to n-1. It is somewhat of an unusual choice for a symbol. I've always thought of it as a factorial 'on the other side'. I don't know if that's what was intended. The k language compresses many old APL functions into symbols like this. Some make sense, some make less sense. You are after all dependent on your keyboard for the range of symbols you can type. In APL the same function was indicated by a lowercase Greek iota.
'+' is being used in two different ways. The first way shows it being used in conjunction with an adverb, so '+/' is like a one-argument function that gets applied to the list at the right, but then '+' is inserted between each of the elements of the list because of '/'.
'\' is a very similar adverb. It's called 'scan', and it's like 'over', except intermediate output is produced. It can help illustrate what's going on.
In the second case, '+' is being used as an infix operator, taking two arguments, 1 and !100, so '+' adds 1 to each element in the list 0 to 99, inclusive. This is simply what happens when you add an atom to a list. Each element in the list gets incremented by the value of the atom. Most verbs in k are like this, in that they do what you want, or maybe, they do what they should do if your preference is for something to occur rather than for an error to be thrown.