Brandon
2013-06-23 23:39:29
Can we get Simba Lambda Captures?
Example:
and in PS (if we could):
It would actually be amazing.. You can just write one generic sorting algorithm and let users pass it a comparator/lambda. How difficult would this be to add in Simba?
Example:
void SortPointsByDistance(std::vector<Point> PointArray, std::function<void(const Point &One, const Point &Two)> LambdaComparator)
{
std::sort(PointArray.begin(), PointArray.end(), LambdaComparator);
}
void SortPointsByDistance(std::vector<Point> PointArray)
{
SortPointsByDistance(PointArray, [&](const Point &PointOne, const Point &PointTwo) {
int X1 = PointOne.SX - X;
int Y1 = PointOne.SY - Y;
int X2 = PointTwo.SX - X;
int Y2 = PointTwo.SY - Y;
return ((X1 * X1) - (Y1 * Y1)) < ((X2 * X2) - (Y2 * Y2));
});
}
and in PS (if we could):
Pascal:
Procedure SortPointsByDistance(Points: TPointArray; Comparator: Lambda);
begin
Sort(Points, Lambda);
end;
Procedure SortPointsByDistance(Points: TPointArray);
begin
Sort(Points, function(PointOne, PointTwo: TPoint)
begin
//Compare points here..
end;
);
end;
//OR:
Function SomeFunction(@PointOne, @PointTwo: TPoint): Boolean; //Where the @Sign captures.
begin
//Do comparison..
end;
Procedure SortPointsByDistance(Points: TPointArray);
begin
Sort(Points, @SomeFunction);
end;
//OR:
Procedure Sort(var Points: TPointArray; Compare: Function(PointOne, PointTwo: TPoint): Boolean);
begin
//Compare(Points[0], Points[1]);
end;
//Generic type.
Procedure Sort<T>(var Data: TArray; Compare: Function(One, Two: T): Boolean);
begin
//Compare(Data[0], Data[1]);
end;
It would actually be amazing.. You can just write one generic sorting algorithm and let users pass it a comparator/lambda. How difficult would this be to add in Simba?