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 < chunkSize && e.MoveNext() do
                        buffer.[!i] <- e.Current
                        i := !i + 1
                let copyAndSlideBack() =
                    let res = Array.copy buffer
                    System.Array.Copy(res,stride,buffer,0,chunkSize-stride)
                    res
                while !go do
                    fillBuffer()
                    let res = copyAndSlideBack()
                    go := !i = chunkSize
                    if !go then
                        i := chunkSize-stride
                        yield res
                    else
                        let m = res.[0..!i-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 []>

Full name: Script.Seq.stridedChunks
val stride : int
val chunkSize : int
val source : 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 failwith : message:string -> 'T

Full name: Microsoft.FSharp.Core.Operators.failwith
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 []

Full name: Microsoft.FSharp.Collections.Array.zeroCreate
val go : bool ref
Multiple items
val ref : value:'T -> 'T ref

Full name: Microsoft.FSharp.Core.Operators.ref

--------------------
type 'T ref = Ref<'T>

Full name: Microsoft.FSharp.Core.ref<_>
val i : int ref
val fillBuffer : (unit -> unit)
System.Collections.IEnumerator.MoveNext() : bool
property System.Collections.Generic.IEnumerator.Current: 'a
val copyAndSlideBack : (unit -> 'a [])
val res : 'a []
val copy : array:'T [] -> 'T []

Full name: Microsoft.FSharp.Collections.Array.copy
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
  ...

Full name: System.Array
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
val m : 'a []
property System.Array.Length: int
Next Version Raw view Test code New version

More information

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