Anaphora in Ruby
github.com
github.com
But man do I cringe every time I see him do backflips to hack around the warts in Ruby. I don't understand why he spends so much effort trying to work around fundamental problems in Ruby.
Use a better language, or pull an rhickey and make another good language worth using. Reg is obviously smart enough to do it.
Reasons to do backflips:
1. They're fun in their own right.
2. I learn something about the quality of good vs. poor tools by fixing shortcomings in tools.
3. Sometimes the backflip is an exercise intended to get someone who thinks in Ruby to think about things outside of Ruby.
4. Rule 3 applies to Reg as much as his readers :-)
And oh yes, I should be very clear that I am not ready to declare that everyone should be programming in Ruby with anaphora. They are interesting, and I am personally happy with String#to_proc, but beyond that... Hard to say...
Examples of using "it":
map { (1/$_)+1 } 1..100;
grep { $_ eq 'something' } @list;
You can also replicate the if and big_long_calculation using "for": for (big_long_calculation) {
$_->foo;
}
But if u prefer to stick with "if" then you can do: if (my $it = big_long_calculation) {
$it->foo;
}
And remember in a lot of cases using "it" isn't mandatory: say for @list;
is same as: say $_ for @list;2. "in Ruby" - ... raganwald/homoiconic?
3. "github.com" - Maybe it's just a new project? Nah, I've never heard the word "Anaphora" before, so it's probably raganwald writing about some weird/nifty/awesome thing.
4. "raganwald" - Yes, yes, yes. But is it homoiconic?
5. clicks link - Bingo!
It's in the blog piece because when talking about things resembling an English sentence, sometimes writing them on one line highlights the relationship.
Typical dreamer behaviour, can babble about anaphora but if asked to write an if expression on one line in a job interview would bomb out...
http://dogbiscuit.org/mdub/weblog/Tech/Programming/Ruby/Meth...
This is one reason I like Ruby so much though. Replaying recorded messages is how I use syntax like "asm.mov [EAX * ECX + base], 0xdeadbeef" in my x86 assembler library (or DSL if you're trendy).
(1..100).map(&'(1/_)+1')
can be written in Haskell with sections and composition: map ((+1) . (1/)) [1..100]
It's even shorter, if you strip out the whitespace. (1..100).map(&:to_s)
(1..100).map(&it.to_s)
The first example reads to me as mapping a method over the numbers, while the second reads to me as a very terse way of describing a function that sends a method to each number.I am not doing a good job of explaining the difference in my head, but I do know that sometimes I really like point-free style because I am thinking in operations and methods rather than in functions.