Improving my Lambda Expression skills - help Nick?

This forum is meant for anything you would like to share with other visitors
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

Improving my Lambda Expression skills - help Nick?

Post by Phil Hepburn »

Good afternoon all,

but particularly Nick as I think it is he who can help me out and point me in the right direction.

I am a point where I have written LINQ Extension Method syntax queries and my general Lambda syntax in X# is working and OK I feel.

What I now need is a good place to go and read Lambda expressions to solve more complex problems than most of us publish. Nick I found and old Forum post of yours which goes in the right direction - your examples on Order Items in Orders.

I suppose at the same time I need to be also exploring the Extension methods themselves, so I know what less obvious ones do. Like 'Many' just what does that do, and what of multiple variables in the Lambda code, etc., etc., etc..

Do you get my drift ? Understand my needs?

This afternoon I have added some useful parts to the XSharp eNotes file.

TIA
Phil.
Wales, UK.
NickFriend
Posts: 248
Joined: Fri Oct 14, 2016 7:09 am

Improving my Lambda Expression skills - help Nick?

Post by NickFriend »

Hi Phil,

That's pretty vague! Rather than Lambda expressions, I think you mean LINQ - the lambda expression is often just a bit of logic to tell which items or properties to include in the LINQ method.

Whichever, if you give an idea of what you want to achieve, then I can try and help. Though to be honest, although I use LINQ a lot, it tends to be fairly simple stuff, just to avoid foreach loops and such-like.

Back here re-adjusting to the cold after a couple of weeks away in Cuba and South America.... I should have stayed there!

Nick
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

Improving my Lambda Expression skills - help Nick?

Post by Phil Hepburn »

Okay then Nick and guys,

I feel not quite so vague this morning, and have made some progress with Lambda and "Method Syntax" today. Oh! happy December by the way :ohmy:

Now then Team and colleagues, I need some help with writing X# syntax for creating compilable lambda code / functions.

Can any of you help me correctly code the following :-
lambda_01.jpg
lambda_01.jpg (48.46 KiB) Viewed 276 times
I need enough code to compile and test run it with some sort of minimal output.

TIA,
Phil.
Wales, UK.
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

Improving my Lambda Expression skills - help Nick?

Post by Phil Hepburn »

Hi guys,

Since my last post I have figured a few things out and have gotten a few positive results.

I got a few tips from the official X# help notes (yes, don't laugh!) and this attached image shows my first success :-
lambda_11.jpg
lambda_11.jpg (48.83 KiB) Viewed 276 times
Having tasted some small success I then went on a couple of steps to do the following :-
lambda_12.jpg
lambda_12.jpg (100.23 KiB) Viewed 276 times
I know the example is not very great or clever, as we could far easier code the value as X * X by hand ;-0) But the principle works, and so I can make it more complex.

One thing that caught me out was that I had to use the C# conventional Lambda operator ' => ' and not that I have been using up to now in LINQ code like { |x| x.Name }.

Can you explain Robert / Chris ?

Not that it really matters, as long as we know which to use. But it may be interesting for some of us to know why this is.

Hope that this interests a few - AND - that someone can come up with the X# code syntax for the tree example I posted previously.

Cheers,
Phil.
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

Improving my Lambda Expression skills - help Nick?

Post by Phil Hepburn »

Okay then folks,

Its Friday afternoon (again) so lets make this post the last one for today !?

I have easily been able to upgrade the Lambda example to something a bit more meaningful in terms of the complexity of the task performed using the Lambda Delegate.

For those who are numerate, it is a third degree polynomial I believe.
lambda_21.jpg
lambda_21.jpg (46.02 KiB) Viewed 276 times
This is not the end of things however, as I intend to have an array of 'Formulae' type objects which have properties with values of all 5 input parameters, and use these in the delegate in the LINQ query.

Fingers crossed,
Regards,
Phil.
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

Improving my Lambda Expression skills - help Nick?

Post by Phil Hepburn »

Sorry,

For those guys who like to copy and paste code here is a chunk - you need to put the two parts in the correct place in your test app - place the deledate declaration at the top of a class, and the rest in a test method.

Code: Select all

//--- delacred at top of PRG / main Class ---
            //=== public delegate del5(i as int, a as int, b as int, c as int, d as int) as int ===

            local FormulaOf as del5
            FormulaOf := { |x,a,b,c,d| => a*x*x*x - b*x*x - c*x + d}
            //--- this is third degree polynomial ---

            local myarray as int[]
            myarray := <int>{1,2,3,4,5,6,7,8,9,10,11,12,13}
            MessageBox.Show(myarray.Length.ToString(),"my array :")

            var Formulae := from num in myarray;
                           where num >= 3 && num <= 9;
                           select FormulaOf(num, 2,3,4,5)

            local cTextF := "" as string

            foreach num as int in Formulae
                cTextF += num.ToString() + CRLF    
            next
            MessageBox.Show(cTextF,"my Formula results :")
Regards,
Phil.
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

Improving my Lambda Expression skills - help Nick?

Post by Phil Hepburn »

Hi again Forum folk,

Since I am off the air for a couple of days or so from tonight, I thought I would post my latest code 'position' with Lambda, LINQ, and related stuff. I am adding this development material to 'ClickStartXSharp' as I go.

The attached image shows a delegate which is a Lambda expression to evaluate a polynomial equation given both the value of X as well as all the four coefficients.

And yes, I know there are matrix ways of dealing with stuff like this - BUT - it is a more interesting test example than a few others I have seen in my Googling.

Robert - there are issues with the editor and new message facilities again - attacks ?

So I am trying to send the code (no formatting available either).

public class PolyForm
public property x as int auto
public property a as int auto
public property b as int auto
public property c as int auto
public property d as int auto

public constructor(nx as int, na as int, nb as int, nc as int, nd as int)
self:x := nx
self:a := na
self:b := nb
self:c := nc
self:d := nd
return
end class


//--- declared at top of PRG / main Class ---
//=== public delegate del5int(i as int, a as int, b as int, c as int, d as int) as int ===

local EquationValue as del5int
EquationValue := { |x,a,b,c,d| => a*x*x*x - b*x*x - c*x + d}
//--- this is third degree polynomial ---

local myarray as PolyForm[]
local implied PF1 := PolyForm{1,2,3,4,5}
local implied PF2 := PolyForm{2,-1,-4,4,-13}
local implied PF3 := PolyForm{3,2,22,-222,69}
local implied PF4 := PolyForm{4,2,-33,4,-5}
local implied PF5 := PolyForm{5,12,23,-45,15}

myarray := <PolyForm>{PF1,PF2,PF3,PF4,PF5}
MessageBox.Show(myarray.Length.ToString(),"my 'PolyForm' array :")

var Formulae := from oPF in myarray;
where oPF.x <= 4;
select EquationValue(oPF.x, oPF.a, oPF.b, oPF.c, oPF.d)

local cTextF := "" as string

foreach num as int in Formulae
cTextF += num.ToString() + CRLF
next
MessageBox.Show(cTextF,"my 'PolyForm' results :")

Regards,
Phil.
P.S. I will try once more in another post reply to show the image.
Attachments
lambda_31.jpg
lambda_31.jpg (31.31 KiB) Viewed 276 times
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

Improving my Lambda Expression skills - help Nick?

Post by Phil Hepburn »

Well it looks as though the attachment did get through after all. But all the controls for Inserting etc. were missing.

As too the emoticons and the formatting facility / features.

Now see you in a few days or so. Enough progress for one day.

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

Improving my Lambda Expression skills - help Nick?

Post by robert »

Phil,

In one of the last builds (1.0.1) we have added a new syntax for Lambda expressions, because the compiler sometimes had a hard time figuring out when something was meant to be a codeblock or a Lambda.
Especially in xBase style code when assigning to a USUAL or OBJECT the compiler had a hard time figuring out what was meant.
Therefore the compiler now assumes:
- Core dialect: both the codeblock syntax and the lambda syntax are both lambda expressions
- Non Core: Codeblock syntax = codeblock, Lambda syntax = Lambda.

Robert

Oh and before I forget:
In the lambda syntax the pipes are optional

Code: Select all

{ a,b,c => a*b*c}
is just as valid as

Code: Select all

{ | a,b,c | => a*b*c}
and lambda parameters can be typed as well

Code: Select all

{ a AS LONG,b AS LONG,c AS LONG  => a*b*c }
and can have REF or OUT modifiers:

Code: Select all

{ a AS LONG,b REF LONG,c AS LONG  => b := a*b*c }
XSharp Development Team
The Netherlands
robert@xsharp.eu
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

Improving my Lambda Expression skills - help Nick?

Post by Phil Hepburn »

Hi Nick, and all,

I have discovered some working code for the sort of thing I was after, and I will share it with you.

Before I make a couple of short comments, checkout the following image :-
lambda_41.jpg
lambda_41.jpg (86.61 KiB) Viewed 276 times
Look at line 74, and see there is code reference to both 's' and 'i'. In previous sample code snippets I had not realised that 'i' referred to the index of the element (often called 'e' and not 's' as here) - so | e, i | seems a common placed usage for the element and its index value.

Now then, PLEASE help me ;-0)

Anyone got any tips and/or bright ideas ? (on this general topic of Lambda expressions and their use with Collections and LINQ queries)

Phil.
Wales, UK.
Post Reply