tags:

views:

105

answers:

2

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.

+7  A: 

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);
Daniel Earwicker
+1  A: 
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)););
James Curran
Thanks James. I still need the first ";" too right?
Joan Venge
Pretty sure the first ; is a syntax error unless the body of the lambda is in { braces }.
Daniel Earwicker
Earwicker is right. The first ";" is a mistake. (force of habit)
James Curran