## Snippets tagged seq

• ### Unfolding Sequences

Show's using the unfold function to create a sequence that terminates once some limit is passed.

39 people like this

Posted: 13 years ago by Robert Pickering

• ### Cartesian Product of Sequences

Computes the Cartesian product of a sequence of sequences. See corresponding example for a list of lists.

55 people like this

Posted: 13 years ago by Neil Carrier

• ### Break 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.

73 people like this

Posted: 13 years ago by Dmitri Pavlenkov

• ### Partition 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.

69 people like this

Posted: 13 years ago by Rick Minerich

• ### Function to generate circular infinite sequence from a list

Function to generate circular infinite sequence from a list

21 people like this

Posted: 13 years ago by Ankur Dhama

• ### Seq.unsort

Randomizes order of specified sequence

23 people like this

Posted: 13 years ago by Phillip Trelford

• ### Enumerator 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.

29 people like this

Posted: 13 years ago by Tomas Petricek

• ### 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: 13 years ago by Rick Minerich

• ### Seq.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.

7 people like this

Posted: 13 years ago by Stephen Swensen

• ### Sequence 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#

6 people like this

Posted: 13 years ago by Ryan Riley

• ### Euler #5

Euler #5 solution

4 people like this

Posted: 12 years ago by Michael Falanga

• ### 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: 12 years ago by Thorsten Meinecke

• ### n-ary Seq.map

A pattern for creating n-ary Seq.map functions.

5 people like this

Posted: 12 years ago by Nick Palladinos

• ### IEnumerator

seq to IEnumerator and IEnumerable

3 people like this

Posted: 12 years ago by Graham Spiers

• ### Another successive enumeration

Applying functions on the n first elements

1 people like this

Posted: 11 years ago by Nicolas2

• ### Split A Seq Into Chunks

This function splits a sequence into lists of length n until there is less than n elements then those are returned.

12 people like this

Posted: 11 years ago by Taha Hachana

• ### Draw from sequences

Peeks from a few seq randomly according to specified frequencies. not tested

2 people like this

Posted: 11 years ago by nicolas2

• ### 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.

5 people like this

Posted: 10 years ago by Samuel Bosch

• ### Create a histogram of a sequence using map

Creates a map containing each item and its frequency as a key/value pair. Then sort by value in reverse order before printing each item.

2 people like this

Posted: 10 years ago by Bjørn Bæverfjord

• ### Fibonacci sequence with scan

Fibonacci sequence with scan

5 people like this

Posted: 8 years ago by Andriy Tolstoy

• ### Functional and simple version for Collatz Conjecture or 3n + 1 Problem

Functional and simple version for Collatz Conjecture or 3n + 1 Problem

5 people like this

Posted: 8 years ago by Fabio Galuppo

• ### 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.

3 people like this

Posted: 7 years ago by Peder Sørensen

• ### Sequence Random Permutation

A generic function that randomly permutes the elements of a sequence.

0 people like this

Posted: 6 years ago by Taha Hachana

• ### Recursor

A sugar around IEnumerator<'a> to make it nicer to use with recursive functions

5 people like this

Posted: 6 years ago by manofstick

• ### Seq.trySkip

Tries to skip the given number of items and returns the rest. Disposes the enumerator at the end.

3 people like this

Posted: 5 years ago by Matthias Dittrich

• ### list function for cross tabulation and pivot tables

Convert a list of tuples into a crosstab of key with list of values with that key

4 people like this

Posted: 4 years ago by Steve Channell

• ### Seq strided chunks

Overlapping chunks from a sequence - a mix of 'windowed' and 'chunkBySize'. Useful for chopping up sequences for a variety of time-domain analysis tasks (re-write to remove deprecated F# syntax)

1 people like this

Posted: 1 year ago by Faisal Waris

• ### Haskell 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.

203 people like this

Posted: 13 years ago by Nick Palladinos

• ### Asynchronous sequences

An asynchronous sequence is similar to the seq type, but the elements of the sequence are generated asynchronously without blocking the caller as in Async. This snippet declares asynchronous sequence and uses it to compare two files in 1k blocks.

109 people like this

Posted: 13 years ago by Tomas Petricek

• ### Fibonacci sequence

Cached fib sequence

15 people like this

Posted: 13 years ago by Dmitri Pavlenkov

• ### Seq.tryTake

A more tolerant and open-minded take.

19 people like this

Posted: 13 years ago by Dan Finch

• ### Take 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.

36 people like this

Posted: 13 years ago by Tomas Petricek

• ### Abstract Console.ReadLine as an infinite sequence

Abstracts console input as an infinite sequence of lines of text

7 people like this

Posted: 13 years ago by fholm

.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.

44 people like this

Posted: 13 years ago by Tony Lee

• ### 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: 13 years ago by Rick Minerich

• ### Seq.triplewise

Triple version of Seq.pairwise.

2 people like this

Posted: 13 years ago by ptan

• ### Take 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]

1 people like this

Posted: 13 years ago by Ankur Dhama

• ### Yet 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.

6 people like this

Posted: 12 years ago by Gennady Loskutov

• ### Random Subset

A function that takes a random subset from a seq<'T>.

1 people like this

Posted: 12 years ago by Taha Hachana

• ### n-ary Seq.map (Numerals)

A pattern for creating n-ary Seq.map functions, based on numerals.

5 people like this

Posted: 12 years ago by Nick Palladinos

• ### Partition a list

The snippet implements 'List.partitionWhile' which behaves as a combination of 'Seq.takeWhile' and 'Seq.skipWhile': It splits the list into a part containing elements from the beginning of a list that match a given predicate and remaining elements.

7 people like this

Posted: 11 years ago by Tomas Petricek

• ### Sum of Squares Monoid

This snippet defines a computation builder that sums the squares of float values. It includes Combine, Zero, Yield, Delay, and For operations.

2 people like this

Posted: 11 years ago by Joel Huang

• ### Largest palindrome made from the product of two n-digit numbers

A generalised version of the solution to the fourth Project Euler problem - Largest palindrome product, using sequences. The key to understanding this code is how "Seq.map (fun x -> (Seq.map (fun y -> x * y) baseSeq)) baseSeq" generates a sequence of sequences that contains the products of all possible combinations of two n-digit numbers. "Seq.map (fun x -> (Seq.map (fun y -> x * y) {1..3})) {1..3}" will generate: seq [seq [1; 2; 3]; seq [2; 4; 6]; seq [3; 6; 9]]

3 people like this

Posted: 11 years ago by Bjørn Bæverfjord

• ### 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: 10 years ago by Samuel Bosch

• ### BitArray to Sequence

Convert a BitArray to a sequence.

1 people like this

Posted: 10 years ago by Samuel Bosch

• ### Generate a date range sequence

Generates a sequence of dates (ascending or descending), incrementing (or decrementing) by one day at a time, inclusive of the start and end dates.

3 people like this

Posted: 9 years ago by Daniel Bradley

• ### Yet Another Fizz Buzz (YAFB)

Yet Another Fizz Buzz (YAFB)

8 people like this

Posted: 8 years ago by @Functional_S

• ### 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: 8 years ago by Tomas Petricek

• ### Generate a date range sequence (another alternative)

Generates a sequence of dates (ascending or descending), incrementing (or decrementing) by one day at a time, inclusive of the start and end dates. This is an alternative to http://www.fssnip.net/oS

5 people like this

Posted: 6 years ago by Fernando Saldanha

• ### Converting binary tree to sequence using yield

This sample show how to use seq-yield syntax to convert a binary tree to a sequence of elements.

3 people like this

Posted: 6 years ago by Dmitry Soshnikov

• ### Seq.tryTake with proper lifetime

Fixes http://www.fssnip.net/1K/title/SeqtryTake by moving the enumerator into the `seq` computation and using `use` instead of `let` to dispose the enumerator at the end.

1 people like this

Posted: 5 years ago by Matthias Dittrich

• ### 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: 4 years ago by Rick Minerich

A variation of another snippet "Seq.groupWhen". This one groups adjacent elements based on a predicate that accepts two arguments - the previous and current elements

2 people like this

Posted: 1 year ago by Faisal Waris

• ### Unfolding Sequences

Demonstrates how to use unfold to create a sequence of fibonacci numbers

5 people like this

Posted: 11 months ago by Dave Yost