4 ms·
Anaphora in Ruby
- judofyr 17y ago1. "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 17y agoIndeed. It's a consistently good blog. Many thanks to Reginald for his hard work on it.
- bravura 17y agoOn 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.)
- arohner 17y agoI 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.
- raganwald 17y ago"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." 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...
- mr_justin 17y agoHow is the lack of an anaphoric expression in Ruby a wart or even a fundamental problem?
- raganwald 17y agoI wouldn't call the lack of anaphoric expression a problem, but I prefer that it be easier to add anaphoric expression to Ruby.
- petercooper 17y agoI'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 17y agoYou 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 17y agoOh 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 17y agoI 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...
- ionfish 17y agoThe 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 17y agoAlthough 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.
- draegtun 17y agoMost 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;
- raganwald 17y agoAlso: http://groups.google.com/group/ruby-talk-google/browse_thread/thread/26445dcef22f5a5/1772d0c487d4c570?hl=en&lnk=ol& http://groups.google.com/group/ruby-talk-google/browse_threa...;
- sjs 17y ago3 years late ;-) http://dogbiscuit.org/mdub/weblog/Tech/Programming/Ruby/MethodMissingMagic 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).