Hyperlinkv0.8.0-beta.28

Channel

Channel.flattenArrayconsteffect/Channel.ts:2912
<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>(
  self: Channel<
    ReadonlyArray<OutElem>,
    OutErr,
    OutDone,
    InElem,
    InErr,
    InDone,
    Env
  >
): Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>

Flattens a channel that outputs arrays into a channel that outputs individual elements.

Example (Flattening arrays of channel output)

import { Channel, Data } from "effect"

class FlattenError extends Data.TaggedError("FlattenError")<{
  readonly message: string
}> {}

// Create a channel that outputs arrays
const arrayChannel = Channel.fromIterable([
  [1, 2, 3],
  [4, 5],
  [6, 7, 8, 9]
])

// Flatten the arrays into individual elements
const flattenedChannel = Channel.flattenArray(arrayChannel)

// Outputs: 1, 2, 3, 4, 5, 6, 7, 8, 9
transforming
Source effect/Channel.ts:291238 lines
export const flattenArray = <
  OutElem,
  OutErr,
  OutDone,
  InElem,
  InErr,
  InDone,
  Env
>(
  self: Channel<ReadonlyArray<OutElem>, OutErr, OutDone, InElem, InErr, InDone, Env>
): Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env> =>
  transformPull(self, (pull) => {
    let array: ReadonlyArray<OutElem> | undefined
    let index = 0
    const pump = Effect.suspend(function loop(): Pull.Pull<OutElem, OutErr, OutDone> {
      if (array === undefined) {
        return Effect.flatMap(pull, (array_) => {
          switch (array_.length) {
            case 0:
              return loop()
            case 1:
              return Effect.succeed(array_[0])
            default: {
              array = array_
              return Effect.succeed(array_[index++])
            }
          }
        })
      }
      const next = array[index++]
      if (index >= array.length) {
        array = undefined
        index = 0
      }
      return Effect.succeed(next)
    })
    return Effect.succeed(pump)
  })
Referenced by 12 symbols