Greg Beech's Website

September 2008 - Greg Beech's Tech Blog

  • Function currying in C#

    C# does not automatically curry functions, so a function that multiplies two double s together and returns the result must be assigned to a delegate that takes two arguments and returns a result: Func < double , double , double > multiply = (a, b) => a * b; This seems reasonable, but unfortunately it means that if we want to define a partial application of the function where one argument is...
  • Images wrong or missing in blog posts

    Recently I've been using more images to illustrate blog posts, and unfortunately have run into an issue with Windows Live Writer and Community Server 2008 . The full details are in that forum, including a workaround and a fix, but essentially if the image is pasted from the clipboard without saving it to disk first Writer generates file names that overlap, and Community Server uses the same folder...
    Filed under:
  • Determining the bias of a shuffle algorithm

    Last time I wrote Shuffle and TakeRandom extension methods for IEnumerable<T> which use the Fisher-Yates algorithm to perform an unbiased shuffle of a sequence and take a random subset from it. In order to optimise the performance of the TakeRandom method I had to reverse the documented implementation of the shuffle so it produced the results at the start of the sequence rather than the end,...
  • Shuffle and TakeRandom extension methods for IEnumerable<T>

    It's a fairly common practice on web sites to display sets of randomly ordered items, whether it's a selection of your friends, items related to the one you're looking at, or even something as simple as a tag cloud. Sometimes the entire set of items is randomised, and sometimes you want to take a random selection from the set and display that (the latter is typically done so you can cache...
  • Optimised IP address to country code mapping

    One of the things that web sites tend to do frequently is look up the country a user is from based on their IP address, for example to direct them to the appropriate site for their country or to restrict content that cannot be legally shown to them. There are numerous solutions to perform this mapping such as calling third-party web services or looking up the country from a database, but as it is such...
Copyright (C) Greg Beech. All rights reserved.
Powered by Community Server (Non-Commercial Edition), by Telligent Systems