1 people like it.

Intersperse an element into a sequence

This function enables the repeated insertion of a specified value into a sequence. It ensures that Seq.skip will never throw an exception by always ensuring it never skips more elements than remain in the sequence.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
    /// intersperses the value c into the elements of the sequence s after every n elements of s
    /// e.g. intersperse -1 4 (seq{1..10}) will generate {1; 2; 3; 4; -1; 5; 6; 7; 8; -1; 9; 10;}
    let rec intersperse c n s = match Seq.isEmpty s with
                                | true -> s
                                | false ->  seq {let list = Seq.truncate n s |> Seq.toList
                                                 let length = List.length list
                                                 yield! list
                                                 if length >= n then
                                                    yield c
                                                    let rest = Seq.skip length s
                                                    yield! intersperse c n rest
                                                }
val intersperse : c:'a -> n:int -> s:seq<'a> -> seq<'a>

Full name: Script.intersperse


 intersperses the value c into the elements of the sequence s after every n elements of s
 e.g. intersperse -1 4 (seq{1..10}) will generate {1; 2; 3; 4; -1; 5; 6; 7; 8; -1; 9; 10;}
val c : 'a
val n : int
val s : seq<'a>
module Seq

from Microsoft.FSharp.Collections
val isEmpty : source:seq<'T> -> bool

Full name: Microsoft.FSharp.Collections.Seq.isEmpty
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<_>
Multiple items
val list : 'a list

--------------------
type 'T list = List<'T>

Full name: Microsoft.FSharp.Collections.list<_>
val truncate : count:int -> source:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.truncate
val toList : source:seq<'T> -> 'T list

Full name: Microsoft.FSharp.Collections.Seq.toList
val length : int
Multiple items
module List

from Microsoft.FSharp.Collections

--------------------
type List<'T> =
  | ( [] )
  | ( :: ) of Head: 'T * Tail: 'T list
  interface IEnumerable
  interface IEnumerable<'T>
  member Head : 'T
  member IsEmpty : bool
  member Item : index:int -> 'T with get
  member Length : int
  member Tail : 'T list
  static member Cons : head:'T * tail:'T list -> 'T list
  static member Empty : 'T list

Full name: Microsoft.FSharp.Collections.List<_>
val length : list:'T list -> int

Full name: Microsoft.FSharp.Collections.List.length
val rest : seq<'a>
val skip : count:int -> source:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.skip
Next Version Raw view Test code New version

More information

Link:http://fssnip.net/t4
Posted:7 years ago
Author:Hugh Gleaves
Tags: intersperse skip insert periodic