Re: Extension for writing concise, expressive code

Ken Snyder
Fri, 21 Sep 2007 20:28:33 -0000

jcoglan wrote:
> 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.
>
> ...
> http://dev.rubyonrails.org/ticket/9611.
This is totally sweet.  I''ve often fleetingly wondered if it would be
possible to create iterators from strings.  This is really creative and
intuitive.  I''m not from the Ruby world, but I love it!

My only suggestion would be to require an explicit call #toFunction() so
that it would be more obvious that it is an iterator function and not
require an extra line be added to the Enumerable functions.  (if
(iterator) iterator = Function.from(iterator); )

So, for example:

[''FOO'', ''BAR'',
''BAZ''].map(''toLowerCase.toArray''.toFunction());

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

[''apples'', ''oranges'',
''plums''].sortBy([''replace'', /^(..)(.)/,
''$2$1''].toFunction());


What do you think?


- Ken Snyder