0 people like it.

Treat a sequence as a circular sequence

Create a circular (repeating) sequence from an input sequence, optionally limiting the total number of elements.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
    // Make the supplied sequence appear circular but with
    // at most 'n' elements, n = -1 elicits infinite repetition.
    let circular n s =
        let rec circularize s = 
            seq {
                yield! s
                yield! circularize s
                }
        match n with
        | -1 -> circularize s
        | _  -> circularize s |> Seq.truncate n
val circular : n:int -> s:seq<'a> -> seq<'a>

Full name: Script.circular
val n : int
val s : seq<'a>
val circularize : (seq<'b> -> seq<'b>)
val s : seq<'b>
Multiple items
val seq : sequence:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Core.Operators.seq

--------------------
type seq<'T> = System.Collections.Generic.IEnumerable<'T>

Full name: Microsoft.FSharp.Collections.seq<_>
module Seq

from Microsoft.FSharp.Collections
val truncate : count:int -> source:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.truncate
Raw view Test code New version

More information

Link:http://fssnip.net/t7
Posted:9 years ago
Author:Hugh Gleaves
Tags: sequence , circular , repeating , infinite