Home
Insert
Update snippet 'Take every Nth element of sequence'
Title
Description
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.
Source code
module Simple = // [snippet:Composing existing functions] let everyNth n seq = seq |> Seq.mapi (fun i el -> el, i) // Add index to element |> Seq.filter (fun (el, i) -> i % n = n - 1) // Take every nth element |> Seq.map fst // Drop index from the result // [/snippet] module Efficient = // [snippet:Efficient version using enumerator] let everyNth n (input:seq<_>) = seq { use en = input.GetEnumerator() // Call MoveNext at most 'n' times (or return false earlier) let rec nextN n = if n = 0 then true else en.MoveNext() && (nextN (n - 1)) // While we can move n elements forward... while nextN n do // Retrun each nth element yield en.Current } // [/snippet]
Tags
sequence
seq
sequence
seq
Author
Link
Reference NuGet packages
If your snippet has external dependencies, enter the names of NuGet packages to reference, separated by a comma (
#r
directives are not required).
Update