F#: How do i split up a sequence into a sequence of sequences

后端 未结 8 1978
执念已碎
执念已碎 2021-01-02 06:12

Background:

I have a sequence of contiguous, time-stamped data. The data-sequence has gaps in it where the data is not contiguous. I want create a

8条回答
  •  悲&欢浪女
    2021-01-02 06:31

    Okay, trying again. Achieving the optimal amount of laziness turns out to be a bit difficult in F#... On the bright side, this is somewhat more functional than my last attempt, in that it doesn't use any ref cells.

    let groupBy cmp (sq:seq<_>) =
      let en = sq.GetEnumerator()
      let next() = if en.MoveNext() then Some en.Current else None
      (* this function returns a pair containing the first sequence and a lazy option indicating the first element in the next sequence (if any) *)
      let rec seqStartingWith start =
        match next() with
        | Some y when cmp start y ->
            let rest_next = lazy seqStartingWith y // delay evaluation until forced - stores the rest of this sequence and the start of the next one as a pair
            seq { yield start; yield! fst (Lazy.force rest_next) }, 
              lazy Lazy.force (snd (Lazy.force rest_next))
        | next -> seq { yield start }, lazy next
      let rec iter start =
        seq {
          match (Lazy.force start) with
          | None -> ()
          | Some start -> 
              let (first,next) = seqStartingWith start
              yield first
              yield! iter next
        }
      Seq.cache (iter (lazy next()))
    

提交回复
热议问题