MCPcopy Create free account
hub / github.com/Effect-TS/effect / flattenArray

Function flattenArray

packages/effect/src/Channel.ts:3239–3276  ·  view source on GitHub ↗
(
  self: Channel<ReadonlyArray<OutElem>, OutErr, OutDone, InElem, InErr, InDone, Env>
)

Source from the content-addressed store, hash-verified

3237 * @since 4.0.0
3238 */
3239export const flattenArray = <
3240 OutElem,
3241 OutErr,
3242 OutDone,
3243 InElem,
3244 InErr,
3245 InDone,
3246 Env
3247>(
3248 self: Channel<ReadonlyArray<OutElem>, OutErr, OutDone, InElem, InErr, InDone, Env>
3249): Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env> =>
3250 transformPull(self, (pull) => {
3251 let array: ReadonlyArray<OutElem> | undefined
3252 let index = 0
3253 const pump = Effect.suspend(function loop(): Pull.Pull<OutElem, OutErr, OutDone> {
3254 if (array === undefined) {
3255 return Effect.flatMap(pull, (array_) => {
3256 switch (array_.length) {
3257 case 0:
3258 return loop()
3259 case 1:
3260 return Effect.succeed(array_[0])
3261 default: {
3262 array = array_
3263 return Effect.succeed(array_[index++])
3264 }
3265 }
3266 })
3267 }
3268 const next = array[index++]
3269 if (index >= array.length) {
3270 array = undefined
3271 index = 0
3272 }
3273 return Effect.succeed(next)
3274 })
3275 return Effect.succeed(pump)
3276 })
3277
3278/**
3279 * Flattens a channel that emits `Take` values into a channel that emits the

Callers

nothing calls this directly

Calls 3

transformPullFunction · 0.70
loopFunction · 0.70
succeedMethod · 0.45

Tested by

no test coverage detected