Blog

EditProj: Visual Studio extension to edit project files

What’s this?

Have you ever modified a Visual Studio project file (e.g., *.csproj) by hand? If you want to do this in Visual Studio, you first have to select ‘Unload Project’, followed by ‘Edit $projectFile’ and finally ‘Reload Project’. The alternative I used is to open the project file in an external editor and, when saved, Visual Studio will prompt to reload the project automatically.

The second method proves Visual Studio will not crash if the project file is modified without it knowing it, or in other words, there is no need to unload the project first.

I’ve had to do this maneuver quite often at work, more often than I would have liked. Last weekend I made an extension for Visual Studio to allow to do exactly what I wanted: skip the unloading and reloading clicks, with the direct option to ‘Edit Project File’.

It’s called EditProj. Feedback is welcome.


Show, don’t tell

After installing EditProj, you’ll find a new item in the context menu (when you right click) of projects in the Solution Explorer (depicted above). You can now edit the project file, save it, and get a prompt to reload the project. See in action how I remove the file “LICENSE.txt” from the project.


How

After trying out different methods (which resulted in errors that the file cannot be opened), I settled for the hack of copying the contents of the project file into a temporary file, and then update the original project file with the editions made to the temporary one.

If you are interested, check out the source. Contributions / bug reports welcome!


Azure Traffic Manager external endpoints & PowerShell support

Traffic Manager is a service in Azure that allows you to load-balance applications at the DNS level.

In very few words: you can create a profile to which you add endpoints. When resolving the domain name for your profile, the DNS servers of Traffic Manager will be contacted and will return one of the endpoints that belong to that profile, depending on the load balancing method chosen. See this for more info.

Recently, some features have been added to Traffic Manager:

Support for external endpoints

Traffic Manager until now only allowed you to specify the endpoints of a profile with “Cloud Services” and Azure Websites. But no more! Now you can use any URL for endpoints to be monitored and perform the load-balancing method chosen.

This lights up some interesting scenarios such as Traffic Manager routing for on-premises deployments, websites deployed somewhere else, or even use Traffic Manager profiles as endpoints (a hack to have nested profiles).

PowerShell cmdlets

With the new PowerShell cmdlets (yes, I know what you’re thinking, but PowerShell is actually pretty nice, though unfamiliar) for Traffic Manager, you can automate the management of profiles and endpoints. Let’s see some examples:

Creating a profile

We authenticate in Azure

1
PS C:\> Add-AzureAccount

Let’s create a Traffic Manager profile

1
2
3
4
5
6
7
8
9
10
11
12
PS C:\> New-AzureTrafficManagerProfile -Name cool-profile -DomainName cool.profile.trafficmanager.net -LoadBalancingMethod RoundRobin -MonitorPort 80 -MonitorProtocol Http -MonitorRelativePath "/" -Ttl 300

TimeToLiveInSeconds : 300
MonitorRelativePath : /
MonitorPort         : 80
MonitorProtocol     : Http
LoadBalancingMethod : RoundRobin
Endpoints           : {}
MonitorStatus       : Inactive
Name                : cool-profile
DomainName          : cool.profile.trafficmanager.net
Status              : Enabled

We now see that it exists in our Azure subscription.

1
2
3
4
5
6
7
8
9
10
11
12
13
PS C:\> $profile = Get-AzureTrafficManagerProfile cool-profile
PS C:\> $profile

TimeToLiveInSeconds : 300
MonitorRelativePath : /
MonitorPort         : 80
MonitorProtocol     : Http
LoadBalancingMethod : RoundRobin
Endpoints           : {}
MonitorStatus       : Inactive
Name                : cool-profile
DomainName          : cool.profile.trafficmanager.net
Status              : Enabled

No endpoints? Nonsense!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
PS C:\> $profile | Add-AzureTrafficManagerEndpoint -DomainName www.reddit.com -Type Any -Status Enabled


TimeToLiveInSeconds : 300
MonitorRelativePath : /
MonitorPort         : 80
MonitorProtocol     : Http
LoadBalancingMethod : RoundRobin
Endpoints           : {www.reddit.com}
MonitorStatus       : Inactive
Name                : cool-profile
DomainName          : cool.profile.trafficmanager.net
Status              : Enabled

PS C:\> $profile | Add-AzureTrafficManagerEndpoint -DomainName www.meetup.com -Type Any -Status Enabled


TimeToLiveInSeconds : 300
MonitorRelativePath : /
MonitorPort         : 80
MonitorProtocol     : Http
LoadBalancingMethod : RoundRobin
Endpoints           : {www.reddit.com, www.meetup.com}
MonitorStatus       : Inactive
Name                : cool-profile
DomainName          : cool.profile.trafficmanager.net
Status              : Enabled

We now check the portal but still see no endpoints. This is because we only modified the profile instance locally. In order to send this update to Azure, we pipe the $profile into Set-AzureTrafficManagerProfile:

1
PS C:\> $profile | Set-AzureTrafficManagerProfile

That’s better!

DNS resolution

Let’s verify our setup works. We expect queries to cool.profile.trafficmanager.net to return both CNAME to www.reddit.com and CNAME to www.meetup.com (deciding whether we stay in looking at cat pictures or decide to go to a meetup). See here for the query.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
edu@thinty ~ $ dig cool.profile.trafficmanager.net

; <<>> DiG 9.8.3-P1 <<>> cool.profile.trafficmanager.net
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 60008
;; flags: qr rd ra; QUERY: 1, ANSWER: 5, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;cool.profile.trafficmanager.net. IN  A

;; ANSWER SECTION:
cool.profile.trafficmanager.net. 297 IN   CNAME   www.reddit.com.
www.reddit.com.       31  IN  CNAME   reddit.com.edgesuite.net.
reddit.com.edgesuite.net. 5440    IN  CNAME   a659.b.akamai.net.
a659.b.akamai.net.    5   IN  A   92.123.72.198
a659.b.akamai.net.    5   IN  A   92.123.72.209

We resolve again and…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
edu@thinty ~ $ dig cool.profile.trafficmanager.net

; <<>> DiG 9.8.3-P1 <<>> cool.profile.trafficmanager.net
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 47541
;; flags: qr rd ra; QUERY: 1, ANSWER: 6, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;cool.profile.trafficmanager.net. IN  A

;; ANSWER SECTION:
cool.profile.trafficmanager.net. 293 IN   CNAME   www.meetup.com.
www.meetup.com.       149 IN  A   190.93.245.104
www.meetup.com.       149 IN  A   190.93.244.104
www.meetup.com.       149 IN  A   190.93.247.104
www.meetup.com.       149 IN  A   190.93.246.104
www.meetup.com.       149 IN  A   141.101.115.104

;; Query time: 13 msec
;; SERVER: 89.101.160.5#53(89.101.160.5)
;; WHEN: Sun May 25 21:20:59 2014
;; MSG SIZE  rcvd: 157

Great, it seems to have worked! Note that we can’t expect every single query we make to return a different result, as there is caching from our machine all the way to the Traffic Manager DNS resolvers. We can now use cool.profile.trafficmanager.net to manage traffic both to reddit and meetup.com.

This was just a brief introduction into Traffic Manager and some of its new features (more coming soon). If you are interested, check out the links below or drop me a line.

Links


Competitive programming

Algorithm contests were always a bit of “so what?” for me, but the truth is, I hadn’t really tried to participate in one of them or even tried a few problems more than a couple of hours.

At Christmas, I re-assessed how I spend my free time, and I decided that some changes were in order. Among the changes, I decided I would be doing some programming challenges every week.

I started with good old TopCoder and its ugly UI. I did some practice SRMs and followed a few tutorials to refresh my mind. There are some very nice things about TopCoder:

Plugins

The existence of plugins that auto-generate testcases and the function signature allows you to focus solely on the algorithm. I’m currently using moj as it satifies my needs (it works with any text editor, and works with Java and C++), but I’ve gotten to know the more flexible CHelper. CHelper is for IntelliJ only, but works with more challenges sites: for Codeforces, CodeChef and TopCoder it autogenerates code and grabs the test cases. For any other programming challenge, you can enter the test cases into CHelper (input and output), and it will generate the checking code.

All in all, the pain of manually entering test cases for challenges from sites like HackerRank is alleviated by CHelper, but still the convenience of simply opening a problem in TopCoder and all the information you get is automatically translated into code is pretty sweet.

Tutorials

There are some very nice tutorials provided by members of the community. Assuming you know basic datastructures (read CLRS and/or Skiena’s design manual), I recommend going through the following:

  • Dynamic programming. If you’re confused about DP, first read this Quora answer, then the tutorial.

  • Binary search. Don’t expect the typical binary search on sorted arrays. This tutorial generalizes that algorithm over some ordered search space (vs array) and predicate (vs ≥).

  • Graphs 1, 2 and 3. Good to get the hang of common graph algorithms.

  • Tries. Very simple datastructure that is not that widely taught (I couldn’t find it on CLRS). There are other better tutorials for tries, though.

  • String algorithms. String search is an easy problem to describe, yet efficient algorithms aren’t trivial. I’m reading this one tomorrow.

Do not in any case limit yourself to this sample of tutorials! Take a look at the books I mentioned above and also at the list of tutorials in TopCoder.

Warning: don’t think you’ll just use one algorithm for each problem. Algorithms must be part of your toolbox when solving a challenge. I see these tutorials as presenting algorithm families that you might be able to adapt to solve some parts of the challenges.

Solutions

Another nice thing about TopCoder is the existence of match editorials: summaries about each SRM problem, going from fully implemented solutions to some vague guidelines.

The solutions submitted by other members are also available. From the Arena plugin you can see some solutions, but I found this website to be pretty convenient to see the solutions of the contestants in each SRM.

I recommend only looking at the solutions once you have a working implementation yourself or you are really stuck.

Other competition sites

There are many programming challenges websites, each with its own repository of problems. Each site has its pros and cons, but I’ve decided to stick with TopCoder for the moment as I haven’t really seen any features from other websites that I miss.

Conclusion

The best thing is to dive in and practice often, and when there’s an opportunity to participate on a competition (there’s an SRM weekly), go and try it!

I upload my solutions to GitHub in case you want to check them out.


MiXture – Bridging F# and JavaScript

In this blog post I will introduce MiXture, a project I made to bridge F# and JavaScript. MiXture allows F# code to call JavaScript and vice versa. MiXture uses V8, Google’s JavaScript engine, to execute JS code. MiXture is offered in the form of a DLL, that should work on all platforms executing .NET; however I’ve only tested this on Mono v2.10.9 for Mac, and there are high chances it won’t work out-of-the-box for other platforms.

Where MiXture came from

The implementation of MiXture is based on J.B. Matthews’ Ph.D. dissertation, where he describes the semantics of joining two languages. In his dissertation, he introduces two calculi (for an ML-like and a Scheme-like languages) to formally describe two theories for language interoperability. I won’t go into the details of his dissertation, and in fact I will only deal with the most integrated kind of interoperability: the natural embedding.

The natural embedding translates values of one language into the other. For MiXture, this is a two-way translation capability for JavaScript and F#. This is not always possible, as there might not be an exact equivalent representation in the other language (e.g., JavaScript does not have integer type numbers).

Other JavaScript bridges

While I started this project mainly from reading J.B. Matthews’ dissertation, I also encountered when I was handing in my dissertation that similar projects are out there in the wild. For instance, Apple pushed the first commit a year ago for an Objective-C — JavaScript bridge. I was quite surprised when I saw that Apple’s library coincidentally shares some of the names in the API (e.g., JSValue, JSContext). I’m glad I saw this as it validates this project is not just done from the theoretical point of view I was coming from, but also has some real-word usages :).

Check out an introduction to Apple’s library here and here.

Overview of the MiXture API

Let’s take a look at the API MiXture exposes to F# developers wanting to create applications using both languages.

JSContext

A JSContext is an execution environment that allows seperate, independent JavaScript applications to run in MiXture. JSContext is a wrapper for V8 contexts.

There are some functions in the module JSUtils to manipulate JSContexts, such as create_context: unit -> JSValue, set_current_context: JSContext -> unit and register_values: (string * JSValue) list -> unit.

JSValue

The JSValue in F# represents native JavaScript values in a specific JSContext seen in F#.

embed and project

The two main functions to work with MiXture are embed and project, which use values of the type JSValue. The JSValue in F# represents native JavaScript values seen in F#. embed converts any F# value into a JSValue and project performs the opposite operation. The type someone could expect for embed would be 'a -> JSValue, however, the type is obj -> JSValue as embed does not behave polymorphically (it inspects the type of its argument in order to dispatch it to the appropriate converting function).

For the specific case in which we are embedding a polymorphic function, we must use the function embed_poly_func, as we can only get the polymorphic type of a value using quotations. We’ll see an example of this in a code sample below.

Code samples

Let’s move to the fun part, some examples that show off some of the features of MiXture:

  • Here, we project a JavaScript function and see that MiXture supports currying:
1
2
3
4
5
6
7
8
9
10
11
12
13
// surround_str is an F# curried function
> let surround_str: string -> string -> string -> string =
    "(function(beginning, end, str) { return beginning + str + end; })"
    |> JSUtils.execute_string
    | project;;
val surround_str : string -> string -> string

> let angle_surround = surround_str "<" ">";;
val angle_surround : string -> string

> printfn "%s" <| angle_surround "Hello, world!"
// ==> "<Hello, world!>"
val it : unit = ()
  • In this other example, we show that MiXture also supports polymorphism from JavaScript to F#…
1
2
3
4
5
6
7
8
9
10
11
> let js_rev<'a> : 'a list -> 'a list =
    "Array.reverse"
    |> JSUtils.execute_string
    |> project
val js_rev<'a> : ('a list -> 'a list)

> js_rev [3.14; 2.71; 1.68]
val it: float list = [1.68; 2.71; 3.14]

> js_rev ["Life"; "is"; "simple"; "it's"; "just"; "not"; "easy"]
val it : string list = ["easy"; "not"; "just"; "it's"; "simple"; "is"; "Life"]
  • … and from F# to JavaScript:
1
2
3
4
5
6
> let js_poly_append = embed_poly_func <@ Array.append @>;;
val js_poly_append : JSValue

// register the value js_poly_append as "poly_append" in the current JavaScript context
> register_values ["poly_append", js_poly_append];;
val it : unit = ()

We can see the use of embed_poly_func here to embed a polymorphic function. poly_append is now registered in the current JSContext, and we could execute JavaScript code by passing it to the function JSUtils.execute_string. We show it as if it were an interactive JavaScript console:

JavaScript using poly_append; the lines beginning with “>” would be passed to JSUtils.execute_string
1
2
3
4
5
6
> var append_naturals = poly_append([0,1,2])
function () { [native code] }
> append_naturals([3,4,5])
0,1,2,3,4,5
> append_naturals(["hello", "world"])
Error: An F# exception occurred. Types not compatible

We see that even JavaScript is not type-safe, it is not allowed to pass the incorrect types to poly_append.

Free memory management

Another highlight is that MiXture doesn’t require manual memory management as it uses F#’s and V8’s garbage collectors.

Consider the case in which F# has a JSValue that is a V8 persistent handle. When this JSValue comes out of scope, MiXture automatically calls Persistent::MakeWeak on the V8 handle to trigger a callback from the V8’s garbage collector.

Type equivalences

Here’s a table of type equivalences for MiXture:


If you are interested in knowing more, here’s my dissertation that goes into all the gory details.

You can also check out the source on GitHub.


Hello (World) Dublin

I have been here in Dublin for almost 4 weeks now, and among other things:

  1. I have found an awesome apartment. Still need to move in.
  2. I know how to move around the center (more or less).
  3. I finally have a cell number (it took several attempts until I received a SIM card by mail).
  4. I am getting a little stressed about work (welcome to adult life!).
  5. I found out about hurling and gaelic football (weird, weird!).
  6. I’ve played some foosball in the office

Other than that, I feel like Dublin is very similar to the UK (or the UK is similar to Dublin, whatever swings your way), and people here in Ireland are really friendly.

TV shows I have been watching (no spoilers)

One can dedicate a whole blog to shows, commenting particular episodes, etc. Here is a little bit:

Suits

This show is about cool lawyers with even cooler-looking suits. It shows all the intricacies of being a lawyer at a top firm in New York (or so they say, I never had the pleasure of seeing it in real life).

I feel they have been dragging for too long a case in the third season, but I can’t say I didn’t enjoy it until the mid-season finale (mid-season finale is weird, huh? the other half of the season comes out in the winter).

Breaking Bad

This super awesome, frenetic show is coming to an end, and it couldn’t do it in a better way. For every episode I’m just blown, and we have two 75-minute long episodes left!

"Walter White vs Heisenger (main character)"


Final year of uni (Part II) recap

2012-2013 was my final academic year at university, and as such, I wanted to do the best that I could. I had learned from my mistakes from previous years, and here’s a summary.

The dissertation, a.k.a. the Part II project

This last year involved doing an individual project (usually a piece of software) with a write-up in form of a dissertation. At the time when I was selecting the topic, I was working with JavaScript in my internship, a language I started to really like. After some searching on Google Scholar, I found Jacob’s Ph.D. dissertation. He worked on designing a syntax and semantics to combine two different languages, ML and Scheme. This idea sounded interesting, and I had some experience with ML from previous courses at university. This sounded promising.

I then found that the F# authority Tomáš Petříček was a Ph.D. student at Cambridge and had some project proposals. It turns out that he had experience with JavaScript, too. So away I went and contacted him to see if he was interested in my idea: implement Jacob’s ideas for F# and JavaScript.

After a few discussions and some panicking emails from my part, we made the project idea clear. I would use Google V8 JavaScript engine to form the bridge between JavaScript and F# via PInvoke. I spent many hours coming up with ideas, disheartened because there were some problems that seemed impossible to solve. Believe it or not, hard work pays off. I managed to solve the problems (with Tomáš’ help of course), and produced a working DLL that allows you to write code like the following:

F# — Sample interoperability between F# and JavaScript — We are embedding the F# function “List.append” into JavaScript
1
2
3
4
5
> List.append;;
val it : (a list -> a list -> a list) = <fun:clo@21>
> let jappend = embed_poly_fun <@List.append@>;;
val jappend : JSValue
> register_values ["jappend", jappend];; valit:unit=()
JavaScript — Sample interoperability between F# and JavaScript — We are using the embedded function in JavaScript
1
2
3
4
> jappend ([213, 42]) ([271, 1492])
213,42,271,1492
> jappend (["hello", "world"]) (["how", "are", "you?"])
hello,world,how,are,you?

It seems that Apple had a similar project with Objective-C and JavaScript. There are others in the wild, but that’s up to you to find if you are curious.

Edit: more detailed explanation in this blog post.

Exams

At Cambridge, there is only one set of exams per year, which is taken in May-June. For us in our final year at the Computer Lab, we had 3 exams with questions from several different courses. This accounted for ¾ of our results, the remaining quarter being the evaluation of our dissertation. I’m not a fan of this grading system “all or nothing”, but one had to follow the rules.

I think I spent at least 2 days deciding which courses to prepare for the exams, and it paid off. When the goal at the end of the academic year is to obtain good results (and not necessarily focus on learning interesting topics), the best strategy is to go for the courses that:

  1. Have a comprehensible set of lecture notes, and/or an associated book which you can use in case the lecturer’s explanation is not clear.

  2. Get “unsurprising” questions in the exams. Some lecturers like to test students on topics that are not covered in lectures, so I would avoid those.

  3. Can be “practiced”. A course like Hoare logic can be practiced by doing many proofs, whereas Principles of Communications cannot.

I was very pleased with my final results, because I managed to play the game.

Next year plans

Of course, my time at university was ending, so I had to decide what I wanted to do next. I interviewed with several companies for software developer positions, and in the end I accepted an offer from Microsoft to work in their Redmond campus. However, after the visa mess that happened this year for H1Bs, I wasn’t able to secure one.

U.S. Citizenship and Immigration Services (USCIS) reached the statutory H-1B cap of 65,000 for fiscal year (FY) 2014 within the first week of the filing period, which ended on April 5, 2013.

Fortunately, Microsoft has offices all around the world, so I was told they would “relocate” my offer. It looked like I was going to be able to start working this year after all, and so it happened that I am now employed in the Dublin office (“European Development Center”).

And here I am, I moved to Dublin just over a week ago and I finished my first week working at Microsoft. It has been tough, there are many new things to learn, but I am up for the challenge.


Dogs are cool.