

/*

* call-seq:

* array.collect {|item| block } -> anarray

* array.map {|item| block } -> anarray

*



* Invokes block once for each element of self. Creates a

* new array containing the values returned by the block.

* See also Enumerable#collect .

*



* a = [ “a”, “b”, “c”, “d” ]

* a.collect {|x| x + “!” } #=> [“a!”, “b!”, “c!”, “d!”]

* a #=> [“a”, “b”, “c”, “d”]

*/

static VALUE

rb_ary_collect(ary)

VALUE ary;

{

long i;

VALUE collect;



if (!rb_block_given_p()) {

return rb_ary_new4(RARRAY(ary)->len, RARRAY(ary)->ptr);

}



collect = rb_ary_new2(RARRAY(ary)->len);

for (i = 0; i < RARRAY(ary)->len; i++) {

rb_ary_push(collect, rb_yield(RARRAY(ary)->ptr[i]));

}

return collect;

}





class Object

def unfold options = {}, &incrementor

return [] unless options[:while].nil? || options[:while].to_proc.call(self)

transformed = options[:map] && options[:map].to_proc[self] || self

return [transformed] if options[:to] && options[:to].to_proc.call(self)

incrementor.call(self).unfold(options, &incrementor).unshift(transformed)

end

end

I am a long-time fan of self-hosted languages . In that post I listed the reasons I thought that a language should be mostly or entirely written in itself. Here’s another reason writing a language in itself is important: If a language’s core libraries and frameworks are written in that language, it is possible for every programmer to improve on them.Ruby’s core libraries are written in C. Here’s the source for Ruby’s collect method:Perhaps you like working with Haskell-style fold and unfold rather than the Smalltalk-style collect, select, and detect. No problem, you can hack your own in Ruby, like this:One hitch: your fold and unfold are hundreds of times slower than Ruby’s built-in-C methods and classes. It reminds me of Newton programming. Apple gave us a really cool language—NewtonScript—for writing applications. Except, the built-in applications were written in C, and the C compiler was only for Apple engineers.The good news about Ruby is that you can write your own classes in C if you want to. But that is a significant barrier to entry for many programmers, shrinking the available pool of programmers who will enhance the language.Having core libraries in C is a great choice for implementing a language that is to be used for other things like building web applications. But it is not a great choice for a language that is to be used to build other languages. And “building other languages” is exactly what Bottom-Up Programming or Meta-Linguistic Abstractions are all about. In other words, writing the core libraries in C is not a great choice for a language where programmers write their own abstractions.Now for many things, the speed penalty of writing your own abstractions in Ruby is negligible. But not everything. So there is always going to be this class of things—and I think collection manipulation is one of those things—where you need to be able to write stuff that is as good as what comes out-of-the-box.If new stuff is an order of magnitude slower, you might be able to use it for non-critical things, but your chances of persuading anyone else to use it are very low. Which means that the language as a whole progresses slowly because real progress can only happen in areas where performance doesn’t matter. Like database-bound web applications.Having an implementation where the built-in stuff is on the same footing as your stuff opens up the doors for actual progress. It forces the language itself to be Good Enough, and it makes it possible for every Ruby programmer to improve the language.Java has this incredibly powerful and popular IDE, Eclipse. It is so powerful that many people feel it is impossible to write production Java code without it. Why is it so powerful?One of the major reasons it is both powerful and popular is the availability of plug ins. It seems to support the language, UML diagrams, source code control, and everything up to (I’m pretty sure) time tracking for client billing. Naturally, the plug ins are written in Java, just like the almost all of the built-in functionality.Clara Creative can write her own plug in and it won’t be a second-class citizen. And since it is a tool for Java programmers, Clara Creative already knows how to write plug ins, she doesn’t need to drop into another language.Wow, that is neat. And it does help explain why Eclipse has so many plug ins, and why they are popular: there is no low-level language barrier, and they all on an equal footing with each other.This shouldn’t surprise you. Eclipse evolved from IBM VisualAge Micro , which was written by Smalltalk programmers in Smalltalk. And of course, Smalltalk is a language where almost everything is written in Smalltalk itself. Smalltalk programmers expect to be able to extend the language and environment without penalty.In the end, the choice of whether to implement core features in C or Ruby will always be difficult. The temptation to optimize for speed will always be strong, especially when the language is fighting for mind share. But extensibility and variety is also a win, and Ruby fights with its libraries and features as much as with its performance.Perhaps we won’t all be using fold and unfold instead of collect, select, and detect. But if we aren’t, it ought to be because we prefer the originals, not because the replacements are crippled in comparison, or because the kind of person who likes inventing new tools prefers to write them in Ruby instead of in C.I’m looking forward to hearing more from the Rubinius team. I really think they hold the key to the future. Thanks, Ezra, for your comment.

Labels: ruby