Friday, August 29, 2008

Podcasts

I admit it: I'm a podcast junkie! When running, commuting, walking, whatever: my iPod Nano (2nd generation) is with me practically everywhere.

Here's some podcasts that I follow. Most of them are followed by recommended episodes. Enjoy!

Hanselminutes
- What is Done? - A Conversation with Scrum Co-Creator Ken Schwaber
- Lean Software Development with Tom and Mary Poppendieck
- Quetzal Bradley on Testing after Unit Tests

Software Engineering Radio
- Simon Peyton Jones on Functional Programming and Haskell
- Retrospectives with Linda Rising (This almost made me cry, seriously!)
- The New Guardian.co.uk website with Matt Wall and Erik DoernenBurg

Deep Fried Bytes
- Talking Domain-Driven Design with David Laribee - Part 1
- Talking Domain-Driven Design with David Laribee - Part 2

Pragmatic Podcasts
- Andy Hunt on Pragmatic Wetware
- Dave Thomas on Pragmatic Publishing

ThoughtWorks - IT Matters Podcast
- Domain Specific Languages - Part 1 of 2 (no permalink)
- Domain Specific Languages - Part 2 of 2 (no permalink)

Alt.NET podcast
- (Listen to all episodes..)

Lean Agile Straight Talk
- Overcoming Impediments to Test-Driven Development
- Test-Driven Development and Design Patterns

MSDN Radio (In Swedish. The homepage is not up to date. Dag König's blog links to more episodes.)
- Magnus Mårtensson, Dotway

Agile Toolkit Podcast
- Ruby Dave
- Smalltalk Dave

.NET Rocks!
- Jon Harrop Makes Us F#
- James Kovacs Inverts our Control
- XML Literals Panel from TechEd 2008

OnSoftware (Video, so this I watch at the computer)
- F#

Also, Channel 9 has great video content!
- Brian Beckman: Don't Fear the Monads
- Anders Hejlsberg, Herb Sutter, Erik Meijer, Brian Beckman: Software Composability and the Future of Programming Languages
- Brian Beckman: Monads, Monoids, and Mort

Friday, August 8, 2008

Google Calendar



I love Google Calendar, mostly because of the free sms service, sending you an sms x minutes before an event. What's the point of otherwise having a calendar if you only get notifications from it when sitting in front of a computer, such as how ms exchange and similar works. "You need to be at the dentist in 30 minutes", well hopefully I'm sitting in front of the computer when this message pops up. Of course you can always install a mail service in your mobile, and receive a mail when an appointment is about to happened, and you can buy an expensive mobile phone having it's own calendar which you then can sync with the computer from time to time. But for me, I just want to say "at that time, I need to be there, and wherever I am 30 minutes before that, notify me". That's why I love google calendar, because you can be anywhere and still get a notification!

Now, that's all glorious and so, but as always there are some problems - I want to be able to sync it with other calendars, so that I don't need to open up a browser, enter the adress, and log in every time I need to add an event. For Mozilla Thunderbird there is an add on to support calendars called Lighting, and for this add on there is another one called Provider for Google Calendar, which makes it possible to view and update the google calendar from the calendar within Thunderbird. Niiice!

But hey, wait a minute, the reason I wanted to support google calendar in the first place was because of it's sms service, but as it turns out, the google calendar provider for thunderbird doesn't support this :( So in the end I still need to access google calendar from within a web-browser, and change the settings of all my events manually.

So that's where I'm now, still looking for the perfect solution. Google calendar is still the best option for me, but if you know about a better one then please let me know...

Monday, June 2, 2008

Hacking VS C# 2008 Express

VS C# 2008 Express is a really nice product in many ways - you've got most of the stuff you need, but most of all, it's free!

Though, I was playing around for it a bit, and needed to support compiling against a specific platform (x86 instead of Any CPU which is default). The reason for this was that, in my project, I was refering to a dll written in managed c++ - compiled for x86! Mixing x64 and x86 is never a good solution, and as C# projects per default compiles against Any CPU they will run in 64-bits mode on a 64-bit computer. When the application tries to load the 32-bit dll, the application crashes...

Now, in VS C# 2008 Express, you cannot specify another target platform but the Any CPU platform. Guess they think that "if you need support for specific platforms, you should be able to pay for a commercial version".

I was reading through MSDN, and found this note:
Note /platform is not available in the development environment in Visual C# Express.

"Damn", I thought, "this is the end. But what does development environment mean?". I had to try it out manually using the command prompt, so I compiled the project in VS and copied the command line output from the output window, being something like:
C:\WINDOWS\Microsoft.NET\Framework\v3.5\Csc.exe /noconfig /nowarn:1701,1702 ...
I put the line in a batch file, and added the /target:x86 flag, compiled it and... it worked! The project got compiled using x86 as a target platform.

As it turns out, VS C# 2008 Express does support the /target option, and probably others as well. They only lack the support for the option in the IDE, which is kinda a hacky way of disabling a feature, IMO.

Now, I don't want to build the project using a batch file every time, I would like to build it inside VS IDE so that others can easily build it. As the VS project files are nothing but XML, you can easily open them up in any editor you like to see their contents. I did this to find out if there was some way to add a target flag to the compiler, but unfortunately I couldn't find anything interesting (of course that would be too easy!). I continued with searching the web, and found this strange property:
CSharpProjectConfigurationProperties3.PlatformTarget Property
This member provides internal-only access to C# project configuration properties.
...
External components can access these properties through the Properties collection for the appropriate Visual Studio automation object.
"Well", I said to my self, "the Properties collection would probably refer to the XML properties found in the project file, so once again I opened up the project file, and browsed to two property groups being:
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
and
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
In these groups, I added the PlatformTarget property:
<PlatformTarget>x86</PlatformTarget>
I saved the file, reloaded it in VS, recompiled the project... and viola, it worked! All you needed to do was to find out the magic property to add to the project file! :)

This proves that C# Express contains more under the hood than what's visible to the eye. With a simple editor, and some knowledge on how to search MSDN, you can get a lot out of this product!

Friday, May 23, 2008

Lazy FizzBuzz in Haskell


Christian has been writing too many FizzBuzzes [1,2,3] right now, without me responding! We also got a comment in the first fizzbuzz post from sganslandt I've actually had the code for this post for two weeks now, but haven't had the time to finish it.

The post is based on lazy evaluation, but the neat thing is that is the default behavior in Haskell, in contrast too most other languages. Among other things, lazy evaluation allows you to reason in another way when construcing you software.

Having lazy evaluation in a language makes it "kind of important" to have control over the side-effects, so lazy evaluation and pure functions live in symbiosis in Haskell.

Let's start out with a regular divides..
> divides m n = m `mod` n == 0
We then construct a function that takes a divisor, a string, a number, and returns a string. Note that the number is the last argument.
> genWord :: Int -> String -> Int -> String
> genWord x str i
> | i `divides` x = str
> | otherwise = ""
Now the fun begins! We can generate infinite lists with "fizzes" and "buzzes", just by mapping genWord over an infinite list.
> fizzes = map (genWord 3 "Fizz") [1..]
> buzzes = map (genWord 5 "Buzz") [1..]
> nums = [1..]
So, for example, the first ten elements of fizzes looks like this:
["","","Fizz","","","Fizz","","","Fizz",""]
Now, having three infinite lists, we need to combine those to one infinite list. Is this hard to digest? I guess it's not the "normal routine" that one follows when constructing imperative programs. So, how could this in any way be useful outside the esoteric world of academia? Let us talk a bit more on that after all code, focusing on one thing at a time. Here's combine:
> combine :: String -> String -> Int -> String
> combine str1 str2 n
> | null ret = show n
> | otherwise = ret
> where ret = str1 ++ str2
In other words, we concatenate the "fizz" string with the "buzz" string and if the concatenated string is empty, we return a number instead (as a string). This makes the case of "n%15" unnecessary, which is nice. The idea for creating combine is that if we know how to combine one element, we are much closer to a solution for our problem. Though, we do restrict ourselves when defining the type for combine. The only requirement for "n" is that it is showable, but the example is just much more clearer when not using parametrized types..

A problem left to solve is the problem of having "a bunch of lists" and returning one list. The function zip does exactly that, and zip3 is function that takes three lists and returns a list of triples (source here).

From ghc's Prelude documentation:

zip3 :: [a] -> [b] -> [c] -> [(a, b, c)]
zip3 takes three lists and returns a list of triples, analogous to zip.

Now, we could rewrite combine to take a triple instead of three arguments. Currying is perhaps not well-known concept, but essentially it is another way of applying arguments to a function:

f(x,y) // Normal style (un-curried)
f x y -- Curried

So, we define a function, uncurry3, which takes a function (with three arguments) as the first argument, and returns a function which takes one argument, a triple. A funny thing is that the type declaration is longer than the implementation:
> uncurry3 :: (a -> b -> c -> d) -> ((a, b, c) -> d)
> uncurry3 f (a,b,c) = f a b c
Here's the "grande finale"! We apply fizzes, buzzes and nums to zip3, giving an infinite list of triples. We then map the uncurried function (of the "third order") on that infinite list, giving an infinite list of fizzbuzzes with numbers in between.
> resultInf :: [String]
> resultInf = map (uncurry3 combine) (zip3 fizzes buzzes nums)
Finally, we take the first 100 elements of the infinite list, and print it to stdout.
> result100 :: [String]
> result100 = take 100 resultInf
> printResult :: IO ()
> printResult = mapM_ putStrLn result100

The complete program is 15 lines of code, exclusive type type declarations, and I argue that the code is easy to read and understand. So, let us talk about why we want to create an esoteric and academic example like this, but first a quick recap..

divides is the mathematical operator '|'
genWord is a rule for generating words
fizzes, buzzes and nums generates sets of values
combine is a rule for combining three elements into one
uncurry3 converts a curried function to function of triples
result100 and printResult deals with presenting the result

Do you see the trace of math here? We treat the lists as sets and define some transformations which makes it easier to reason about the problem, in the spirit of Poyla. We decompose the problem into smaller sub-problems, which makes it much more easy to reason about the program. Separation of Concerns (SoC) is an OO principle that advocates exactly this.

When I think about it, OO has a lot of nice principles which improves the quality of the software, but I argue that OO is extremely hard to get right! Principles such as LawOfDemeter, SoC, MinimalInterface and the Liskov substitution principle, are just a few principles which (often) make good OO software, but are hard to get right all at once. Further, I'm not convinced that these principles are discussed among developers in their daily work, if the developers don't have a "pattern background" or in other ways are interested in OO design. Time pressure is probably the killer here. What is discussed on your (developer) meetings at work?

Finally, if you haven't seen it, watch Brians Beckman's "Don't fear the monads" on Channel9.

Note: as before, this post is written in Literate Haskell, which means that you can save the entire content in this post, paste it into a file and load that file with ghci. Try it!

Update: You might want to read Don Stewart's post (called "Haskell is a strict language") on the optimizations that's going on in ghc. Very nice!

Thursday, May 22, 2008

FizzBuzz with F#

Once again, I'm going to give a solution to the fizz buzz problem, but this time in a, for me, completely new language - F#!

I've been following the articles on Why I Love F# by Dustin Campell, but even though I was inspired by the language, I never got to download F# and try it... until this very day! I installed it an hour ago, experimented some, and just have to share my experience. As a first mission I tried to write a solution for the fizz buzz problem, and (with some basic knowledge from haskell programming) it didn't take me too long to find a solution. Here it is:

let fizzBuzz (x:int) =
match x with
| x when x % 15 = 0 -> "FizzBuzz"
| x when x % 3 = 0 -> "Fizz"
| x when x % 5 = 0 -> "Buzz"
| x -> x.ToString()

let main = System.Console.WriteLine(String.concat "\n" (List.map fizzBuzz [1..100]))

More or less the same solution as the one given by bugrit in the last post. The fizzBuzz function takes an integer and returns the fizzbuzz string representation of that number, using pattern matching. Unfortunate, you have to specify the type of the argument because F# can't infer the type when using x.ToString() (because ToString() is a member function of object, which can be of any type).

The main function uses String.concat to concatenate the strings returned by fizzBuzz. In F#, all basic functions are put in libraries, which as prefixed with their name, such as String and List. They resemble namespaces in C# a lot, and by using the open keyword you can use all functions in a given library globally, without the prefix.

There are a lots of more stuff to find out about the language, for example how to integrate it with your current C# project. I'll blog about it if I have time to try it out!

Monday, May 12, 2008

What is your favorite language?

We all have our favorite language, but for most people it's almost religious to discuss which language is the best. I realized I don't know what language people really prefer, simply because I've never discussed it!

Having a look at TIOBE Programming Community Index for May 2008 got me thinking. You will see Java in top, followed by C and then (Visual) Basic. I couldn't get this, until I read the following:
Observe that the TIOBE index is not about the best programming language or the language in which most lines of code have been written.

Of course, Java produces a lots of lines of code, and C has been around for a long long time. Visual Basic I can't say why is rated third, but I guess every newbie programmer develops in VB, and also, you have all those macros! I guess those makes up for a few lines?! :)

C# is only at 8'th place, but I hope that's because you don't need to write as much code in it as you do with other languages... which probably isn't true because you DO write a lots of code in it. With C# 3.0 you can use synthetic sugar to write less code, but not that much ;) I really think the reason C# is not being that highly ranked is because it hasn't been around for that long. People are still experimenting with it, and those companies that has been around for a while can't see the reason for replacing Java with C#. But, in the future, when everyone is jumping off Java, C# will grow in popularity!

Anyway, I added a vote to this blog (in the left sidebar), so go ahead and vote on your favorite language. And, if you want, post a comment on this post, arguing for why you think your language is the best!

Saturday, May 10, 2008

C# Lambda Lazy List FizzBuzz :)

Ok, so here is my next example of an implementation of the FizzBuzz problem. I'm going to use a combination of C# 3.0's lambda expressions, lazy lists and extension methods!

First, I create a function returning a lazy list over all numbers starting from 1:
static IEnumerable<int> AllNumbers()
{
int i = 1;
while (true) yield return i++;
}
Second, I create a function returning a lazy list of strings, being pText every pCount time. I.e. null, null, Fizz, null, null, Fizz.
static IEnumerable<string> FizzBuzz(int pCount, string pText)
{
while (true)
{
for (int i = 0; i < pCount-1; i++)
{
yield return null;
}
yield return pText;
}
}

Now, I need a way of combining the values two lists, so I create an extension method for this. It takes another list and a combinator function:
static IEnumerable<T> Combine<T, TA, TB>(this IEnumerable<TA> pA, IEnumerable<TB> pB, Func<TA, TB, T> pCombineFunc)
{
var i = pA.GetEnumerator();
var j = pB.GetEnumerator();
while(i.MoveNext() && j.MoveNext())
{
yield return pCombineFunc(i.Current, j.Current);
}
}

And here is the whole FizzBuzz program, returning a list of strings being 1, 2, Fizz, 4, Buzz...:
static IEnumerable<string> FizzBuzz()
{
return AllNumbers().Combine(
FizzBuzz(3, "Fizz").Combine(FizzBuzz(5, "Buzz"), (x, y) => x + y),
(x, y) => (y != "") ? y : x.ToString());
}

To write the result to the console, I use this main function:
static void Main(string[] args)
{
foreach (string fizzBuzz in FizzBuzz().Take(100))
{
Console.WriteLine(fizzBuzz);
}
}

Thats it! Nice and easy eh? :) Hope you liked it and perhaps makes you look into C# 3.0, if you haven't already? You can always download Visual Studio 2008 C# Express for free if you want to try it out!

Edit: Changed generic parameters <T> to html-characters. Changed C# 3.5 to 3.0 (thank u Gustaf :))