Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F# - how to group previous, this and next elements in circular Seq

Tags:

f#

In F# How to best convert a finite Sequence-like seq [0; 1; 2; 3; 4] into a Sequence of tuples like seq [4,0,1 ; 0,1,2 ; 1,2,3 ; 2,3,4 ; 3,4,0] ?

Addition: My Seq represents circular data. In this case the vertices of a closed polyline. I need the neighboring elements to compute the angle of each corner.

like image 710
Goswin Rothenthal Avatar asked Nov 28 '22 15:11

Goswin Rothenthal


1 Answers

Here's a simple solution which only uses sequences. Note that if the input and output is always going to be a list, there's a slightly more complicated but faster solution which only uses lists and traverses the input just once.

// Example usage: neighbors [0..4]
let neighbors input =
    let inputLength = Seq.length input
    input
    // The sequence needs to be looped three times;
    // the first and last time handle the previous value for the first
    // element in the input sequence and the next value for the last
    // element in the input sequence, respectively.
    |> Seq.replicate 3
    // Start at the previous element of the first element in the input sequence.
    |> Seq.skip (inputLength - 1)
    // Take the same number of elements as the tuple.
    |> Seq.windowed 3
    // Only keep the same number of elements as the original sequence.
    |> Seq.take inputLength
    // Convert the arrays to tuples
    |> Seq.map (fun values ->
        values.[0], values.[1], values.[2])
    // Return the result as a list of tuples.
    |> Seq.toList
like image 62
Jack P. Avatar answered Dec 26 '22 11:12

Jack P.