I am trying to learn lambda in C# 3, and wondering how this function would be written using lambdas:
Say you have a collection of Point3 values.
For each of these points, p:
create a new p, where .Y is:
Math.Sin ((center - p).Length * f)
center and f are external variables to be provided to the function. Also Point3 type will have a constructor that takes x, y, z values.
asked Mar 18, 2009 at 20:23
Joan Venge
334k226 gold badges498 silver badges713 bronze badges
2 Answers 2
Input collection is source, output collection is result:
IEnumerable<Point3> source = ...
IEnumerable<Point3> result = source.Select(p => new Point3(p.x, Math.Sin ((center - p).Length * f), p.z);
answered Mar 18, 2009 at 20:26
Daniel Earwicker
117k38 gold badges209 silver badges290 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
List<Point> oldList = .....;
List<Point> newList = List<Point> ();
double center = ...;
double f = ....;
oldList.ForEach(p=>
newList.Add(new Point(p.X, Math.Sin ((center - p).Length * f), p.Z)););
answered Mar 18, 2009 at 20:28
James Curran
104k37 gold badges186 silver badges264 bronze badges
2 Comments
Daniel Earwicker
Pretty sure the first ; is a syntax error unless the body of the lambda is in { braces }.
James Curran
Earwicker is right. The first ";" is a mistake. (force of habit)
lang-cs