1 people like it.

Generate a repeating infinite sequence

A little function which generates an infinite sequence consisting of repeats of items from some shorter sequence.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
module Seq = let rec cycle xs = seq { yield! xs; yield! cycle xs }

// Tests
let intList = [1..4]
let charList = ['a'..'d']
let objList = [obj (); obj (); obj (); obj ()]

Seq.cycle intList |> Seq.take 20 |> Seq.iter (printfn "%A")
Seq.cycle charList |> Seq.take 20 |> Seq.iter (printfn "%A")
Seq.cycle objList |> Seq.take 20 |> Seq.iter (printfn "%A")
module Seq

from Microsoft.FSharp.Collections
val cycle : xs:seq<'a> -> seq<'a>

Full name: Script.Seq.cycle
val xs : seq<'a>
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<_>
val intList : int list

Full name: Script.intList
val charList : char list

Full name: Script.charList
val objList : System.Object list

Full name: Script.objList
type obj = System.Object

Full name: Microsoft.FSharp.Core.obj
Multiple items
module Seq

from Script

--------------------
module Seq

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

Full name: Microsoft.FSharp.Collections.Seq.take
val iter : action:('T -> unit) -> source:seq<'T> -> unit

Full name: Microsoft.FSharp.Collections.Seq.iter
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn

More information

Link:http://fssnip.net/a5
Posted:7 months ago
Author:Sehnsucht
Tags: sequences