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: 
/// 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 intersperse c s =
    let rec tail_intersperse c s acc =
        match s with
            | [] -> acc
            | [head] ->acc @ [head]
            | head::tail -> tail_intersperse c tail (acc @ [head;c])

    tail_intersperse c s []
val intersperse : c:'a -> s:'a list -> 'a list

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 s : 'a list
val tail_intersperse : ('b -> 'b list -> 'b list -> 'b list)
val c : 'b
val s : 'b list
val acc : 'b list
val head : 'b
val tail : 'b list

More information

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