Skip to main content

Posts

Showing posts from April, 2013

C# Wish List: yield for Collections

I remembered a new C# feature I want today. If you're familiar with 'yield', it's a great keyword in C# used in iterator blocks. eg:

// Ignoring that there would be much simpler ways to do this anyway. public static IEnumerable<int> GetEvens(IEnumerable<int> numbers) { foreach (var number in numbers) { if (number % 2 == 0) { yield return number; } } }
Using yield will also result in lazy evaluation as demonstrated here, when using the method above:

static void Main(string[] args) { var list = Enumerable.Range(1, 10).ToList(); var output = GetEvens(list); list.Add(12); list.Add(22); list.Add(32); foreach (var item in output) { Console.WriteLine(item); } }
Which prints:

2 4 6 8 10 12 22 32
Which is an awesome feature. However, I often find cases where I'd like to yield the rest of a collection. For instance, in the Comb…

Different Problem, Same Effect

** Warning: Guitar post **
A couple of years ago I decided I wanted the pickups changed in my Ibanez RG350DX guitar, to save time and money I figured I'd do it myself. Swapping pickups, in general, should be pretty easy. It just requires un-soldering the existing connections, unscrewing the pickup rings, and doing the reverse with the new ones. The first time I tried this, the soldering iron didn't get hot enough, and as I was replacing 2 pickups from a 3 pickup configuration I got stuck. I relented and took that one into a local guitar shop and let them sort it.

Fast-forward to now, I bought a new electric a few months ago, and while really happy with it, thought it could benefit from the same pickup change the Ibanez got because of some subtleties in the high-end. I remembered having to take it into the shop and thought "not this time". When they eventually arrived (and that's another story entirely), I got to work on replacing them. I took some pictures of the…

Irregular Expressions

To parse the input of my hand history parser, I thought about a couple of possible options for reading the text. Given the dynamic nature of the file format, from my limited use of regular expressions up to that point, I decided they would be the best tool for the job and would also give me an opportunity to learn about them a bit. You can start as simply as just checking a string will or won’t match:

bool match = Regex.IsMatch("exampletext", ".+");
But when you have lines of text that contain multiple sections of information you will have to stray into grouped matching. Let’s say a line contains some summary information, that a player has won some amount of money at the end of a hand and as such their cards are displayed. Eg.

"Player1 won $1.90 with Ah Jh"
Grouped matching allows you to take all the useful information at once:

"(?<player>[\w\s\.]+)\swon\s\$(?<amount>\d+\.\d{2})\swith\s(?<leftcard>.{2})\s(?<rightcard&…

Reading Grouped Data

Having multiple records of information stored as plain text in files is certainly neither uncommon or difficult to read back in. Grouped lines however, was a problem I came across a while ago, with data being split across multiple lines and being separated by a line of empty text. My first shot at this worked fine:

let ProcessFile (allLines: string list) = let list = new List<List<string>>() let rec SplitFile (input: string list) = if input.Length <> 0 then list.Add(new List<string>(input.TakeWhile(fun x -> x <> ""))) let nextGroup = input.SkipWhile(fun x -> x <> "").SkipWhile(fun x -> x = "") SplitFile (Seq.toList nextGroup) SplitFile allLines |> ignore list
But I was unhappy with this solution. It doesn’t seem idiomatic to new List<List<string>> and store the data in there. So I took to StackOverflow in search of a better answer, which …

Identifying and Processing Flexible Structured Data

In writing a card game history parser, I came across an interesting problem. At first it seemed like it might be mostly straightforward, as these types of problems often do. When you consider a game like Texas Hold’em, there are several rounds of betting, referred to as streets. These streets as you expect are sequential. Preflop always leads to the flop, the flop always leads to the turn, the turn always leads to the river and if more than one player got that far, the player with the strongest hand takes the money. The problem I ran into is in the ‘if’ part of that last sentence. When a sufficient number of players during any part of the hand folds such that there is only 1 remaining, the hand is then short-circuited to the summary, which states some brief information about what happened. When you consider the possible combinations here:


Preflop -> Summary
Preflop -> Flop -> Summary
Preflop -> Flop -> Turn -> Summary
Preflop -> Flop -> Turn -> River ->…

Classes and Records

I recently started writing a text parser, a foundation stage in a larger project I’ve been planning to process large numbers of card game hands for a bit of big data experimentation in F#. I began in C#, I figured I’d just get the annoying bit out of the way here quickly in the language I was most familiar with, load the library in with the F# stuff and move on from there. After about 30 minutes of sketching out the classes for data storage, it started to feel rather clunky and weighed down. I’d already got a couple of hundred lines of code and I hadn’t even started processing anything yet, for example:

using System.Collections.Generic; namespace HHParser { /// <summary> /// A class that represents a 'street' and all of its actions. /// </summary> public class StreetActions { /// <summary> /// Initialises a new instance of the StreetActions class. /// </summary> public StreetActions() …