Snippets tagged Seq
Unfolding Sequences
Show's using the unfold function to create a sequence that terminates once some limit is passed.
25 people like this
Posted: 1 years ago by Robert PickeringUnfolding Sequences
Demonstrates how to use unfold to create an infinite list of the fibonacci numbers
39 people like this
Posted: 1 years ago by Robert PickeringHaskell function : iterate
Implements iterate function from Haskell's Prelude. The function generates an infinite sequence by applying a function to the initial value (first) and then to the result of previous application.
102 people like this
Posted: 1 years ago by Nick PalladinosCartesian Product of Sequences
Computes the Cartesian product of a sequence of sequences. See corresponding example for a list of lists.
31 people like this
Posted: 1 years ago by Neil CarrierBreak sequence into n-element subsequences
I'm working on parallel computations and I thought it would be useful to break work into chunks, especially when processing each element asynchronously is too expensive. The neat thing is that this function is general even though motivation for it is specific. Another neat thing is that this is true lazy sequence unlike what you'd get if you used Seq.groupBy. There are three versions for your enjoyment.
38 people like this
Posted: 1 years ago by Dmitri PavlenkovFibonacci sequence
Cached fib sequence
10 people like this
Posted: 1 years ago by Dmitri PavlenkovAsynchronous sequences
An asynchronous sequence is similar to the seq<T> type, but the elements of the sequence are generated asynchronously without blocking the caller as in Async<T>. This snippet declares asynchronous sequence and uses it to compare two files in 1k blocks.
36 people like this
Posted: 1 years ago by Tomas PetricekSeq.tryTake
A more tolerant and open-minded take.
13 people like this
Posted: 1 years ago by Dan FinchFunction to generate circular infinite sequence from a list
Function to generate circular infinite sequence from a list
12 people like this
Posted: 1 years ago by Ankur DhamaTake every Nth element of sequence
A function that takes every Nth element of a sequence where N is passed as an argument. The snippet shows a naive function and a function using IEnumerator directly to provide an efficient implementation.
19 people like this
Posted: 1 years ago by Tomas PetricekPartition a sequence until a predicate is satiated
This function is given a partition predicate and a sequence. Until the predicate returns false, a list will be filled with elements. When it is, both the list and the remainder of the sequence will be returned. Note that this example preserves the laziness of the unchecked sequence elements.
38 people like this
Posted: 1 years ago by Rick MinerichSeq.unsort
Randomizes order of specified sequence
21 people like this
Posted: 1 years ago by Phillip TrelfordAbstract Console.ReadLine as an infinite sequence
Abstracts console input as an infinite sequence of strings
7 people like this
Posted: 1 years ago by fholmEnumerator computation builder
The snippet defines computation builder for working with IEnumerator. The bind operation (let!) reads next element from the enumerator, so the computation can be used for expressing things that cannot be written just using seq.
25 people like this
Posted: 1 years ago by Tomas PetricekRestartable File.ReadLines
.Net 4.0 added File.ReadLines to view a file as a sequence, but the sequence can only be read once. A simple wrapper with seq{yield!} fixes that.
32 people like this
Posted: 1 years ago by Tony Lee
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:
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 MinerichSequence Random Permutation
A generic function that randomly permutes the elements of a sequence.
29 people like this
Posted: 1 years ago by Taha HachanaSeq.partition
A partition function on sequences, like List.partition and Array.partition, but yields elements in either partition on demand. Suitable as an extension to Seq.
2 people like this
Posted: 1 years ago by Stephen SwensenSeq.triplewise
Triple version of Seq.pairwise.
1 people like this
Posted: 12 months ago by ptanSequence Generator from Async
Generates a sequence using a sequence generator and Async.StartWithContinuations. This is an attempt at modeling the OWIN delegate structure in F#
4 people like this
Posted: 11 months ago by Ryan RileyA 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 MinerichSeq.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 PetricekTake value from a sequence only when it changes
Take value from a sequence only when it changes (based on a predicate). Ex: Seq [1;1;1;3;3;3;5;5;5] will result in [3;5]
0 people like this
Posted: 9 months ago by Ankur DhamaEuler #5
Euler #5 solution
3 people like this
Posted: 8 months ago by Michael FalangaYet another command-line parser
The snippet shows a parser for command-line arguments supporting value lists for single commands. Calling with the following arguments: "Arg 1" "Arg 2" -test "Case 1" "Case 2" -show -skip "tag" produces the following map: map [("", seq ["Arg 1"; "Arg 2"]); ("show", seq []); ("skip", seq ["tag"]);("test", seq ["Case 1"; "Case 2"])] which can be used to find what data have been sent along with different commands. Calling with the following: "Arg 1" "Arg 2" /test="Case 1" "Case 2" --show /skip:tag produces the same result.
2 people like this
Posted: 7 months ago by Gennady LoskutovSeq.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 MeineckeRandom Subset
A function that takes a random subset from a seq<'T>.
0 people like this
Posted: 5 months ago by Taha Hachanan-ary Seq.map
A pattern for creating n-ary Seq.map functions.
2 people like this
Posted: 4 months ago by Nick Palladinosn-ary Seq.map (Numerals)
A pattern for creating n-ary Seq.map functions, based on numerals.
2 people like this
Posted: 4 months ago by Nick Palladinos