

Anaphora in Ruby - raganwald
http://github.com/raganwald/homoiconic/blob/master/2009-09-22/anaphora.md#readme

======
arohner
I love Reg's writing. It's consistently good and well thought out.

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.

~~~
mr_justin
How is the lack of an anaphoric expression in Ruby a wart or even a
fundamental problem?

~~~
raganwald
I wouldn't call the lack of anaphoric expression a problem, but I prefer that
it be easier to add anaphoric expression to Ruby.

------
bravura
On of my friends wrote his Ph.D. thesis on anaphora resolution. In it, he used
homoerotic literature as the corpus. (It's much harder to figure out which
"he" the text means when every character is male.)

~~~
raganwald
I wish I had the talent to rewrite the article using homoerotic examples. That
would be awesome! Sigh...

------
draegtun
Most Perl programmers think "it" when they use or see $_

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;

------
judofyr
1\. "Ahaphora" - Weird word, could it be ...

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!

~~~
chriseppstein
Indeed. It's a consistently good blog. Many thanks to Reginald for his hard
work on it.

------
petercooper
I've read a lot of Ruby code over the years and never seen "if [..] _then_ "
used so much as in this (otherwise fun) piece. Have I just been lucky and it's
actually more commonly used than I think? :)

~~~
raganwald
You need "then" or a colon when compressing an if statement onto one line. My
production code generally puts the test expression in trail (foo if bar) when
things fit on one line and therefore I almost never use it.

It's in the blog piece because when talking about things resembling an English
sentence, sometimes writing them on one line highlights the relationship.

~~~
petercooper
Oh yeah, I see that, but it's in all but one of the multi-liners too. Not that
I'm against the idea - it's just not something I've seen much of :)

~~~
raganwald
I guess I was trying to be consistent. I'll fix the multi-liners. if it helps
any, I forgot how to write a one line if with the test expression in head
position and had to look it up.

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

------
sjs
3 years late ;-)

[http://dogbiscuit.org/mdub/weblog/Tech/Programming/Ruby/Meth...](http://dogbiscuit.org/mdub/weblog/Tech/Programming/Ruby/MethodMissingMagic)

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

------
ionfish
The first example,

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

~~~
raganwald
Although point-free style achieves (or surpasses) the brevity of anaphoric
style, it does so in a manifestly different way. To use two Ruby examples,

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

------
raganwald
Also: [http://groups.google.com/group/ruby-talk-
google/browse_threa...](http://groups.google.com/group/ruby-talk-
google/browse_thread/thread/26445dcef22f5a5/1772d0c487d4c570?hl=en&lnk=ol&);

