Extension for writing concise, expressive code

jcoglan
Fri, 21 Sep 2007 18:50:06 -0000

Hello all,

I''ve just released a bit of code I''m calling
''Reiterate'' that aims to
allow you to use strings, arrays and hashes where you''d normally use
iterator functions in Enumerable methods. Think of what
''pluck'' and
''invoke'' do, but applied to lots of other methods.

It works by defining a #toFunction instance method on String, Array
and Hash and using those to create iterator functions from those data
types. Some examples:

[''FOO'', ''BAR'',
''BAZ''].map(''toLowerCase.toArray'')
// -> [[''f'', ''o'', ''o''],
[''b'', ''a'', ''r''],
[''b'', ''a'', ''z'']]

var divs = someDivs.findAll({hasClassName: ''myClass'', visible:
true});

[''apples'', ''oranges'',
''plums''].sortBy([''replace'', /^(..)(.)/,
''$2$1''])
// -> ["oranges", "apples", "plums"]

Those are fairly contrived, I know, but they illustrate things fairly
well. More info here: http://blog.jcoglan.com/reiterate/ -- I''d really
appreciate some feedback on how useful this is; it''s cleaned things up
for me in a few places.

Also, I''d like to see this go into the core if people think
it''s worth
it. I think it sits quite well with Prototype''s aim of easing
development, and it provides a nice mirror of Symbol#to_proc and
Methodphitamine (to some extend) for those of us into Ruby. See the
ticket and consider giving a +1 if you like it: