5条回答
  •  猫巷女王i
    2020-12-19 04:20

    The following is an impure implementation but yields immutable sequences lazily:

    let unflatten f s = seq {
        let buffer = ResizeArray()
    
        let flush() = seq { 
            if buffer.Count > 0 then 
                yield Seq.readonly (buffer.ToArray())
                buffer.Clear() }
    
        for item in s do
            if f item then yield! flush()
            buffer.Add(item)
    
        yield! flush() }
    

    f is the function used to test whether an element should be a split point:

    [1;2;3;4;1;5;6;7;1;9] |> unflatten (fun item -> item = 1)
    

提交回复
热议问题