Friday, July 22, 2011

DOS on Dope - ASP.NET MVC the old school way

Would you like to combine old school DOS skills with modern day web frameworks? Then DOS on dope might be something for you. Everything is written in DOS .bat scripts. Yes, everything. Views and controllers are all familiar .bat scripts.

I've set up a small home page with a single Index view under a Home controller as you would expect from a simple MVC application. Here's the source:
@echo off
call ..\..\Views\Shared\_header Web Page
call ..\..\h\h1 Welcome to my web page

echo [strong] This web page is under construction [/strong]

echo.
echo [blockquote]
echo [ul]
pushd %cd%
cd ..
FOR /F "delims=~" %%A IN (' DIR /B/a:d') DO (
echo [li][a href='%%A/']%%A[/a][/li]
)
echo [/ul]
echo [/blockquote]

echo [table]
echo [tr]
echo [td]
echo [img src = "Content/anders.jpeg" width = "150"][/img]
echo [/td]
echo [td width = "400" valign = "top"]
echo Hi, I am Anders, and this is my web page. Feel free to browse around and explore my life and works.
echo [br]
echo [br]
echo Be shure you head over to
echo [a href = "http://dod.codeplex.com/"]http://dod.codeplex.com/[/a]
echo where you can learn how to make your own web page
echo [/td]
echo [/tr]
echo [/table]
echo [blink][a href = "Content/index.bat.txt"]Forget Razor, check out this awesome MVC syntax[/a][blink]

::echo [blockquote][pre]"C:\Temp\Blog\Controllers\Home\Index.bat"[/pre][/blockquote]


Nice, or what?


Links:
http://external2.webtop.no/dodblog/
http://dod.codeplex.com/

Friday, July 15, 2011

Code kata challenge #2

I've been playing around with linq-like functionality as a code kata the last few days. I've simplified the my Where method and made a simple skip and take function. I can now do this without the actual linq namespace:

var results = list
.Skip(2)
.Take(200)
.Where(x => x % 2 == 0);


My Skip method:

public static IEnumerable<T> Skip<T>(
this IEnumerable<T> list, int count)
{
var enumerator = list.GetEnumerator();
for (var i = 0; i < count; i++)
enumerator.MoveNext();

while (enumerator.MoveNext())
yield return enumerator.Current;
}


My Take method:

public static IEnumerable<T> Take<T>(
this IEnumerable<T> list, int count)
{
var enumerator = list.GetEnumerator();
enumerator.MoveNext();
for (var i = 0; i < count; i++)
{
yield return enumerator.Current;
enumerator.MoveNext();
}
}


My Where method:

public static IEnumerable<T> Where<T>(
this IEnumerable<T> list, Predicate<T> expression)
{
foreach(var item in list)
if(expression(item))
yield return item;
}


It's darned fast too. Delayed excecution rocks.

Tuesday, July 12, 2011

Code kata challenge

Code kata is a term coined by Dave Thomas, author of several programming books. He suggest you do small exercises regularly in order to keep yourself fit as a programmer.

No doubt, this can be usefull, but doing a small bank account example ten times over doesn't make you fully capable of writing financial applications. In the real world you are likely to meet real world challenges.

Now, what I like are challenges that make me better at core features or work methods. I like challenges like "here's a set of tests, write the method that makes them all pass". If you're good at that, you we'll be better at working with unit tests (TDD in fact).

After watching Justin Etheredge on Linq over at Tekpub, I've got an idea for a code kata. Recreate the Linq functions like Justin does in episode 1. Once you've got the hang of one, move to the next. That should keep you busy for the next month or so and you will be the master of new language features like extension methods, collection initializers, delegates, anonymous delegates, and lambas.

Here's how you might extend IEnumerable in order to implement a WHERE method:
using System;
using System.Collections.Generic;

namespace Linq.Kata
{
public static IEnumerable<T> Where<T>(
this IEnumerable<T> list, Predicate<T> expression)
{
foreach(var item in list)
if(expression(item))
yield return item;
}
}
This method extends IEnumerable and takes a function delegate as parameter. We traverse the list and execute the delegate function on each item in the list. The method would be called like this:
var integerlist = new int[] {1, 2, 3, 4, 5, 6};
var integerResukts = integerlist.KataWhere(x => x % 2 == 0);

var stringList = new string[] {"anders", "christian"};
var stringResults = stringList.KataWhere(x => x == "anders");

Some links:

Kata Catalogue
http://codingdojo.org/cgi-bin/wiki.pl?KataCatalogue

Mastering Linq @ Tekpub
http://tekpub.com/view/linq/1