Compare lambda expressions

Alex via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Apr 26 07:32:59 PDT 2016


Hi all!

Not sure, if this belongs directly to the D language, or it is 
rather maths, but:
given to delegate generating functions and a main

auto func1(int arg)
{
     bool delegate(int x) dg;
     dg = (x) => arg * x;
     return dg;
}

auto func2(int arg)
{
     bool delegate(int x) dg;
     dg = (x) => arg * x * x / x;
     return dg;
}

void main()
{
     auto dgf1 = func1(4);
     writeln(dgf1(2));
     auto dgf2 = func2(4);
     writeln(dgf2(2));
     writeln(dgf1 == dgf2);
}

the last line gives false, either in this form, as well with 
.ptr, as well with .funcptr.

The question is, how to say, whether two lambda expressions are 
the same?
What if the lambda expressions are predicates? This case would be 
more important for me...

I understand, that there is only a little hope to get answer 
other then "impossible", even for the constrained case. In such a 
case, I have another opportunity:

Say, I define two lambda functions as strings in, say a json 
file. I'm going to parse them and build functions out of them.
How to force the input in a unified manner? In such a case, I 
could compare the given strings with some weird regular 
expression...
So, the question for this case is:
Is there some normalized form of writing a lambda expression? Any 
literature would be more then sufficient as an answer...

Many thanks in advance :)


More information about the Digitalmars-d-learn mailing list