Array generation

Rick Waldron waldron.rick at gmail.com
Sun Jul 10 13:52:55 PDT 2011


David, I like the way you paint your bike sheds. Array.range() (or similarly
functional but differently named) is definitely another one of those
"oft-rerolled" solutions.

Rick

On Sun, Jul 10, 2011 at 4:23 PM, David Herman <dherman at mozilla.com> wrote:

> > So from this viewpoint (and regarding that example with squares), it's
> good to have also `Array.seq(from, to)` method (the name is taken from
> Erlang, I just frequently uses lists:seq(from, to) there):
>
> <bikeshed>Array.range seems like an intuitive name as well.</bikeshed>
>
> > Array.seq(1, 5).map((x) -> x * x); [1, 4, 9, 16, 25]
>
> This pattern (integer range immediately followed by map) is so common that
> many Schemes have a more general function that fuses the two traversals,
> sometimes called build-list or list-tabulate:
>
>    Array.build(n, f) ~~~ [f(0), ..., f(n-1)]
>
> Another common and useful fusion of two traversals that's in many Schemes
> is map-filter or filter-map:
>
>    a.filterMap(f) ~~~ [res for [i,x] of items(a) let (res = f(x, i)) if
> (res !== void 0)]
>
> I rather arbitrarily chose to accept both null and undefined here as way to
> say "no element" -- a reasonable alternative would be to accept *only*
> undefined as "no element".
>
> Dave
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20110710/63bb9d8d/attachment.html>


More information about the es-discuss mailing list