1 people like it.

Seq strided chunks

Overlapping chunks from a sequence - a mix of 'windowed' and 'chunkBySize'. Useful for chopping up sequences for a variety of time-domain analysis tasks (re-write to remove deprecated F# syntax)

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
module Seq =
    let stridedChunks stride chunkSize (source : seq<_> ) =
        if stride <= 0 then failwith "stride should be a positive integer"
        if chunkSize <= 0 then failwith "chunkSize should be a positive integer"
        if stride > chunkSize then failwith "stride > chunkSize not supported"
        seq {
                use e = source.GetEnumerator()
                let buffer = Array.zeroCreate chunkSize
                let go = ref true
                let i = ref 0
                let fillBuffer() =
                    while i.Value < chunkSize && e.MoveNext() do
                        buffer.[i.Value] <- e.Current
                        i.Value <- i.Value + 1
                let copyAndSlideBack() =
                    let res = Array.copy buffer
                    System.Array.Copy(res,stride,buffer,0,chunkSize-stride)
                    res
                while go.Value do
                    fillBuffer()
                    let res = copyAndSlideBack()
                    go.Value <- i.Value = chunkSize
                    if go.Value then
                        i.Value <- chunkSize-stride
                        yield res
                    else
                        let m = res.[0..i.Value-1]
                        if m.Length > 0 then
                            yield m
                    }

    (*
    let t = [0;1;2;3;4;5;6]
    stridedChunks 2 4 t  // val it : seq<int []> = seq [[|0; 1; 2; 3|]; [|2; 3; 4; 5|]; [|4; 5; 6|]]
    stridedChunks 1 3 t |> Seq.toArray // val it : int [] [] = [|[|0; 1; 2|]; [|1; 2; 3|]; [|2; 3; 4|]; [|3; 4; 5|]; [|4; 5; 6|]; [|5; 6|]|]
    stridedChunks 1 1 t |> Seq.toArray //val it : int [] [] = [|[|0|]; [|1|]; [|2|]; [|3|]; [|4|]; [|5|]; [|6|]|]
    *)
module Seq

from Microsoft.FSharp.Collections
val stridedChunks : stride:int -> chunkSize:int -> source:seq<'a> -> seq<'a []>
val stride : int
val chunkSize : int
val source : seq<'a>
Multiple items
val seq : sequence:seq<'T> -> seq<'T>

--------------------
type seq<'T> = System.Collections.Generic.IEnumerable<'T>
val failwith : message:string -> 'T
val e : System.Collections.Generic.IEnumerator<'a>
System.Collections.Generic.IEnumerable.GetEnumerator() : System.Collections.Generic.IEnumerator<'a>
val buffer : 'a []
module Array

from Microsoft.FSharp.Collections
val zeroCreate : count:int -> 'T []
val go : bool ref
Multiple items
val ref : value:'T -> 'T ref

--------------------
type 'T ref = Ref<'T>
val i : int ref
val fillBuffer : (unit -> unit)
property Ref.Value: int with get, set
System.Collections.IEnumerator.MoveNext() : bool
property System.Collections.Generic.IEnumerator.Current: 'a with get
val copyAndSlideBack : (unit -> 'a [])
val res : 'a []
val copy : array:'T [] -> 'T []
namespace System
type Array =
  member Clone : unit -> obj
  member CopyTo : array:Array * index:int -> unit + 1 overload
  member GetEnumerator : unit -> IEnumerator
  member GetLength : dimension:int -> int
  member GetLongLength : dimension:int -> int64
  member GetLowerBound : dimension:int -> int
  member GetUpperBound : dimension:int -> int
  member GetValue : [<ParamArray>] indices:int[] -> obj + 7 overloads
  member Initialize : unit -> unit
  member IsFixedSize : bool
  ...
System.Array.Copy(sourceArray: System.Array, destinationArray: System.Array, length: int64) : unit
System.Array.Copy(sourceArray: System.Array, destinationArray: System.Array, length: int) : unit
System.Array.Copy(sourceArray: System.Array, sourceIndex: int64, destinationArray: System.Array, destinationIndex: int64, length: int64) : unit
System.Array.Copy(sourceArray: System.Array, sourceIndex: int, destinationArray: System.Array, destinationIndex: int, length: int) : unit
property Ref.Value: bool with get, set
val m : 'a []
property System.Array.Length: int with get

More information

Link:http://fssnip.net/7Wh
Posted:9 months ago
Author:Faisal Waris
Tags: seq