Thursday, October 28, 2010

C#: Async on the way!

Easy asynchronous communication, coming in C# 5:

I am pleased to announce that there will be a C# 5.0 (*), and that in C# 5.0 you’ll be able to take this synchronous code:
void ArchiveDocuments(List urls)
{
  for(int i = 0; i < urls.Count; ++i)
    Archive(Fetch(urls[i]));
}
and, given reasonable implementations of the FetchAsync and ArchiveAsync methods, transform it into this code to achieve the goal of sharing wait times as described yesterday:
async void ArchiveDocuments(List urls)
{
  Task archive = null;
  for(int i = 0; i < urls.Count; ++i)
  {
    var document = await FetchAsync(urls[i]);
    if (archive != null)
      await archive;
    archive = ArchiveAsync(document);
  }
}
Where is the state machine code, the lambdas, the continuations, the checks to see if the task is already complete? They’re all still there. Let the compiler generate all that stuff for you

Haskell/Python: Calling Python from Haskell

Interesting. I don't know what else to say. Embedding Python into Haskell? I can't imagine this is useful, but maybe for someone.

Wednesday, October 27, 2010

Haskell: Finger Trees

The Finger Tree is a way to implement arrays in a functional language. This description for a Haskell version is instructive.

(The example in my previous Haskell post happened to use Finger Trees.)

Tuesday, October 26, 2010

Haskell: Complete example program

Whoa! Someone has written an excellent, didactic description of a complete Haskell program. While "Learn You A Haskell for Great Good" and the Haskell Wikibook are still great ways to learn the language, this new walk-thru helps to put all the pieces together. Highly recommend.

Monday, October 25, 2010

Java/C#: Generics

Java and C# generics are quite similar, but the implementations are very different. Here are some useful links for the curious:

Summary:
In short, all that Java generics permit is greater type safety with no new capabilities, with an implementation that permits blatant violation of the type system with nothing more than warnings
Furthermore, C#/.NET convey additional performance benefits due to the lack of required casts (as the verifier ensures everything is kosher) and support for value types (Java generics don't work with the builtin types like int), thus removing the overhead of boxing, and C# permits faster, more elegant, more understandable, and more maintainable code.

Friday, October 22, 2010

Ruby: Parsing Expression Grammars

Ruby's syntactic flexibility often makes it a convenient choice for a DSL (Domain-Specific Language). Treetop is a nice way to specify a PEG (Parsing Expression Grammar) in Ruby. Here is an example.

In the example, note that the SexpParser class is defined magically via Treetop.Load(). (See Instantiating and using parsers.) I do not like such magic, but it is a common thing amongst Ruby coders.

Tuesday, October 19, 2010

Win32: Beware string-length restrictions

Some Win32 functions impose constraints on the lengths of strings. E.g. UNICODE_STRING. Here is a nasty example:

http://blogs.msdn.com/b/larryosterman/archive/2010/10/19/because-if-you-do_2c00_-stuff-doesn_2700_t-work-the-way-you-intended_2e00_.aspx

Tuesday, October 5, 2010

JavaScript: An idea for closures

Here someone advocates the Thrush Combinator (let) in lieu of anonymous functions in JavaScript. Interesting.

A beautiful memory. (Also, shifting arrays in place.)

Here is a wonderful epithet for a deceased coder, written as a description of efficient array-shifting.

In case this is ever deleted, here is the quote from Shakespeare, sonnet LX:
Like as the waves make towards the pebbled shore,
So do our minutes hasten to their end,
Each changing place with that which goes before
In sequent toil all forwards do contend.

Monday, October 4, 2010

Python: List of running processes (cross-platform)

On Linux, we have lsof. On Windows, things are much more complicated. Fortunately, there is a cross-platform Python module which can find information about all running processes, psutil.

UNIX/Python: sockets, select, and poll

Doug Hellman has provided a very readable description of how to use sockets, in Python.

Note: Most of this will not work with Windows.