0

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

2 Answers 2

7

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
Sign up to request clarification or add additional context in comments.

Comments

1
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

2 Comments

Pretty sure the first ; is a syntax error unless the body of the lambda is in { braces }.
Earwicker is right. The first ";" is a mistake. (force of habit)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.