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

  • Between operators

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

    9 people like this

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

  • How many words can you spell on a calculator?

    We all know about BOOBIES - but how many other dictionary words can you spell upside-down on a calculator?

    2 people like this

    Posted: 6 years ago by Kit Eason

  • Poll a file until it is quiet

    One of the problems with using FileSystemWatcher to detect new files and process them is that it tells you when the file starts being created, not when it finishes. Use this little function to poll the file until it stops being written to.

    6 people like this

    Posted: 6 years ago by Kit Eason

  • Eurovision - Some(points)

    The Eurovision final scoring system using records and some higher order functions. (Results are fictional - no-one seems to publish the full voting live online.)

    2 people like this

    Posted: 5 years ago by Kit Eason

  • Here's another fine fractal you've gotten me into

    An ASCII Mandelbrot visualisation. (Suggested by a tweet from Jon Harrop, but any bugs are mine.)

    2 people like this

    Posted: 5 years ago by Kit Eason

  • Checking for perfect squares

    An implementation of John D. Cook's algorithm for fast-finding perfect squares: http://www.johndcook.com/blog/2008/11/17/fast-way-to-test-whether-a-number-is-a-square/

    1 people like this

    Posted: 5 years ago by Kit Eason

  • Bulk copy into table with IDENTITY column

    Want to do a SqlBulkCopy into a table which has an IDENTITY column? If your DataTable has the same columns as the target (but without the ID), you can map the columns numerically, skipping the first target column.

    1 people like this

    Posted: 5 years ago by Kit Eason

  • Conway sequence

    Generate a Conway "look and say" sequence. Each sequence element is generated by reading the previous element from left to right and saying how many repeats of each item there are. Eg. 1211-> 111221 (ie. "one one, one two, two ones").

    1 people like this

    Posted: 5 years ago by Kit Eason

  • Enumerate files without permissions errors

    It's comparatively hard to enumerate files in a directory and subdirectories without getting exceptions for items you don't have permission to read. Here's a function which just ignores items you can't read and enumerates everything else. Useful for searching big network shares.

    2 people like this

    Posted: 5 years ago by Kit Eason

  • Implement a suppression list

    Take a sequence and exclude values from it based on another 'suppression list' sequence.

    1 people like this

    Posted: 5 years ago by Kit Eason

  • Count noise-lines in C# code

    This is the code I used to generate this pie chart: http://pic.twitter.com/PgPEFByg56 Enough said?

    9 people like this

    Posted: 5 years ago by Kit Eason

  • Calculate the root-mean-square (RMS) of a dataset

    Calculate the root-mean-square of a dataset. Eg. given the voltages over time of an alternating current, calculate the RMS voltage.

    3 people like this

    Posted: 4 years ago by Kit Eason

  • Unzip from multiple zip files in one pass

    If you have a directory containing multiple zip files, unzip them all in one pass. (Requires .NET 4.5)

    3 people like this

    Posted: 4 years ago by Kit Eason

  • Diff two strings

    Identify differences between two generally aligned strings.

    4 people like this

    Posted: 4 years ago by Kit Eason

  • Compute MD5 hash of a string

    Compute MD5 hash of a string

    1 people like this

    Posted: 4 years ago by Kit Eason

  • Functional backoff/retry

    Backoff/retry with injected mapping from call result to success criterion, and backoff times as a parameter. (New version with the backoff in the right place!)

    2 people like this

    Posted: 2 years ago by Kit Eason

  • Sending an email

    Here's how to send an email through SMTP. Works from FSI provided you are an authenticated user for the SMTP server. (Vandalism reverted.)

    2 people like this

    Posted: 2 years ago by Kit Eason

  • Finding matching pairs in two sequences

    Here's a function which takes a comparator function and two sequences, and returns tuples consisting of an item from each sequence, where the comparator function returns true for those two items. This is a small part of my wider project to generate guitar chord shapes. One of the requirements there is to take a list of 'wanted' notes for a chord, and a list of 'available' notes within a range of frets and to combine them into an actual set of frettings. That's what led to tryFindFirstPair and hence to findPairs.

    1 people like this

    Posted: 6 years ago by Kit Eason

  • Mixed number

    A function to calculate 'mixed numbers' - eg. 1 1/8. (With fix to reduce the fractional part to its lowest terms)

    2 people like this

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

    0 people like this

    Posted: 6 years ago by Kit Eason

  • Calculate left margins

    Take some text and work out where the left margins are, returning the most common margin settings first. Useful for processing somewhat messy text items like movie scripts. Left margins are defined by leading spaces. Tabs must be expanded before calling.

    3 people like this

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

    4 people like this

    Posted: 4 years ago by Kit Eason

  • Hexdump (2)

    A different, sketchy attempt at hexdump.

    3 people like this

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