SRL Archive - Suggestions

Lambdas


Brandon

2013-06-23 23:39:29

Can we get Simba Lambda Captures?


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?

NKN

2013-06-24 11:36:26

:O

I wish Java 7 had actual Lambdas instead of those fake ones. t.t
(Onto Java 8?!)

Ontopic:
This would be a really cool addition to Simba.

What would be need to be done to add it though? o.O

Brandon

2013-06-24 11:46:56

From: NKN
:O

I wish Java 7 had actual Lambdas instead of those fake ones. t.t
(Onto Java 8?!)

Ontopic:
This would be a really cool addition to Simba.

What would be need to be done to add it though? o.O



Yeah I read somewhere Java 8 will finally get them :D

I'm not sure what it would take to add them. A few of the examples above work but only the first two would be true lambdas. The last two uses Function pointers :(

I really hope it could be added.

NKN

2013-06-24 11:52:56

From: Brandon
Yeah I read somewhere Java 8 will finally get them :D

I'm not sure what it would take to add them. A few of the examples above work but only the first two would be true lambdas. The last two uses Function pointers :(

I really hope it could be added.

Time to learn Lazarus or w/e and get into the guts of Simba?

Daniel

2013-06-24 23:29:26

I highly doubt this will be implemented in PS by the Simba developer team.

But Lape, there's always hope :)

xtrapsp

2013-06-25 04:17:00

Is Lambdas just a way of writing code without having begin and end round it?

Brandon

2013-06-25 23:38:18

From: xtrapsp
Is Lambdas just a way of writing code without having begin and end round it?


No sir. Lambda stands for Anonymous function. A function that has no name and can be written inside another function or simply written within parameters. Can also be done inside a for loop such as:


for_each(int &I : SomeArray; []{
I = 10; //Change the value of I in the array.
//Or do something here..
});

find_if(Array.begin(), Array.end(), [&](int I) {return I == 10;});

//The above find_if will search an entire array and return iterators to each index that has a value of 10.
//It uses the lambda to tell it which values are 10.


Array.erase(Array.begin(), remove_if(Array.begin(), Array.end(), [&](int I) {I == 10}));

//The above erase uses the lambda to tell it which values in the array are 10.
//It then removes all indices that contain the value 10.

//See with the lambda, you can pass it any function. Anything for comparison. It doesn't have to be ints.
//And it doesn't have to be a separate/global function.


Ex (If java supported this.. Which it will in Java 8.. Hopefully):



public class Xtrapsp
{
public Xtrapsp(function F)
{
F();
}

public Xtrapsp(function F, Params...)
{
F(Params)...; //The "..." is actually an operator called the Ellipsis operator and allows you to enter 0 - any amount of parameters.
}
}

//Then you use it like:

Xtrapsp Instance([]{
System.out.println("Hello World!");
});

//Which is the equivalent of doing:


void Print()
{
System.out.println("Hello World!");
}

Xtrapsp Instance(Print);

Another usage is to declare a function within another function. Such that the inner function is encapsulated and can only be used by the outer function. No other function can see or access it. With this concept, you can write a function that sorts any and everything. Just have the user pass it a lambda/comparator function and it runs that on its data.

Can make arrays of anything. Array of functions, etc.

CynicRus

2013-06-26 02:42:44

In Delphi since 2009 this seems like:

program demo;

type
TSimpleProcedure = reference to procedure;
TSimpleFunction = reference to function(x: string): Integer;

var
x1: TSimpleProcedure;
y1: TSimpleFunction;

begin
x1 := procedure
begin
Writeln('Hello World');
end;
x1; //invoke anonymous method just defined

y1 := function(x: string): Integer
begin
Result := Length(x);
end;
Writeln(y1('bar'));
end.
But implementing this in Lazarus can be very hard work.