Why does Array.from also take a mapFn?

Hudson, Rick rick.hudson at intel.com
Tue Jun 25 12:09:33 PDT 2013


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?
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. 



More information about the es-discuss mailing list