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: 5 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: 5 years ago by Kit Eason

  • Between operators

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

    8 people like this

    Posted: 5 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.

    1 people like this

    Posted: 5 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.

    0 people like this

    Posted: 5 years ago by Kit Eason

  • Curve difference by sum-of-squares

    An operator to compare two curves by the sum-of-squares-of-differences method.

    2 people like this

    Posted: 5 years ago by Kit Eason

  • Transform a sequence into a sequence-of-sequences

    Take a sequence and make it into a sequence of sequences, where the inner sequences are of a specified length. (The last inner sequence may be shorter.) Useful, for instance, for rending sequences into two-way HTML grids.

    1 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.

    15 people like this

    Posted: 5 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.)

    2 people like this

    Posted: 5 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: 5 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: 4 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: 4 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.

    2 people like this

    Posted: 4 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: 4 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: 4 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: 4 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.

    2 people like this

    Posted: 4 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: 3 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: 3 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/

    3 people like this

    Posted: 3 years ago by Kit Eason

  • Hexdump (2)

    A different, sketchy attempt at hexdump.

    3 people like this

    Posted: 3 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!)

    2 people like this

    Posted: 2 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: 1 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 months ago by Kit Eason