LINQ for JavaScript & jQuery

Jan 19, 2011
0 comments

If you are familiar with C#, you probably use LINQ on a daily basis. But did you ever dream using it in your client's code. I use a lot of JavaScript in applications I develop mostly making use of jQuery library and more than once I did wished to be able to use a LINQ on client side.

Today I was facing a dilemma of querying a lot of data in JavaScript and JSON again. I did a little search and find out that there is actually a solution that allows me to use LINQ in my script.

It is called linq.js and resides on Codeplex

This library seems like a serious development effort with a lot of features. Here are some of them.
- Implement all .NET 4.0 methods
- Implement many extra methods inspired by Rx, Ruby and others
- Complete lazy evaluation
- Full IntelliSense support for VisualStudio
- Two versions - linq.js and jquery.linq.js (jQuery plugin)
- Support Windows Script Host
- Binding for Reactive Extensions for JavaScript(RxJS) and IntelliSense Generator

It's almost too good to be true, isn't it. You can see a full reference here

And here are some examples to catch your eye.

A basic one

var jsonArray = [
    { "user": { "id": 100, "screen_name": "d_linq" }, "text": "to objects" },
    { "user": { "id": 130, "screen_name": "c_bill" }, "text": "g" },
    { "user": { "id": 155, "screen_name": "b_mskk" }, "text": "kabushiki kaisha" },
    { "user": { "id": 301, "screen_name": "a_xbox" }, "text": "halo reach" }
]
// ["b_mskk:kabushiki kaisha", "c_bill:g", "d_linq:to objects"]
var queryResult = Enumerable.From(jsonArray)
    .Where(function (x) { return x.user.id < 200 })
    .OrderBy(function (x) { return x.user.screen_name })
    .Select(function (x) { return x.user.screen_name + ':' + x.text })
    .ToArray();
// shortcut! string lambda selector
var queryResult2 = Enumerable.From(jsonArray)
    .Where("$.user.id < 200")
    .OrderBy("$.user.screen_name")
    .Select("$.user.screen_name + ':' + $.text")
    .ToArray();
See how close it is to C#
// C# LINQ (delegate)
Enumerable.Range(1, 10)
    .Where(delegate(int i) { return i % 3 == 0; })
    .Select(delegate(int i) { return i * 10; });
// linq.js - anonymous function
Enumerable.Range(1, 10)
    .Where(function(i) { return i % 3 == 0; })
    .Select(function(i) { return i * 10; });

// C# LINQ (lambda)
Enumerable.Range(1, 10).Where(i => i % 3 == 0).Select(i => i * 10);
// linq.js - lambda expression
Enumerable.Range(1, 10).Where("i => i % 3 == 0").Select("i => i * 10");

// $ is default iterator variable like Scala's "_" or Groovy's "it"
Enumerable.Range(1, 10).Where("$ % 3 == 0").Select("$ * 10");
 // "" is shortcut of "x => x" (identity function)
Enumerable.Range(4, 7).Join(Enumerable.Range(8, 5), "", "", "outer,inner=>outer*inner");

// Enumerable.From is wrap from primitive array, string(to charArray), object(to KeyValuePair[]) etc..
var array = [100, 200, 30, 40, 500, 40, 200];
var ex1 = Enumerable.From(array).Distinct().ToArray(); // [100, 200, 30, 40, 500]
var ex2 = Enumerable.From("foobar").ToArray(); // ["f", "o", "o", "b", "a", "r"];
var ex3 = Enumerable.From({foo:10, bar:20}).ToArray(); // [{Key:"foo",Value:10}, {Key:"bar",Value:20}]

// C# - AnonymousType
array.Select((val, i) => new { Value = val, Index = i });
// linq.js - object literal
Enumerable.From(array).Select("val,i=>{Value:val, Index:i}")
It comes as a jQuery plugin as well. Which seems like a great option to me.
// $.Enumerable
$.Enumerable.Range(1, 10).Where("$%2==0").ForEach("alert($)");

// TojQuery - Enumerable to jQuery
$.Enumerable.Range(1, 10)
    .Select(function (i) { return $("

In overall this looks very good and states on a great new possibilities to those who creates serious applications with a lot of client side coding.
Performance, actual compatibility, convenience and other possible issues should still be checked, but no doubt that for those of us who know and love LINQ in C#, it's awesome that we can have client-side LINQ methods available.

Great job!

HTML5 in a nutshell

Jan 16, 2011
0 comments

Well, a long awaited HTML5 is here and many of developers already taste from its great features. But what about those who don't? There are a couple of things you could hear every time some new technology or technique comes to a market. The most popular beyond them are: "I don't have time to learn it", "I already have things working so why bothering" and so on. Well I have only 4 things to tell you about HTML5 that should make you jump to work on it right now. And the first one is: It is really simple to start working with HTML5 but let's take it one by one.

4 Things about HTML5

1. It's not a one big thing, it is a collection of individual features.

So you can’t detect “HTML5 support,” because that doesn’t make any sense. But you can detect support for individual features, like canvas, video, or geolocation.

2. You don't have to rewrite anything.

HTML5 is build on its predecessor HTML4. If your application worked yesterday, it will continue working on HTML5. What you can actually do is to improve things like extending standard text box element and to distinguish it to different text types like email, search etc. In older browsers it will look just the same, but a modern ones will suddenly appear to behave a lot better. For example mobile browser will show a different keyboard for email.

3. It's really, really easy and fast to get started.

“Upgrading” to HTML5 is as simple as changing your doctype. The doctype should already be on the first line of every HTML page. Previous versions of HTML defined a lot of doctypes, and choosing the right one could be tricky. In HTML5, there is only one doctype:

<!DOCTYPE html>  -  Easy right?

Upgrading to the HTML5 doctype won’t break your existing markup, because there is a backward compatibility in HTML5. It will allow you however to use new semantic elements.

4. It actually already works!

When you will start playing with your first application you'll be nicely surprised that HTML5 is already well-supported by the majority of browsers, including mobile.

A long awaited (by myself) First Post

Jan 13, 2011
0 comments

So, here we are. Hopefully my first post that will start a series of them.
Over the past few years dozens of times I told myself: "Here is the subjects you have to blog on. It is interesting and helpful". Then I thought: "I'll do it tomorrow" and putted it on the shelve along with other not published posts, articles and thoughts.

But today, actually a couple of days ago, I have finally done all the preparations for decent blog and now I am actually writing. What this blog will be about? I guess it will mostly include some technical stuff, tips, tricks and code samples. Some personal thoughts as well. Maybe.

I do promise to stay focused and not putting long hardly readable content.

Okay then, lets get started.