Tuesday, April 28, 2009

LINQ - the mathematical aspect

One of my colleague remarked that LINQ is "VERY SIMILAR" to TSQL. However, he also cursed that why the LINQ guys have implemented it "reversely".
What he meant that why the Select is reversed…

SQL query will be

   1: Select Name from Employee where sal > 80000 




LINQ





   1: var q = from emp in Employee



   2:         where emp.sal > 80000



   3:         Select emp;




I cant help but giving him references to Korth & Sudarshan and mathematical Projections…



That also reminded me of taking a quick tour of algebra which i am posting over here.. hope it helps in rekindling some old memories…:D



Koncepts first..



Technorati Tags:


Idempotent: when the operation (on function)is performed the result remains unchanged, no matter how many times it gets performed. For example, the number 1 is an idempotent of multiplication: 1 × 1 = 1, also max(x, x) = x. More info over here. Think stability.



Closure: the smallest object that both includes the object as a subset and possesses some given property. E.g.: x + y = z; where x,y,z are Natural numbers . adding two natural numbers to get another natural number. This operation may go on for ever.


However consider x-y=z. Here if we consider x=2, y=1 then



2-1=1. Which is NOT Closure (from one end) as the above function may produce negative values. e.g. given x=1 and y=2… which yields 1-2 = –1.



Think anonymous functions or inner classes. Because of closure property the inner class . function may access the members of the enclosing class.



THINK IQueryable<T> of LINQ. It provides for domain-level closure.



The combination of above two Idempotent and closure makes algebra complex and LINQ a great fun!!!



Because of the above two we have function composition, in which output of function may be linked to input of other and so on..



IQueryables can be combined together to create new IQueryables that also yield sequences of T. LINQ is closed on both levels. It’s closed at the level of the entities that it is retrieving, but it’s also closed at the level of the functions it uses to represent queries.



Interestingly speaking, NON closure of any function have given birth to new concepts… like NON- closure of substraction of NATURAL numbers gave birth to concept of WHOLE NUMBERS..



Associativity: The composition of functions is always associative. That is, if f, g, and h are three functions with suitably chosen domains and codomains, then f ∘ (gh) = (fg) ∘ h. Since there is no distinction between the choices of placement of parentheses, they may be safely left off.



The above three concept along with concept of Inversibility provides an base of MONAD (Category theory) and thus functional programming. Quoting wiki:



If F and G are a pair of adjoint functors, with F left adjoint to G, then the composition G \circ F is a monad. Therefore, a monad is a functor from a category to itself. If F and G are inverse functors the corresponding monad is the identity functor.



in C# 3.0 queries are actually monad comprehensions in disguise.



using Lambdas




   1: var r = 1.ToIdentity()



   2:          .SelectMany(x => 2.ToIdentity(), (x, y) => x + y);



   3:  



   4: Console.WriteLine(r.Value);








using LINQ





   1: var r = from x in 1.ToIdentity()



   2:         from y in 2.ToIdentity()



   3:         select x + y;








Now; coming back to original question:  why the Select is reversed in LINQ..



Actually LINQ to objects queries are analogous to the relational algebra and therefore they are not as much declarative in nature as they are a specification of a series of operations.



1.  Each query clause is evaluated independently of the previous clauses.  Only the binding information flows between them. (Idempotent) :D



2.  The clauses are evaluated in order, hinting at imperative evaluation. (Closure) :D



therefore, the performance of LINQ is based upon the order of clauses.



As it is evident from the above discussion that LINQ works in a fashion that output of one function is input to another… In above LINQ example select clause is used AFTER from clause. Which means output from from is fed into select for filtering and projection of results.



You can refer more from here.



Do let me know your thoughts/ suggestions on same.



Happy LINQing… :D





Technorati Tags:

Thursday, April 09, 2009

Silverlight presentation

Blogged with the Flock Browser

10 reasons why silverlight 3.0 excites me…

10 reasons why silverlight 3.0 excites me…

1. It really enforces good architecture: With principals like MVC (M-V-VM) implicitly into client side Xaml, DRY(Don’t repeat yourself) Built into RIA services and Scaffolding feature (Service Context) binding with ORMs like Entity Model and L2S (LinQ to SQL) in place, the solution is rock solid and very scalable.

2. Extensible architecture: Think Dependency injection / inversion of controls. Silverlight XAML dynamic loading makes such task easy and manageable. Learning curve to achieve the same is intuitive and les cumbersome than most of the other libraries.

3. Silverlight implicit collections: Language features embedded in XAML (silverlight rendering engine langauge) are powerful. Observable collection, Converters, Dependency property really makes it easy to develop new controls.

4. Data binding features: With SL 3.0 data binding is almost like its WPF counterpart. Bind almost anything to everything declarative. Programmers delight!!!

5. Animations /2d/3d mathematics: With excellent effects and very intutive matehmatics involoved in 2d / 3d rendering, Silverlight is way ahead in terms of other packages in which programmer is supposed to do more matrix multiplications rather than business problem in hand!!! (think of programmer who has to make a custom graph)

5. No more coding for specific browsers: Code in C#. No code in javascript. No coding required for each and every version of x,y,z browsers!! And SL will ensure it runs just fine..

6. In built SEO / Deep linking:

7. Support for Dynamic languages like Iron Python and Iron Ruby: This is specially exciting. Ploygot programming was never so easy. The mathematical capabilities of languages like python (it can parse expression like (2+4) * 5 + 6 / 7…) makes it very easy when required. And the deal is required when UI is supposed to look great..

8. Great Component support:

9. Out of browser experience: With features like Isolated storage and LINQ embedded. Offline experince is really great and very easy to implement in contrast with Google gears and SQL Lite.

10. Most important: INNOVATIONS : Great tools for development like VS, Blend and even XAML PAD!! Server connectivity, Binary serialization, Deep Zoom. The pace at which SL has adapted and grown is awesome.

Blogged with the Flock Browser

Monday, April 06, 2009

Discussing SilverLight!!

I was having an discussion with my wife on things to come in computer software and silverlight. below is a brief snippet:

My wife holds an computer engineering degree and is passionate about new innovations. She is a prolific programmer though she ONLY codes when she feels like(or finds a problem worth her attention) and thus is NOT a regular coder like mortals like me who are ... well, just coders, who codes for money:D


Wife: What is this buzz around Silverlight?
Subodh: Because we need smarter applications which makes extracting the required data easy and more meaningful. See; as human beings we require information. Computers provide information to us; but extracting the data using computers is cumbersome. Not many people are skillful in extracting the right information. As developers of applications our main motive is to provide easiest way to provide the information.
RIAs are a great platform as they provide animations, have in built ways to communicate with server asynchronously. The RIA application is smart enough to do some data crunching on its own and require to connect to server only when some data is required.

As we move towards smart clients; and as more people are comfortable with browsers; the momentum is towards RIAs which combine the goodies of a smart client within browsers. Silverlight provides an easy approach towards developing RIA as developers can leverage on the knowledge they aleady have on .Net platform.

Wife: I got it... But Why Silverlight? what exactly is Silverlight?
Subodh: Actually as with every platform, Silverlight have two faces. From a user perspective, Silverlight is a browser plug-in which when installed will provide Rich user experience to the user. The plugin is free download. Also the plugin is per OS and NOT per browser. what that means is that when silverlight is installed, the application built on top of it will work exactly the same on any browser ; be is IE, firefox or flock.

From a developer perspective Silverlight is a new development platform for developing RIAs. It interestingly takes all good practices from .Net platform.
For UI: SL uses XAML;
For event handling mechanisms: Javascript may be used in SL1.0; C#, VB .net may be used for SL 2.0; and there are languages like IronRuby and IronPython which is supported Silverlight Dynamic Language Runtime(DLR).
For comminicating with Server: Silverlight supports Http WCF. It also supports duplex. It even supports Sockets.


Wife: umm..  We have Silverlight 2.0... Whats deal with 3.0 version...

Subodh: Actaully, i personally like SL because it is the MIX of so many good things. The drive for SL3.0 is to bring in good practices. Now that SL2.0 is stable, the move is towards to bring in patterns like MVC (MVVM to be correct) and domain driven design. (DDD).
So we see that SL30 have a navigation project which supports features like deep linking and back and forward history of browsers. It even supports SEO. (search engine optimization)

DDD is implemented by using a Domain service, which works on the principle of DRY(Dont repeat urself). ROR(ruby on Rails) has been using the principle successfully. Domain service takes the pain out which was required earlier like creating a WCF service exposing it and then writing plumbing code to hook to the server side WCF service. Now the code at the client side is generated at run time, so the developer concentrates on just the Domain service. The validation logic is also put as cross cutting attributes on the entity property itself.

Wife: i got the MVC and the DDD part; what else is new in SL3.0?
Subodh: SL3.0 also now supports the WPF style of databinding in which an object property may be directly binded to another object declaratively in XAML itself... There are really some cool stuff which may be archived out of that just writing plain XAML. For example: a Domain service may be declared in XAML and binded to any controls data binding property with paging, filter and grouping stuff mentioned in XAML itself!!!! :D

Wife:  thats cool... I hear there is a lot on the SL UI controls also??
Subodh: Yes there are lot of new controls, for look and feel ; for graphs and dock panel etc...Also SL3.0 also have a support for 3d now!!! :D
And then there is support for scaffolding which makes programming RIA really easy.

I hope the above conversation will help you get the basics of SL...
I will be sharing some really interesting finding and gotchas on SL in nest series of blog.
Do keep your comments/ suggestions coming in.
Blogged with the Flock Browser

Thursday, April 02, 2009

Using Flock for Blogging

All entries are blogged using FLOCK: - my favorite browser.. :D
Blogged with the Flock Browser

Silverlight Demos

Hi,
I gave a talk on silverlight capabilities and thought it is a good idea to share. The talk was intended to demonstrate the silverlight as a RIA developement tool and cool stuff which may be accomplished.

Following is the snapshot of MindMap DEMO built in silverlight 2.0.
It has Zooming, Panning features. Provides an visual feel of underlying XML data.


Below is the snap shot of famous flip book demo. I have tweaked it to get "feed" from Powerpoint slides. I actually gave the presentation while flipping the pages. :D



As talk was for developers who have experince in other technologies other tahn silverlight.. i decided to demonstrate a Visual sorting demo to provide more techie gotchas and spark. You may see Merge sort in action below


Lastly the 3d



Will be publishing the source code soon once the bandwidth allows me to!!!

Wednesday, April 01, 2009

Silverlight Presentation and demos

With the dawn of Silverlight 3.0 platform, i have realized more and more application will move towards RIA development.
Below is a presentation of the talk which briefs on Silverlight.

I have uploaded it on http://app.colaab.com/R/680fa4c6-8b3f-402e-bbd3-9bde00962502, incidentally which is also a great silver light application.

Though i like it more when it comes to architecture and coding related with silverlight, I have deliberately kept the above presentation light so that it makes sense to even a non techie.

Of course tons of Demo are included. I will be loading them as well along with the screen shots.