Reputation: 175
I have the following code:
scaleV (xs, c) => [c * xs[0], c * xs[1], c * xs[2]];
double phi = (1 + sqrt(5))/2;
List<List<double>> v = [[ 0.0, -1.0, phi],
[-phi, 0.0, 1.0],
[-1.0, phi, 0.0],
[ 1.0, phi, 0.0],
[ phi, 0.0, 1.0]];
To negate each vector, I would expect I could use map(), which is a common functional construct:
var w = v.map((x) => scaleV(x, -1.0));
w[0], w[1], etc. are apparently not directly usable. In the browser console I get "TypeError: w.$index is not a function" when trying to use w[0]. Also "w is List" is false.
I expect this has something to do with the documentation for the map() method:
Returns a lazy Iterable where each element e of this is replaced by the result of f(e).
But I'm not completely sure I understand this. Using List.from on the above doesn't seem to work either.
How do I use map() ?
Upvotes: 0
Views: 2037
Reputation: 13560
As you mentioned, map() returns an iterable. You can iterate over it, for example using forEach
, but you can not access elements by their index.
You can call toList
to create a list of it.
var w = v.map((x) => scaleV(x, -1.0)).toList();
assert(w is List);
But it should also work with the List.from constructor. I'm not sure why isn't working, do you get an exception?
var w = new List.from(v.map((x) => scaleV(x, -1.0)));
assert(w is List);
Upvotes: 1