Array generation

David Herman dherman at mozilla.com
Sun Jul 10 13:23:30 PDT 2011


> 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



More information about the es-discuss mailing list