The Bitter Coder Tutorials, Binsor Style III: Dictonaries

Mon, Jul 14, 2008 One-minute read
DictionaryImage via Wikipedia
Time for Part III in the Binsorization of the Bitter Coder tutorials.  The original tutorial is here. The original tutorial created a class to handle word substitution, which looked a lot like:

public class AliasService

{

private Dictionary<string, string> _aliases;

public Dictionary<string, string> Aliases

{

get { return _aliases; }

set { _aliases = value; }

}

public string Evaluate(string term)

{

if (_aliases == null) return term;

while (_aliases.ContainsKey(term))

{

term = _aliases[term];

}

return term;

}

}

And the application code:

static void Main(string[] args)

{

AliasService aliasService = container.Resolve<AliasService>();

string sentence = "a dog ate my homework";

foreach (string word in sentence.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries))

{

Console.Write("{0} ", aliasService.Evaluate(word));

}

}

Now the Binsor:

import System

import System.Reflection

import System.Collections.Generic

import BitterCoder.Tutorials.Binsor.Core

aliases=Dictionary [of string, string]()

aliases['dog']="duck"

aliases['ate']="broke"

aliases['homework']="code"

component "aliases.service", AliasService:

Aliases=aliases

So, we have a bit of strange syntax to get a generic Dictionary, but then it becomes very natural to create the entries.  Also, we don't have to worry about the type converters that Alex mentions in his tutorial. Running the code gives us: a duck broke my code All good.  Up next is Switching Configurations...
Zemanta Pixie