Recent posts about LINQ and related topics by ThinqLinq

ThinqLinq Samples on Github

When I first created this site, I used it both as a test bed for technologies as I played with them and as a repository for others to use for my presentations and other insights. Over the years people have been free to grab these samples, but haven’t had good ways of giving back to help improve the samples.

In the mean time, there have been quite a number of technology changes. While I may have been late to the party on some, there’s only so many things one can focus on. One of the advances we’ve seen is the explosion of use of web based distributed source control systems. In that light and as an effort to both make it easier for you to access the sample projects, and help you give back, I’ve started publishing my samples on GitHub. While you’ll still be able to download the samples from my downloads page, I won’t be updating them as new versions ship and instead will plan to keep the samples updated on GitHub instead.

At this point, the following projects are available via my Github repository:

  • RX Samples – Includes Windows Phone, Silverlight, Windows Store App, WebForm, HTML/RxJs, and SignalR samples with code in VB, C# and JavaScript.
  • WebAPI Samples – Includes Sample recipe EF 6 models and Web API endpoints.
  • Signalr RX Samples – Includes Chat samples in Web and WPF, Reactive Sensors in Web and Console projects, and Drag/Drop with MVC and WPF

As always, please let me know what you Thinq. Does Git make accessing the samples easier? Is there something else I should add to the samples?

Posted on - Comment
Categories: C# - Entity Framework - JavaScript - LINQ - Rx - RxJs - SignalR - VB - WinRT - WP7 -

Reactive Extensions RX in Action the movie

I’ve been giving presentations for some time now. By far, my favorite talk to give is my Reactive Extensions in Action. This past summer, I had the talk recorded at the Code on the Beach conference. If you haven’t had a chance to see it live, now’s your chance. The focus of this talk is to focus on some of the practical uses of Rx rather than the underpinnings of how it works. I hope you enjoy and this sparks ideas from you on how you can use it in your applications.

 

Rx in Action recorded at Code on the Beach.
Posted on - Comment
Categories: C# - VB - Rx -

Build Impressions

Once again, I had the opportunity to attend Microsoft's //build// conference in person. SatyaThis year did not disappoint. In these days of fast internet and streaming/recorded conference sessions, I still find it beneficial to attend in person, not so much for what is said on stage, but also to see the audience reaction to announcements. In some of the informal gatherings like at lunches or at the airport, I try to make it a habit to ask people what the best thing they saw was to gain a feeling for the atmosphere that was present in rooms that I was not able to attend in person. I also try to pay attention to not only what is said, but also what is conspicuously not said at the conference. In this post, I'll try to share my thoughts from the conference on what was hot and what to keep an eye out for based on the reactions I saw.

In the opening keynote, Microsoft announced to of the most important advances: Universal Apps and the Windows Phone 8.1 with the Cortana voice based digital assistant. With Universal Apps, Microsoft is attempting to move closer to a unified programming model across devices and platforms including Desktop, Tablet, Phone, and XBox. Adding in partners like Xamarin and Unity 3D, these universal apps can also extend to Apple and Andriod as well. There is not 100% API parity across the platforms, but these changes promise to get us much closer to the silver bullet of write once-run anywhere that so many customers are looking for. At this point they indicated that there is a 90% API parity between Windows Store Apps and Windows Phone 8.1. While you can use Xamarin, Miguel did indicate that you will still need to write separate UI tiers for the Andriod and iOS platforms reusing the core shared business logic.

WindowsPhoneSpeaking of platforms, Microsoft took the wraps off of the next major update to Windows Phone. Contrary to some reports, this is a major update both from a developer and end user perspective and generated quite a bit of buzz. The changes include significant lock screen enhancements, a new notification center, and an impressive voice controlled digital assistant called Cortana cortana-windows-phonethat learns from your use and integrates with your online activities. It's like Siri and Google Now got together and had a prodigy. I didn't get to try it out first hand, so it will be interesting to see how much the demos were scripted or if it really works as well as promised. Integrating it with your apps also appears to be relatively painless so it will be interesting to see how well this takes off. The new UX features on the phone along with a unified WinRT model really make the platform something to contend with and signals the convergence that had long been rumored.

Beyond the advances in UX and platforms, developers were pleasantly surprised by the language teams with some important advances in managed languages including the release of the Roslyn compiler. After a long 5+ year effort, the teams have finally completed the rewrite of the compilers in managed languages adding in extensibility hooks for the community to build extensions to the language services. This also allowed the team to add a number of language features that had long been on the back burner, but they were now finally able to incorporate with a more manageable code base. Perhaps the biggest news item of the conference trending on Twitter and at the top of Hacker news as Anders open sourcing the Roslyn compilers. Hopefully as a result, we'll now see a greater community involvement in extending the languages in much the same way that F# has thrived from their small but enthusiastic community.

In addition to the managed languages teams, the developer division has been hard at work on a number of other things that were debuted at Build to largely positive reactions. Among these, the ones that got the most reaction were the release of Typescript 1.0, SIMD support, and .Net Native compilation for Windows Store Apps.

 

XamarinBuildEven though this was a Microsoft conference, attendees are still concerned about cross platform solutions and strategies. As a result of massive attendee interest, the Xamarin talk was moved from a regular session room to the main keynote hall. Judging from the number of seats taken up, I would guess that nearly 1/4 of all attendees came to this session. As mentioned earlier, Xamarin offers a cross platform C# solution, but does not guarantee 100% code reuse across platforms. It was good to see Microsoft acknowledging and embracing this solution and it will be interesting to see what happens going forward with Xamarin, particularly now that the C# compilers were open sourced and are already being incorporated into the Xamarin tools.

Microsoft-Azure-Management-Portal-620x300Not everything at the conference was focused on the clients. Azure and ASP.Net also got a fair amount of love as well. A couple of the more buzz worthy items here were the Azure management portal enhancements, greater integration between Visual Studio and Azure IAAS (Including remote debugging in cloud VM's), JAVA web sites (I don't see Google putting .Net in their cloud, so who's more open now?).

For ASP.Net, there were plenty of ooh's and ahs for browser link and being able to push updates to markup and styles directly from the browser back to Visual Studio. The IE11 F12 developer tools also now support map files (to debug typescript/coffee script/minified JavaScript) and marking files as library files to eliminate debugging into core frameworks (Angular/JQuery/etc) allowing you to focus on the custom application code to improve developer productivity.

I was surprised to see one technology that is still a Microsoft Research project getting so much attention in the sessions. The Orleans project for massive cloud based parallel computing was discussed in several of the sessions I attended, including one describing how it is being used in production for managing Halo game status and stats even tracking all 500,000 games that were ever played on the platform.

WinDesktopFutureThis year's Windows announcements were somewhat dwarfed by some of the others mentioned previously. Many of these enhancements are directed at improving the experience and feature discoverability for mouse/keyboard users. Perhaps the one that made the news the most was a prototype image of a possible Win v.Next start menu. I hope people aren't disappointed when they install Windows 8.1 Update and don't see this new menu because some news sites missed the fact that this is not yet officially in the product, though given the enthusiastic response it got, I wouldn't be surprised to see it appear down the road.

Perhaps more important from an enterprise perspective is the changed licensing and side loading costs for Windows. Dropping the price of Windows and Windows Phone to free for devices under 9 inches should greatly increase the number of OEM's that have preferred the free Android platform. Also the challenges and expense with procuring side loading licenses are now dramatically reduced due to policy changes coming with that distribution model. Additionally, new Brokered apps can access the full power of windows from “Store Apps” for enterprise purposes. Rocky Lhotka has a good summary of the pricing and side loading changes and the impact that they will have for enterprise deployments.

The conference isn't all fun and games. There's also sound. Being a formally trained musician, I was interested to attend a session on incorporating Midi, sequencers, and synthesis. In that session they announced a new MIDI API for WinRT to enable more music interactivity using Windows Store Apps which I need to try playing with in my free time.

Not to be accused of seeing everything through rose colored GLASSes, there were a few notable omissions at the conference. While there were some sessions regarding the "Internet of things" these sessions largely focused on abstract use of Azure in the cloud to manage a a wide array of connected devices. With Google, Sony, Facebook, Pebble, and many others currently pushing forward with an array of devices, I found it surprising that Microsoft didn't make any announcements of new devices at the conference. For a company that's trying to be a device and services company, not having a new device to announce was noticeable. I admit that the delay of the Nokia mobility acquisition and Facebook's recent purchase of Oculus may have put a wrench in the ointment, so there may be things in the works that were just not quite ready to release at this time.

HarryPearsonWith this being the first major public conference since the One Microsoft reorg, and management shuffles, I was particularly interested in getting a feel for the kinds of change Microsoft was making. In general, I found they portrayed a much younger hipper vibe and returning to a more publically open style, unlike the walled garden that was present in the Windows division with the former leadership.

Who would have thought that Microsoft would be publishing libraries written by Twitter (Bootstrap), and Linus Torvalds (Git), and having people on the keynote stage wearing MiguelGoogle Glass and using a MacBook Pro. They continue to more fully embrace a more collaborative open source model with not only Roslyn being open sourced, but many of the core .Net components as well. (link to .Net open site). If there was one overarching theme from this year, it seemed to be a re-Embracing of .Net and the developer community. I like the direction that Satya Nadella appears to be heading and hope that it continues this way.

Posted on - Comment
Categories:

RxJs Select or Map

While reviewing the recent updates to RxJs and looking at some of the tutorial information, I started to get perplexed at the difference between select or map, selectMany or flatMap, and where or filter. I’ve typically used the LINQ/SQL methods of select, selectMany and where, but while reading the learnrx tutorial I noticed he was using map, flatMap, and filter and became confused as to how these are related. A quick peek at the source (which is easy with JavaScript libraries) reveals how. Here’s the signature of select and map for example:

observableProto.select = observableProto.map = function (selector, thisArg) {
    // Do amazing stuff       
};
So we can see that select and map both extend the Observable prototype and alias the same function. The same is true of selectMany/flatMap and where/filter. As a result, which should you use? It doesn’t matter. Use the syntax that you’re more comfortable with. As a LINQ guy who started with Access in my professional career, I thinq I’ll probably stick to the SQL like methods, but don’t be surprised to see map/filter sneak in from time to time.
Posted on - Comment
Categories: RxJs -

Reactive Translator for RxJs

For several years, I’ve been demonstrating using Reactive Extensions to handle taking user input and providing real time translations into multiple languages. This code sample has been so popular that even Soma posted an example using the Bing translator in his introducing Rx post. In most cases, they only issue a single request or issue multiple individual requests for multiple languages. To add a bit of fun to my demo, I decided to spice it up a bit and turn the list of destination languages into another observable that I can join with as well. I’ve had this sample available in my Rx downloads for some time including one for RxJs, but haven’t updated them to newer versions (curently 2.2.2) until now. I figured it was about time that I shared my version.

To begin, let’s create a UI. We’ll keep it simple to allow us to focus on the code that comes later:

    <input type="text" id="TextToTranslate" size="80" />

    <div id="output">
    </div>

As they say at Staples, “That was easy.”

Next comes the fun part. Before you can do anything though, you do need to sign up to get a free Bing App ID. I’m not going to share mine Winking smile. Once you have that, your ready to code. But what are we going to do in our code? This example combines three observable sequences: User key presses, destination languages, and service request/responses. In order to work with the observables and to make it easy to work with our DOM, we need to start by importing two JavaScript libraries: jQuery and Rx (we only need the rx.lite.js version for this example).

    <script type="text/javascript" src="Scripts/jquery-2.1.0.js"></script>
    <script type="text/javascript" src="Scripts/rx.lite.js"></script>

Of our three observables, the second one is the easiest, so let’s get it out of the way first. To turn an array of languages into an observable sequence, we use the fromArray generator on a simple JavaScript array.

var destLanguages = ["de", "es", "zh-CHT", "fr", "it", "ar", "ht", "tlh"];
var languagesObs = Rx.Observable.fromArray(destLanguages);

With that out of the way, Let’s take a look at how we are going to issue the service requests to Bing and turn them into an observable stream. We’ll use the jQuery ajax method to issue our request to the Bing translator using jsonp and pass in the required app id, destination language (to), source language (I speak American English, so ‘from’ is ‘en-us’) and the text we want to translate (text). We can then use JavaScript promise to access this invoker and use RxJs’s fromPromise to turn it into an observable stream that returns the string result.

var translate = function (text, lang) {
    var p = { appId: appId, to: lang, from: 'en-us', text: text };
    var promise = $.ajax({
        url: 'http://api.microsofttranslator.com/V2/Ajax.svc/Translate',
        data: p,
        dataType: 'jsonp',
        jsonp: 'oncomplete',
    }).promise();
    return Rx.Observable.fromPromise(promise);
};

Now that we have these two observables, we need to wire this up with the user’s key presses. Instead of going on and on about each step, here’s the code in it’s glory detail annotated with what each line is doing.

// Get the keypresses
var textInputDetected = Rx.Observable.fromEvent($("#TextToTranslate"), 'keyup')
    // Wait for 1/2 second to see if they're still typing
    .throttle(500)
    // They're done typing. Clear the last output
    .do(function (_) { $("#output").empty(); })
    // Get the input string
    .select(function () { return $("#TextToTranslate").val(); })
    // If it's the same as the last one, don't re-issue the request
    .distinctUntilChanged()
    // Merge the search string with the list of languages
   .selectMany(function (text) {
        return languagesObs.select(function (t1) { return { source: text, dest: t1 }; });
    })
    // Call the service passing the source string and destination language
   .selectMany(function (sourceDest) {
       return translate(sourceDest.source, sourceDest.dest)
            // Return the destination language and translated result from the service
            .select(function (result) { return { dest: sourceDest.dest, translation: result }; });
    });

d = textInputDetected.subscribe(function (x) {
        // Add the result to the list
        $("#output").append(x.dest + ' : ' + x.translation + '<br />');
    },
    function (err) {
        // In case of an error, show the error
        $("#output").append(err + '<br />')
    });

We start by getting the keyup events for the input textbox and turning that into an observable stream (using fromEvent). This is an expensive service operation, so we throttle the request for 1/2 of a second before continuing to let the user finish typing. There’s no reason to send each keypress through the translator. The do method allows us to cause side effects, in this case to clear the last translated results if any exist. We then transform (using select or map) the result to get the user’s supplied text and pass that as the resulting stream. Similar to throttle, we also want to make sure that the user hasn’t just entered the same value twice which would result in the same translation results. We optimize our requests by not reissuing the request using distinctUntilChanged.

The fun stuff comes next. Here we start to merge our observables using selectMany (or flatMap) to match up the input text with the observable list of destination languages. Once we have that, we selectMany again, this time passing in the source string and destination language into the translate method that returns the observable string result and based on that result, we project out the result along with the enclosing destination language.

To conclude, we subscribe to the end observable stream and either append the result to the output list in the case of onNext, or append the error message for onError. Since this is set up over observables, the user can just start typing another string and the process will react to the request all over again. To put this all together, here is the complete code. Remember to get your AppId before you use it because the one I’m showing here won’t work.

    <input type="text" id="TextToTranslate" size="80" />

    <div id="output">
    </div>

    <script type="text/javascript" src="Scripts/jquery-2.1.0.js"></script>
    <script type="text/javascript" src="Scripts/rx.lite.js"></script>
    <script type="text/javascript">
        var appId = "GetYoursAt http://www.bing.com/developers/appids.aspx";
        var destLanguages = ["de", "es", "zh-CHT", "fr", "it", "ar", "ht", "he", "ja", "ko", "no", "ru", "th", "tlh"];

        (function ($) {

            var translate = function (text, lang) {
                var p = { appId: appId, to: lang, from: 'en-us', text: text };
                var promise = $.ajax({
                    url: 'http://api.microsofttranslator.com/V2/Ajax.svc/Translate',
                    data: p,
                    dataType: 'jsonp',
                    jsonp: 'oncomplete',
                }).promise();
                return Rx.Observable.fromPromise(promise);
            };

            // Turn the language array into a cold observable for the selectMany below
            var languagesObs = Rx.Observable.fromArray(destLanguages);

            // Get the keypresses
            var textInputDetected = Rx.Observable.fromEvent($("#TextToTranslate"), 'keyup')
                // Wait for 1/2 second to see if they're still typing
                .throttle(500)
                // They're done typing. Clear the last output
                .do(function (_) { $("#output").empty(); })
                // Get the input string
                .select(function () { return $("#TextToTranslate").val(); })
                // If it's the same as the last one, don't re-issue the request
                .distinctUntilChanged()
                // Merge the search string with the list of languages
                .selectMany(function (text) {
                    return languagesObs.select(function (t1) { return { source: text, dest: t1 }; });
                })
                // Call the service passing the source string and destination language
                .selectMany(function (sourceDest) {
                    return translate(sourceDest.source, sourceDest.dest)
                        // Return the destination language and translated result from the service
                        .select(function (result) { return { dest: sourceDest.dest, translation: result }; });
                });

            d = textInputDetected.subscribe(function (x) {
                    // Add the result to the list
                    $("#output").append(x.dest + ' : ' + x.translation + '<br />');
                },
                function (err) {
                    // In case of an error, show the error
                    $("#output").append(err + '<br />')
                });
            
        })(jQuery);

    </script>

Thinq you can improve on this sample, let me know.

Posted on - Comment
Categories: RxJs - JavaScript - Rx -
 Next