Snippets tagged sequences

  • Farey Sequence

    Return a sequence that contains the numerators and denominators as tuples for Farey Sequence n.

    2 people like this

    Posted: 6 years ago by Graham Spiers

  • Typed access to IEnumerable

    This snippet provides a way to gain typed access to System.Collection.IEnumerable values (such as instances of System.Text.RegularExpression.MatchCollection), as long as they support a Count property and an Item accessor.

    12 people like this

    Posted: 6 years ago by kvb

  • A failed attempt at evaluating sequence items in terms of a try-with block

    A broken code example demonstrating how it's you can't catch a single throwing enumeration and continue with F#'s IEnumerable.

    2 people like this

    Posted: 6 years ago by Rick Minerich

  • Very Fast Permutations

    I spent a lot of time this week profiling different permutation functions from various places on the internet. The following was by far the fastest:

    11 people like this

    Posted: 6 years ago by Rick Minerich

  • Moving Average

    Given a period and a sequence of values, calculates the moving average of the input. E.g., given a period of 4 and a sequence of { 2.0, 8.0, 12.0, 10.0, 6.0, 4.0, 14.0 }, the result of this function is { 8.0, 9.0, 8.0, 8.5 }.

    8 people like this

    Posted: 6 years ago by Brett V. Forsgren

  • All subsets of a set

    A function implemented using sequence expressions that returns all subsets of a specified set. The function is not optimized, but it is very easy to understand.

    10 people like this

    Posted: 6 years ago by Tomas Petricek

  • Soundex Algorithm

    Algorithms for generating US Census and Daitch-Mokotoff soundex string(s) based on a text input. Soundex is a phonetic algorithm for indexing names by sound, as pronounced in English. The goal is for homophones to be encoded to the same representation so that they can be matched despite minor differences in spelling.

    4 people like this

    Posted: 6 years ago by Matt Wilson

  • 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

  • Calculating when the 1000th XKCD will appear

    Calculate's when a the nth XKCD will appear, starting from XKCD 946. For a full explanation this snippet see: http://strangelights.com/blog/archive/2011/09/02/calculating-when-the-1000th-xkcd-will-appear.aspx

    3 people like this

    Posted: 6 years ago by Robert Pickering

  • Seq.groupAfter function

    This snippet is basically the same as http://fssnip.net/6A, except that the element whose predicate holds ends the previous group and the element after it starts a new one. Those two snippets (Seq.groupWhen, Seq.groupAfter) would be generally equivalent to the Haskell functions 'breakBefore' and 'breakAfter' from Data.List.Grouping.

    0 people like this

    Posted: 5 years ago by Thorsten Meinecke

  • 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

  • Sierpinski triangle, WPF

    Draws a Sierpinski triangle using WPF

    12 people like this

    Posted: 5 years ago by Mathias Brandewinder

  • 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

  • Map for ObservableCollection

    The snippet implements 'map' and 'init' functions for the 'ObservableCollection' type. These are similar to well-known functions for sequences, but 'map' works in an incremental fashion. When the source collection changes, the resulting collection is updated.

    2 people like this

    Posted: 5 years ago by Tomas Petricek

  • 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: 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

  • Parsing a trace file via a small cmd utility

    Given an input stream of "int|int" pairs (one such pair per line), output only those lines for which the right integer has at least k distinct integers on the left side, on the entire input stream. The number k is given as a command line argument.

    5 people like this

    Posted: 4 years ago by Aggelos Biboudis

  • Even more Fast hex dump

    A simple function that nicely formats arrays of bytes. Useful in FSI or logging. byte array -> string seq Inspired by http://fssnip.net/hq, but optimized to become O(n).

    3 people like this

    Posted: 4 years ago by Vasily Kirichenko

  • mapi with 64bit integers

    A version of mapi for very long sequences that uses long integers instead of regular integers. I didn't use a mutable variable because of this http://stackoverflow.com/questions/1480582/the-mutable-variable-i-is-used-in-an-invalid-way

    4 people like this

    Posted: 4 years ago by Samuel Bosch

  • Convert list of sequences to sequence of lists

    Function to combine the values of a list of sequences into a sequence of lists where each list contains the nth element of the different sequences. It works like zip but for an arbitrary number of sequences and it returns lists instead of tuples. As with zip when one sequence is exhausted any remaining elements in the other sequences are ignored.

    4 people like this

    Posted: 3 years ago by Samuel Bosch

  • Generating numerical ranges

    Different ways of generating numerical ranges in F#. It turns out that the built-in syntax generates fairly slow code, so the snippet shows two alternative ways that are faster. Any compiler optimizations making the built-in one faster would be nice :-)

    6 people like this

    Posted: 3 years ago by Tomas Petricek

  • Choose while function

    A function that is like 'Seq.choose' but stops producing values as soon as the first 'None' value is produced.

    3 people like this

    Posted: 1 years ago by Tomas Petricek

  • Enumerating function

    This function takes an input sequence and returns a function that returns the elements in the sequence one by one when called consecutively. The returned function will throw if called more times than there are elements in the sequence. Useful when unit testing.

    2 people like this

    Posted: 9 months ago by Peder Sørensen