Lambda expressions - syntax in X# ?

This forum is meant for questions and discussions about the X# language and tools
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

Lambda expressions - syntax in X# ?

Post by Phil Hepburn »

Hi guys,

I seem to search and find nothing so far on Lambda expressions.

Can you help me by giving me a simple sample of some X# working code?

Below is a first test line, just before a commented section which works, and that I am trying to replace with the lambda equivalent.

The compiler seems to dislike the greater than sign '>' ... HELP ???

local MyPersons as ObservableCollection<Person>
MyPersons := Person.GetPersons()

Console.WriteLine("Total Person items :- "+MyPersons:Count:ToString()+CRLF)

var test := MyPersons.Where(p => p:city == "London").Select(p)

/*var results4 := ;
from p in MyPersons;
where p:Name:MiddleName != "middle ...";
orderby p:dob descending;
select CLASS {lastname := p:Name:LastName, BirthDate := p:dob, place := p:city, MidName := p:Name:MiddleName }
*/

Cheers,
Phil.
User avatar
robert
Posts: 4567
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

Lambda expressions - syntax in X# ?

Post by robert »

Phil,

Try the codeblock syntax:

Code: Select all

var test := MyPersons.Where({|p| p:city == "London"}).Select(p)
Robert
XSharp Development Team
The Netherlands
robert@xsharp.eu
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

Lambda expressions - syntax in X# ?

Post by Phil Hepburn »

Thanks Robert,

The idea of yours was right - "spot on" ! I have yet to fully try out the Select part, but here is a test piece of code which works - attached image shows it in action (working).

FUNCTION EleventhSample() AS VOID
//--- some simple tests and trials ---

Console.WriteLine("Calculated age :- "+PersonsAge(DateTime{1988,01,04}):ToString()+CRLF)

local MyPersons as ObservableCollection<Person>
MyPersons := Person.GetPersons()

Console.WriteLine("Total Person items :- "+MyPersons:Count:ToString()+CRLF)

var test := MyPersons.Where({|p| p:city != "London"}).Select({|p| class{lastname := p:Name:LastName}})

foreach var item in test
Console.WriteLine("test works :- "+ item:lastname )
next

Regards,
Phil.
RvdH_Lambda_probs_02.jpg
RvdH_Lambda_probs_02.jpg (17.45 KiB) Viewed 888 times
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

Lambda expressions - syntax in X# ?

Post by Phil Hepburn »

And here is the full and tested OK version of the previous 'simple' syntax :-

var results4 := MyPersons;
.Where({|p| p:Name:MiddleName != "middle ..."});
.OrderByDescending({|p| p:dob});
.Select({|p| CLASS {lastname := p:Name:LastName, BirthDate := p:dob, place := p:city, MidName := p:Name:MiddleName } })
User avatar
wriedmann
Posts: 3784
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Lambda expressions - syntax in X# ?

Post by wriedmann »

Looks cool, but personally I don't like this LinQ style not very much - code seems to hard to read at first glance... But this is a personal feeling, and maybe it changes with the time.

I find it impressive what X# can accomplish!

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

Lambda expressions - syntax in X# ?

Post by Phil Hepburn »

Hi Wolfgang,

YES, I agree, the Lambda version of the LINQ statements (and the 'Method' syntax) to me are less appealing (and NICE) than the 'Simple Syntax' I posted earlier.

There are a number of good reasons for developers sticking to the 'simple syntax' as I have clearly put in my 'ClickStartLINQ' eNotes.

However, since Lambda is used 'under the hood', and also on some rare occasions needs to be used along with the simple syntax, then we do need to know how to do it. I have included these special (or rare) 'Method' occasions in my eNotes. Check out '.Distinct' and 'Reverse'.

Please be aware that some experienced guys like Nick Friend use Lambda extensively and to great effect in their big business apps - he is very used to it. And it does get easier with time and practise.

I have attached an image of a useful and explanatory comment, or two. This has been in my eNotes for over four years.

... >> To help us understand the two syntaxes, here is a quote from the eBook by Pialosi and Russo :-
[ .. their book is "Programming Microsoft LINQ in the .NET Framework"]


So we need Lambda to be there, we need to know the syntax for X# and how to use Lambda when we need to - BUT - we can use the simple (sugary) syntax more on a day to day basis, and this is even less complex than the SQL Server 'T-SQL' code itself. So everyone is a winner really.

Oh! and you can mix the two as well, only using the Lambda bit (on/with the simple syntax code) when there is no alternative.

Hope this helps and informs
LambdaOrSimple_02.jpg
LambdaOrSimple_02.jpg (48.96 KiB) Viewed 888 times
,
Phil.

Regards,
Phil.
LambdaOrSimple_01.jpg
LambdaOrSimple_01.jpg (63.42 KiB) Viewed 888 times
User avatar
wriedmann
Posts: 3784
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Lambda expressions - syntax in X# ?

Post by wriedmann »

Hi Phil,

I like definitively the Lambda expressions and its power in X# - but as with every powerful tool it is very important to not overuse the power to write code that is unmaintainable. Sometimes it will be better to write a method than putting the code in a Lambda expression.

What I personally don't like is LinQ - until now I have not found any application where it simplifies my programming life. But it could be that I'll be convinced in the future - I don't exclude that - "Never say never!".

Your samples are very precious so we can see what is possible to accomplish, but it is everyones decision what to use and what not.

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
Otto
Posts: 174
Joined: Wed Sep 30, 2015 6:22 pm

Lambda expressions - syntax in X# ?

Post by Otto »

Wolfgang, did you take a look at LinQ in combination with e.g. Entity Framework (or another OR mapper)?
Normally we would write reports in SQL (storedprocedures/functions etc), but Linq makes it easier to write the same business logic as part of the application in stead of separating it in some other not so manageble environment like a dbms.

Otto
User avatar
wriedmann
Posts: 3784
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Lambda expressions - syntax in X# ?

Post by wriedmann »

Hi Otto,

currently I don't use EF or any other OR Mapper, and I don't have any plans to do it.

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
NickFriend
Posts: 248
Joined: Fri Oct 14, 2016 7:09 am

Lambda expressions - syntax in X# ?

Post by NickFriend »

Wolfgang,

Linq can make code much more readable (therefore maintainable) - sorry for the C#, it's what I'm used to

Code: Select all

int total = 0;
foreach (MyObject item in myobjectlist)
{
    if (item.MyBooleanProperty)
        total += item.MyIntProperty;
}
... or with Linq ...

Code: Select all

int total = myobjectlist.Where(i => i.MyBooleanProperty).Sum(v => v.MyIntProperty);
To my mind it's much clearer what the Linq expression is doing, rather than tracing through the logic of the foreach, and obviously this is just a stupid simple example.

Nick

PS. I also second what Otto says about ORMs.... bye bye stored procedures and all the other crap in databases!
Post Reply