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.
12 people like this
Posted: 1 years ago by Francesco De VittoriFarey Sequence
Return a sequence that contains the numerators and denominators as tuples for Farey Sequence n.
1 people like this
Posted: 1 years ago by Graham SpiersExtensions 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)
22 people like this
Posted: 1 years ago by Horacio NuñezTyped 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.
6 people like this
Posted: 1 years ago by kvbLazy String
Lazy string based on seq<char>
4 people like this
Posted: 1 years ago by Ankur DhamaVery 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:
4 people like this
Posted: 1 years ago by Rick MinerichPartition 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)
0 people like this
Posted: 1 years ago by Rick MinerichSeq.filter with accumulator
Seq.filter with accumulator, without using mutable or ref.
1 people like this
Posted: 1 years ago by Ankur DhamaMoving 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 }.
7 people like this
Posted: 12 months ago by Brett V. ForsgrenSeq.triplewise
Triple version of Seq.pairwise.
1 people like this
Posted: 12 months ago by ptanAll 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.
8 people like this
Posted: 11 months ago by Tomas PetricekA 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: 1 years ago by Rick MinerichSplit sequences
Split sequences based on a predicate.
1 people like this
Posted: 10 months ago by Ankur Dhama
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).
4 people like this
Posted: 10 months ago by Tomas PetricekSoundex 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: 9 months ago by Matt WilsonJoinList
A JoinList is a variation on the list type that offers a constant time append operation.
5 people like this
Posted: 9 months ago by Ryan RileyFinding 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: 9 months ago by Kit EasonIterateeCPS
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: 9 months ago by Ryan RileyCalculating 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
2 people like this
Posted: 8 months ago by Robert PickeringGluing-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: 8 months ago by Gene BelitskiSeq.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: 6 months ago by Thorsten MeineckeGenerate 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: 3 months ago by Kit EasonTransform 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.
0 people like this
Posted: 2 months ago by Kit EasonTake 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, with possibly a shorter interval at the end if the sequence length is not evenly divisible by the sample size.
0 people like this
Posted: 2 months ago by Kit EasonSierpinski triangle, WPF
Draws a Sierpinski triangle using WPF
8 people like this
Posted: 2 months ago by Mathias BrandewindertryNth
Computes the nth value of a sequence if the nth value is past the length of the sequence then None is returned else some
2 people like this
Posted: 4 days ago by Colin Bull