Why does Array.from also take a mapFn?

Rick Waldron waldron.rick at gmail.com
Tue Jun 25 12:12:14 PDT 2013


On Tue, Jun 25, 2013 at 3:09 PM, Hudson, Rick <rick.hudson at intel.com> wrote:

> I'm trying to understand this particular line.
>
> var squaredSmalls_try3 = Int16Array.from(smalls, map(v=> v*v));  //the
> plan is for this to work.
>
> To me I would expect a complaint that map is undefined. Should map be
> implied?
>

It looks like a leftover from copying this line:

> Int16Array.from(smalls.map(v=> v*v));

assume that he means:

Int16Array.from(smalls, v=> v*v);


Rick



> var squaredSmalls_try3 = Int16Array.from(smalls, (v=> v*v));  //the plan
> is for this to work.
> Or specified
> var squaredSmalls_try3 = Int16Array.from(smalls, Array.map, (v=> v*v));
>  //the plan is for this to work.
>
> - Rick
>
> -----Original Message-----
> From: es-discuss-bounces at mozilla.org [mailto:
> es-discuss-bounces at mozilla.org] On Behalf Of Allen Wirfs-Brock
> Sent: Monday, June 24, 2013 3:50 PM
> To: Domenic Denicola
> Cc: es-discuss
> Subject: Re: Why does Array.from also take a mapFn?
>
> First, you normally want map and other iterative array functions to return
> the same type of collect it was applied to:
>
> var smalls = Int8Array.of(34, 78, -150, 127, -3, 12);
> var negatedSmalls = smalls.map(v=> -v);       //negatedSmalltalk should
> also be a Int8Array
>
> but sometimes you don't
>
> var squaredSmalls_try1 = smalls.map(v=> v*v);   //no good if result is
> Int8Array because many values will be truncated
>
> var squaredSmalls_try2= Int16Array.from(smalls.map(v=> v*v));   // still
> no good, because intermediate array is Int8Array
>
> var squaredSmalls_try3 = Int16Array.from(smalls, map(v=> v*v));  //the
> plan is for this to work.
>
> filter, etc. doesn't have this sort of problem because the values placed
> in the target array are all values that were retrieved from the source
> array.
>
> _______________________________________________
> 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/20130625/ff64a636/attachment.html>


More information about the es-discuss mailing list