Hyperlinkv0.8.0-beta.28

Channel

Channel.filterEffectconsteffect/Channel.ts:3264
<OutElem, E, R>(
  predicate: (a: OutElem) => Effect.Effect<boolean, E, R>
): <OutErr, OutDone, InElem, InErr, InDone, Env>(
  self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>
) => Channel<OutElem, OutErr | E, OutDone, InElem, InErr, InDone, Env | R>
<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env, E, R>(
  self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
  predicate: (a: OutElem) => Effect.Effect<boolean, E, R>
): Channel<OutElem, OutErr | E, OutDone, InElem, InErr, InDone, Env | R>

Filters output elements with an effectful predicate.

When to use

Use when the keep/discard decision depends on an Effect or service and predicate failures should fail the returned channel.

Details

Elements for which the predicate succeeds with true are emitted. Elements for which the predicate succeeds with false are discarded. Predicate failures fail the returned channel.

filtering
Source effect/Channel.ts:326429 lines
export const filterEffect: {
  <OutElem, E, R>(
    predicate: (a: OutElem) => Effect.Effect<boolean, E, R>
  ): <OutErr, OutDone, InElem, InErr, InDone, Env>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>
  ) => Channel<OutElem, OutErr | E, OutDone, InElem, InErr, InDone, Env | R>
  <OutElem, OutErr, OutDone, InElem, InErr, InDone, Env, E, R>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
    predicate: (a: OutElem) => Effect.Effect<boolean, E, R>
  ): Channel<OutElem, OutErr | E, OutDone, InElem, InErr, InDone, Env | R>
} = dual(2, <OutElem, OutErr, OutDone, InElem, InErr, InDone, Env, E, R>(
  self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
  predicate: (a: OutElem) => Effect.Effect<boolean, E, R>
): Channel<OutElem, OutErr | E, OutDone, InElem, InErr, InDone, Env | R> =>
  fromTransform((upstream, scope) =>
    Effect.map(
      toTransform(self)(upstream, scope),
      (pull) =>
        Effect.flatMap(pull, function loop(elem): Pull.Pull<OutElem, OutErr | E, OutDone, R> {
          return Effect.flatMap(
            predicate(elem),
            (passes) =>
              passes
                ? Effect.succeed(elem)
                : Effect.flatMap(pull, loop)
          )
        })
    )
  ))