Snippets created by Kit Eason

  • Wimbledon special!

    Tennis scoring system (at the game level). Includes some pattern-matching examples including 'when' guards.

    4 people like this

    Posted: 13 years ago by Kit Eason

  • Her name is Cherry; we've just met

    Third cut of a guitar chord shape generator. Given a fretted instrument with a particular tuning (eg. 6 string guitar tuned EADGBE), the generator will produce the frettings necessary to play any specified chord. This is not done from a chord library, but algorithmically (hence should work with whacky tunings). This version doesn't fully respect the limitations of the human hand beyond specifying a maximum 'stretch' of a few frets, so some of the shapes generated would need a friend to help play them! This will be dealt with in a future version. This third version contains improved handling of differing tunings and instruments (eg. DADGAD; banjo) but still doesn't check for unplayable shapes.

    8 people like this

    Posted: 13 years ago by Kit Eason

  • Between operators

    A couple of operators for 'between, inclusive' and 'between, exclusive'.

    10 people like this

    Posted: 12 years ago by Kit Eason

  • Tidy up text using composed functions

    Some simple functions to tidy up text and quote it, so that it's suitable to go into a CSV.

    3 people like this

    Posted: 12 years ago by Kit Eason

  • Fast inverse square root

    This is an implementation of the famous 'magic number' method of rapidly calculating (inverse) square roots. (See http://en.wikipedia.org/wiki/Fast_inverse_square_root.) In practice, this version is no faster in F# than using 1./sqrt(x). I've posted it as an example of how you can get down-and-dirty with the bits in F# if you need to.

    7 people like this

    Posted: 12 years ago by Kit Eason

  • Discrete Fréchet Distance

    Compute the Discrete Fréchet Distance between two arrays (which may be of different lengths). Based on the 1994 algorithm by Thomas Eiter and Heikki Mannila. Not extensively tested, so use at your peril! (This version with some small fixes.)

    0 people like this

    Posted: 12 years ago by Kit Eason

  • Take a sample of a sequence

    Take a sample of a specified length from a sequence. The sample is guaranteed to be of the requested size (unless there are too few elements in the original sequence). Sample items will be taken at equal intervals. (This version with some simplifications and tidy-ups.)

    0 people like this

    Posted: 12 years ago by Kit Eason

  • How many lines of code does your project contain?

    A simple way to count the non-blank, non-comment lines in your code. (Doesn't use project files to identify source; just a wildcard. Doesn't support multi-line comments.)

    3 people like this

    Posted: 12 years ago by Kit Eason

  • What word is most like the word "turtle"?

    A while ago I posted a snippet to calculate the 'Discrete Fréchet Distance' between two curves. If we treat a word as a 'curve' by giving each letter an index (with similar-sounding letters having closer indices) we can compare words by the Fréchet distance between them! An alternative to edit-distance...

    3 people like this

    Posted: 12 years ago by Kit Eason

  • Skip that is safe when not enough lines

    A version of Seq.skip which doesn't throw an exception if there are fewer lines in the sequence than you have asked to skip.

    2 people like this

    Posted: 12 years ago by Kit Eason

  • Earth Similarity Index

    Calculating the Earth Similarity Index of a planet. http://phl.upr.edu/projects/earth-similarity-index-esi

    0 people like this

    Posted: 12 years ago by Kit Eason

  • Using Theories with FSUnit, XUnit and NUnit

    Here's how to use the [] and [] attributes to throw a set of data at a single test.

    3 people like this

    Posted: 12 years ago by Kit Eason

  • Generic Latin Square generator

    A type which generates a Latin Square - ie. an n x n array where no value is repeated in any one row or column. Useful in experimental design and some forms of testing. The argument is generic so you can generate a Latin Square of ints, floats, strings, dates, classes etc. (Needs some optimisation - this is a first cut!)

    0 people like this

    Posted: 12 years ago by Kit Eason

  • Break a sequence on a predicate

    Break a sequence into sub-sequences, where the break occurs at points where the specified function returns true when provided with the n'th and the n+1'th elements of the input sequence.

    0 people like this

    Posted: 11 years ago by Kit Eason

  • Detecting fraud with Benford's law

    Many datasets, particularly those which span several orders of magnitude, have a special property. About 30% of the initial digits of all the data items will be the digit '1'. This can be used to detect fraud, for instance in expenses claims, as people tend to concoct figures which don't have this property. These functions implement one possible test for matching Benford's law. (Credits in the comments.)

    2 people like this

    Posted: 11 years ago by Kit Eason

  • Type-inference friendly division and multiplication

    F# necessarily forces you to explicitly cast between int and float for operations such as division. This is necessary because implicit conversion would make type inference much harder. However having to cast all the time in your code can be a pain. These operators reduce the overhead to one or at most two characters of code.

    3 people like this

    Posted: 11 years ago by Kit Eason

  • Do a WriteAllLines but without the final line-end

    Just occasionally, it's useful to be able to do the equivalent of System.IO.File.WriteAllLines, but without the line-ending on the final line.

    1 people like this

    Posted: 11 years ago by Kit Eason

  • Great Circle Distance (II)

    Calculate the great-circle distance between two points on a sphere. (Not done to one-up Samual Bosch's version, but coincidentally inspired by his previous DBSCAN post.)

    2 people like this

    Posted: 11 years ago by Kit Eason

  • The Ramer-Douglas-Peucker path reduction algorithm

    A simple implementation of the Douglas-Peucker path reduction algorithm. Use for simplifying curves, for instance in plotting coastlines. Loosely based on this implementation: http://karthaus.nl/rdp/

    5 people like this

    Posted: 10 years ago by Kit Eason

  • Hexdump (2)

    A different, sketchy attempt at hexdump.

    3 people like this

    Posted: 10 years ago by Kit Eason

  • Pluck and TryPluck

    Functions to select the first element in an array that passes some predicate, and separately all the other array elements. (I'm not sure if this type of operation has a standard name. Please tweet me if you know!)

    8 people like this

    Posted: 9 years ago by Kit Eason

  • Calculate Flesch Reading Ease Score (FRES)

    Calculate the Flesch Reading Ease Score for a string or a file. https://en.wikipedia.org/wiki/Flesch%E2%80%93Kincaid_readability_tests

    1 people like this

    Posted: 8 years ago by Kit Eason

  • Accumulate a value by applying a function - in two different styles

    Two different approaches to the problem of starting with a value, applying a function to it n times, and accumulating a result.

    0 people like this

    Posted: 8 years ago by Kit Eason

  • A very cheeky string split operator

    If we can concatenate strings with +, what would it mean to have a / operator?

    5 people like this

    Posted: 6 years ago by Kit Eason

  • Implement concat for Maps

    Concatenate all the input maps. Where there are duplicate input keys, the last mapping is preserved.

    2 people like this

    Posted: 5 years ago by Kit Eason

  • Higher-Order Functions for Excel

    Some of the standard higher-order functions (like Seq.map, Seq.iter, Seq.filter) but implemented for Excel interop. Effortlessly iterate across ranges of cells, reading them, updating them or formatting them. NB. Type-information won't be displayed correctly on fssnip (Office not installed on server presumably), so to get this working paste the code into VS, make yourself a spreadsheet with a range called 'MyRange' and use FSI to explore.

    2 people like this

    Posted: 1 year ago by Kit Eason