Recent posts about LINQ and related topics by ThinqLinq

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 -

Tools and Apps

Now that I’ve been at Slalom Consulting for 3 years, it’s about time for a hardware refresh. In preparation for that, and inspired by the recent list by Rocky Lhotka of top Win8/RT apps, I thought I would share the list of software/tools that I have found indispensible over the last several years creating software for customers. This is by no means as extensive as the impressive list of tools that Scott Hanselman puts out each year. A while back, I published a list of LINQ tools if you’re looking for some of those.

This list is primarily intended as a list to help me when I build my next laptop and offered here in case you find it helpful as well. It is by no means an endorsement of any specific product and I’m not paid to publish any of these. Feel free to add to this list in the comments as I may well have forgotten something along the way.

System apps and tools

Windows Home Server I use this to seamlessly backup all of the computers on my home network. It’s saved my rear multiple times and makes trying out new builds of Windows and other applications a painless task. I’m still using the original version and it’s been grinding along smoothly for years now.
Windows Live Writer The best tool I’ve found for offline creating/editing of blog posts. So great, I’m using it to write this post.
Paint.Net Impressive free image editor that includes many of the features of Photoshop without the license costs. Includes multiple layers and special effects.
WinDirStat Do you ever wander what’s taking up so much space on your drives? WinDirStat is a nifty way to visualize your space usage and can break it down by folder/file type/etc. It’s an easy way to find those screen capture videos, vhd images, and audio files that can get scattered around the drive over time.
TouchFreeze This simple utility blocks the annoying mouse jump that can happen if your thumb accidently brushes the touchpad while typing.
Syncfusion Metrostudio Over 2500 metro-style icon templates that are free (in exchange for giving them your email address that they will use to spam up) and customizable either as XAML or png images. I’ve used these a number of times when needing an icon for a customer and changed the colors to match their brand.
FileZilla The best free FTP client I’ve found yet.
EasyBCD GUI to manage your boot image. Helps out in a pinch when the boot sector gets corrupted or you want to configure dual boot to a VHD image for testing new OS images. It has a free non-commercial version as well as a minimal cost commercial version.
Oracle VirtualBox Free virtual machine tool. I’ve found this to be lighter weight and easier to configure than HyperV or VmWare. I’m sure there are features that it doesn’t include, but so far I haven’t had the need for the bigger tools except for running the Windows Phone emulator which does require HyperV.
ProcessMonitor Low level debugging tool for when you want to see in minute detail what your drives are doing. The information that it provides can be overwhelming, but I’ve found it useful for tracking down pesky file not found issues where a system is looking for a file in a different path than where it actually exists.
Metrotwit This has been my go-to twitter client for years. Unfortunately due to Twitter’s access token limit, they recently announced that they will no longer support new users or upgrades. I may have to switch back to Tweetdeck or Tweetium once the new computer comes but wanted to leave this on my list in tribute to the app that was.
Foxit Reader Much smaller and faster than Adobe Reader. Handles 95% of my pdf reading needs with less overhead.

Development Tools

Visual Studio The best development IDE out there. I spend most of my time working in the .Net world, but have had to work with a couple others including Eclipse and am much happier and productive with VS than the other tools.
SQL Server Management Tools These are the tools for working with SQL Server including SSMS/ SSDT/ SQL Profiler and Tuning Advisor.
LINQPad Any time I want to test out some code snippets but don’t want to crank up a new project and build a UI, I reach out for LINQ Pad. We liked it so much that we published all of the LINQ in Action samples for you to use in LinqPad.
ILSpy Although not as full featured as the no longer free Reflector, I reach out to the open-source .NET assembly browser and decompiler, ILSpy whenever I want to know how a .Net DLL works under the covers and I don’t have access to the original source code.
WebMatrix If I’m doing a simple site and don’t need the full Visual Studio experience, I often reach out to the free WebMatrix as a web development sandbox.
Dependency Walker This simple tool lets you check the project dependencies of compiled modules which is helpful when trying to understand how existing systems are constructed so that we can try to extend them with additional functionality.
Tortoise SVN If I’m doing work for a client that uses SVN as their source repository, I often use this to integrate the source repository directly into windows explorer.
GitHub for Windows The easiest way to get started with GitHub on Windows computers without the need to fumble around with the command line.
Fiddler Anytime I need to sniff web requests or test services, Fiddler is my tool of choice. Not only are you able to see the requests and responses, but you can easily create new ones by dragging a previous request to the compose tab and modifying the URI/header/footers to meet your needs and issue the request directly without needing to mess with a browser.
WinMerge Often I need to compare files or directories to merge them between different versions. There are a number of diffing tools, but this one lets you quickly diff and merge them.

Visual Studio Extensions

I typically do not install many extensions including the popular CodeRush, Resharper, or JustCode because I’m frequently giving presentations and attendees often are more focused on the plug in tools that I’m using than the task at hand. As a result, I typically use a cleaner install of Visual Studio with just a handful of useful tools.

Nuget If you’re using Visual Studio and haven’t heard of Nuget, you’ve probably been living under a bridge for the last couple of years. NuGet is the way to manage external packages for your applications. Increasingly, parts of .Net including MVC, Entity Framework, Web API, and others are being distributed through NuGet rather than as part of large .Net version upgrades. It’s a must have extension for Visual Studio at this point.
Web Essentials Adds extensions into the web tools (including Html, javascript, css) that they couldn’t fit into the typical product cycles. There are versions for 2010, 2012, and 2013.
Chutzpah Test Runner Visual Studio plug in that allows you to run JavaScript QUnit, Jasmine, and Mocha unit tests the same way you run your MSTest/XUnit tests for .Net code—directly within Visual Studio. Also allows you to debug the JavaScript unit tests quickly and easily.
CssCop Integrates the CSSLint style checker into Visual Studio to help writing better CSS catching common errors that affect browser compatibility.
Azure SDK Tools and SDK’s for working with Azure services.
Fody Simple tool to weave code into your build process. In particular, I use this to add INotifyPropertyChanged events into POCO classes.
Posted on - Comment
Categories: Visual Studio -
 Next