-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Stack Over Flow #15
Comments
convert a list of objects from one type to another using lambda expression var origList = List<OrigType>(); // assume populated
var targetList = List<TargetType>();
foreach(OrigType a in origList) {
targetList.Add(new TargetType() {SomeValue = a.SomeValue});
} var targetList = origList
.Select(x => new TargetType() { SomeValue = x.SomeValue })
.ToList(); This is using a combination of Lambdas and LINQ to achieve the solution. The Select function is a projection style method which will apply the passed in delegate (or lambda in this case) to every value in the original collection. The result will be returned in a new IEnumerable. The .ToList call is an extension method which will convert this IEnumerable into a List. If you know you want to convert from List to List then List.ConvertAll will be slightly more efficient than Select/ToList because it knows the exact size to start with: target = orig.ConvertAll(x => new TargetType { SomeValue = x.SomeValue }); In the more general case when you only know about the source as an IEnumerable, using Select/ToList is the way to go. You could also argue that in a world with LINQ, it's more idiomatic to start with... but it's worth at least being aware of the ConvertAll option. |
How can you determine a point is between two other points on a line segment? def distance(a,b):
return sqrt((a.x - b.x)**2 + (a.y - b.y)**2)
def is_between(a,c,b):
return -epsilon < (distance(a, c) + distance(c, b) - distance(a, b)) < epsilon
def isBetween(a, b, c):
crossproduct = (c.y - a.y) * (b.x - a.x) - (c.x - a.x) * (b.y - a.y)
# compare versus epsilon for floating point values, or != 0 if using integers
if abs(crossproduct) > epsilon:
return False
dotproduct = (c.x - a.x) * (b.x - a.x) + (c.y - a.y)*(b.y - a.y)
if dotproduct < 0:
return False
squaredlengthba = (b.x - a.x)*(b.x - a.x) + (b.y - a.y)*(b.y - a.y)
if dotproduct > squaredlengthba:
return False
return True |
When should you call yourself a senior developer? [duplicate] You can handle the entire software development life cycle, end to end Sometimes, a fresh punk out of college can run circles around veterans who have 20+ years of "experience". Programming is a bizarre world where code is king. Some achieve the above in 2 years or less, others take 10 years. |
learn from question and answer
The text was updated successfully, but these errors were encountered: