Snippets tagged sequences

  • Palindromic dates

    Today, 11. february 2011, is a palindromic day according to the European date format (day/month/year). This snippet collects all the palindromic dates until 31 dec. 9999. They are 366, a surprisingly low number.

    13 people like this

    Posted: 6 years ago by Francesco De Vittori

  • Extensions to the Fold function

    This snippet is helpfull in the following cases: 1) After a consolidation operation using the fold function we need to know how many elements have been processed. 2) A consolidation operation needs to use the index of each of the elements processed and we don't want to use the mapi function first. 3) A combination of the above. Since the following snippet just adds a wrapper to the existing Fold function we can repeat the approach for arrays and sequences (including the plinq ones)

    29 people like this

    Posted: 6 years ago by Horacio Nuñez

  • Lazy String

    Lazy string based on seq

    4 people like this

    Posted: 5 years ago by Ankur Dhama

  • Partition a sequence into groups linearly by predicate

    Partitions a sequence into groups linearly by predicate. I use this for breaking up my lazy record parsing with sequences into entity-sized chunks which are then easily digestible. Note: Edited back from the previous edit as these were heavily profiled and yield! tends to be slow. Edit #2: Now correctly using "use" instead of "let" for sequence.GetEnumerator () (Thanks Vladimir Matveev)

    1 people like this

    Posted: 5 years ago by Rick Minerich

  • Seq.filter with accumulator

    Seq.filter with accumulator, without using mutable or ref.

    1 people like this

    Posted: 5 years ago by Ankur Dhama

  • Seq.triplewise

    Triple version of Seq.pairwise.

    2 people like this

    Posted: 5 years ago by ptan

  • Split sequences

    Split sequences based on a predicate.

    1 people like this

    Posted: 5 years ago by Ankur Dhama

  • JoinList

    A JoinList is a variation on the list type that offers a constant time append operation.

    5 people like this

    Posted: 5 years ago by Ryan Riley

  • IterateeCPS

    An iteratee that uses continuation-passing style as an optimization. There is no more discriminated union, and the signature should feel familiar to those using Async.StartWithContinuations.

    5 people like this

    Posted: 5 years ago by Ryan Riley

  • Gluing-up sequence members

    While thinking on Project Euler Problem 40 solution (http://projecteuler.net/index.php?section=problems&id=40) the following subproblem has surfaced: how to glue up string presentations of a sequence members to reach a given length of result string. The snippet gives at least 3 different implementations of such function with performance comparison; as a bonus a solution to Problem 40 is given.

    2 people like this

    Posted: 5 years ago by Gene Belitski

  • Generate a repeating infinite sequence

    A little function which generates an infinite sequence consisting of repeats of items from some shorter sequence.

    1 people like this

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

  • tryNth

    Computes the nth value of a sequence if the nth value is past the length of the sequence then None is returned else some

    3 people like this

    Posted: 4 years ago by Colin Bull

  • Simple single-server queue simulation

    Simulation and performance measurement of a single-server queue with various arrival and processing rates configurations. More comments on this can be found at http://www.clear-lines.com/blog/post/Simulating-a-simple-Queue-in-FSharp.aspx

    5 people like this

    Posted: 4 years ago by Mathias Brandewinder

  • Undertone: First line of Baa baa black sheep

    Playing with Undertone, using operators for terse tune definitions.

    1 people like this

    Posted: 4 years ago by Michael Newton

  • Sequence of all Subsets of a set

    A function that returns a sequence of subsets generated by the power set of a specified set. The function use bit patterns to generate sets. for example the power set generated by a set with 3 elements set [1;2;3;] has 2^3 sets. each set in the power set is represented by the set bits in each of the integer from 0 to (2^3) -1

    6 people like this

    Posted: 4 years ago by isaiah perumalla

  • Implement a suppression list

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

    1 people like this

    Posted: 4 years ago by Kit Eason

  • hexdump (somewhat fast)

    Simple functions to display a hex dump of a byte array using sequence expressions, with ASCII. Rewrote naive version that used string concat, based on optimizations in http://fssnip.net/ht, and cleaned up ASCII formatting.

    2 people like this

    Posted: 4 years ago by Matthew H. Traylor

  • Course 3: Exploring Titanic dataset

    F# introduction course - Getting data about Titanic passengers using CSV type provider and analyzing them using standard sequence-processing functions known from LINQ. To be used in Try F#.

    1 people like this

    Posted: 3 years ago by Tomas Petricek

  • Generating a tree from a generic type

    I needed a function to generate a tree from a c# class that had some odd semantics, but when I refactored it, I realised almost everyone must have something similar knocking around their codebase, so here's mine.

    0 people like this

    Posted: 3 years ago by Sean Newham

  • BitArray to Sequence

    Convert a BitArray to a sequence.

    1 people like this

    Posted: 3 years ago by Samuel Bosch

  • Faster .. operator for range generation

    The .. operator that is used to generate ranges in F# core is quite slow. This snippet shows an alternative implementation that is about 5 times faster.

    5 people like this

    Posted: 1 years ago by Tomas Petricek

  • Seq.groupWhen function

    The snippet declares a function that groups adjacent elements of a sequence. A new group is started when the specified predicate holds about an element. Groups are constructed eagerly (using lists).

    2 people like this

    Posted: 9 months ago by Tomas Petricek

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

    8 people like this

    Posted: 5 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: 5 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: 5 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: 5 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: 5 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: 5 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: 4 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: 4 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: 3 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: 2 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: 3 months ago by Peder Sørensen