Tennis scoring system (at the game level). Includes some pattern-matching examples including 'when' guards.
4 people like thisPosted: 13 years ago by Kit Eason
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 thisPosted: 13 years ago by Kit Eason
A couple of operators for 'between, inclusive' and 'between, exclusive'.
10 people like thisPosted: 12 years ago by Kit Eason
Some simple functions to tidy up text and quote it, so that it's suitable to go into a CSV.
3 people like thisPosted: 12 years ago by Kit Eason
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 thisPosted: 12 years ago by Kit Eason
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 thisPosted: 12 years ago by Kit Eason
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 thisPosted: 12 years ago by Kit Eason
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 thisPosted: 12 years ago by Kit Eason
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 thisPosted: 12 years ago by Kit Eason
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 thisPosted: 12 years ago by Kit Eason
Calculating the Earth Similarity Index of a planet. http://phl.upr.edu/projects/earth-similarity-index-esi
0 people like thisPosted: 12 years ago by Kit Eason
Here's how to use the [
Posted: 12 years ago by Kit Eason
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 thisPosted: 12 years ago by Kit Eason
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 thisPosted: 12 years ago by Kit Eason
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 thisPosted: 12 years ago by Kit Eason
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 thisPosted: 12 years ago by Kit Eason
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 thisPosted: 11 years ago by Kit Eason
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 thisPosted: 11 years ago by Kit Eason
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 thisPosted: 10 years ago by Kit Eason
A different, sketchy attempt at hexdump.
3 people like thisPosted: 10 years ago by Kit Eason
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 thisPosted: 9 years ago by Kit Eason
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 thisPosted: 9 years ago by Kit Eason
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 thisPosted: 8 years ago by Kit Eason
If we can concatenate strings with +, what would it mean to have a / operator?
5 people like thisPosted: 6 years ago by Kit Eason
Concatenate all the input maps. Where there are duplicate input keys, the last mapping is preserved.
2 people like thisPosted: 5 years ago by Kit Eason
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 thisPosted: 1 year ago by Kit Eason
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 thisPosted: 13 years ago by Kit Eason
A function to calculate 'mixed numbers' - eg. 1 1/8. (With fix to reduce the fractional part to its lowest terms)
2 people like thisPosted: 12 years ago by Kit Eason
Some simple functions to tidy up text and quote it, so that it's suitable to go into a CSV.
0 people like thisPosted: 12 years ago by Kit Eason
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 thisPosted: 12 years ago by Kit Eason
An operator to compare two curves by the sum-of-squares-of-differences method.
2 people like thisPosted: 12 years ago by Kit Eason
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 thisPosted: 12 years ago by Kit Eason
We all know about BOOBIES - but how many other dictionary words can you spell upside-down on a calculator?
2 people like thisPosted: 12 years ago by Kit Eason
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.
7 people like thisPosted: 12 years ago by Kit Eason
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 thisPosted: 12 years ago by Kit Eason
An ASCII Mandelbrot visualisation. (Suggested by a tweet from Jon Harrop, but any bugs are mine.)
2 people like thisPosted: 12 years ago by Kit Eason
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 thisPosted: 12 years ago by Kit Eason
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 thisPosted: 12 years ago by Kit Eason
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").
4 people like thisPosted: 12 years ago by Kit Eason
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.
4 people like thisPosted: 12 years ago by Kit Eason
Take a sequence and exclude values from it based on another 'suppression list' sequence.
1 people like thisPosted: 12 years ago by Kit Eason
This is the code I used to generate this pie chart: http://pic.twitter.com/PgPEFByg56 Enough said?
9 people like thisPosted: 11 years ago by Kit Eason
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 thisPosted: 11 years ago by Kit Eason
If you have a directory containing multiple zip files, unzip them all in one pass. (Requires .NET 4.5)
4 people like thisPosted: 11 years ago by Kit Eason
Identify differences between two generally aligned strings.
4 people like thisPosted: 10 years ago by Kit Eason
Compute MD5 hash of a string
2 people like thisPosted: 10 years ago by Kit Eason
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 thisPosted: 9 years ago by Kit Eason
Here's how to send an email through SMTP. Works from FSI provided you are an authenticated user for the SMTP server. (Vandalism reverted.)
5 people like thisPosted: 8 years ago by Kit Eason
Eliminate repetitions in a sequence, preserving the first element in each run of repeats.
2 people like thisPosted: 6 years ago by Kit Eason
The built in foldBack has parameters in an odd order: you can't pipe the array into it, and the folder signature is different from Array.fold's. Here's how to fix it.
6 people like thisPosted: 6 years ago by Kit Eason
Write colored console messages, without them getting intermingled if writing from parallel/async processes.
9 people like thisPosted: 5 years ago by Kit Eason