Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Stream.filterMapEffectOption combinator #4265

Open
wants to merge 1 commit into
base: next-minor
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/odd-crabs-fry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": minor
---

Added Stream.filterMapEffectOption combinator for effectful filtering and mapping in a single step
16 changes: 16 additions & 0 deletions packages/effect/src/Stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1670,6 +1670,22 @@ export const filterMapEffect: {
): Stream<A2, E | E2, R | R2>
} = internal.filterMapEffect

/**
* Performs an effectful filter and map in a single step, where the provided
* function returns an `Option` wrapped in an `Effect`.
*
* @category utils
*/
export const filterMapEffectOption: {
<A, A2, E2, R2>(
f: (a: A) => Effect.Effect<Option.Option<A2>, E2, R2>
): <E, R>(self: Stream<A, E, R>) => Stream<A2, E2 | E, R2 | R>
<A, E, R, A2, E2, R2>(
self: Stream<A, E, R>,
f: (a: A) => Effect.Effect<Option.Option<A2>, E2, R2>
): Stream<A2, E | E2, R | R2>
} = internal.filterMapEffectOption

/**
* Transforms all elements of the stream for as long as the specified partial
* function is defined.
Expand Down
41 changes: 41 additions & 0 deletions packages/effect/src/internal/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2599,6 +2599,47 @@ export const filterMapEffect = dual<
})
)

/** @internal */
export const filterMapEffectOption = dual<
<A, A2, E2, R2>(
f: (a: A) => Effect.Effect<Option.Option<A2>, E2, R2>
) => <E, R>(self: Stream.Stream<A, E, R>) => Stream.Stream<A2, E2 | E, R2 | R>,
<A, E, R, A2, E2, R2>(
self: Stream.Stream<A, E, R>,
f: (a: A) => Effect.Effect<Option.Option<A2>, E2, R2>
) => Stream.Stream<A2, E2 | E, R2 | R>
>(
2,
<A, E, R, A2, E2, R2>(
self: Stream.Stream<A, E, R>,
f: (a: A) => Effect.Effect<Option.Option<A2>, E2, R2>
): Stream.Stream<A2, E | E2, R | R2> =>
suspend(() => {
const loop = (
iterator: Iterator<A>
): Channel.Channel<Chunk.Chunk<A2>, Chunk.Chunk<A>, E | E2, E, unknown, unknown, R | R2> => {
const next = iterator.next()
if (next.done) {
return core.readWithCause({
onInput: (input) => loop(input[Symbol.iterator]()),
onFailure: core.failCause,
onDone: core.succeed
})
} else {
return pipe(
f(next.value),
Effect.map(Option.match({
onNone: () => loop(iterator),
onSome: (a2) => core.flatMap(core.write(Chunk.of(a2)), () => loop(iterator))
})),
channel.unwrap
)
}
}
return new StreamImpl(pipe(toChannel(self), core.pipeTo(loop(Chunk.empty<A>()[Symbol.iterator]()))))
})
)

/** @internal */
export const filterMapWhile = dual<
<A, A2>(
Expand Down
30 changes: 30 additions & 0 deletions packages/effect/test/Stream/filtering.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as Chunk from "effect/Chunk"
import * as Effect from "effect/Effect"
import * as Either from "effect/Either"
import { pipe } from "effect/Function"
import * as Option from "effect/Option"
import * as Stream from "effect/Stream"
import * as it from "effect/test/utils/extend"
import { assert, describe } from "vitest"
Expand Down Expand Up @@ -46,4 +47,33 @@ describe("Stream", () => {
[Either.right(1), Either.right(2), Either.left("boom")]
)
}))

it.effect("filterMapEffectOption", () =>
Effect.gen(function*($) {
const result = yield* $(
Stream.make(1, 2, 3, 4),
Stream.filterMapEffectOption((n) =>
Effect.succeed(
n % 2 === 0 ? Option.some(n * 2) : Option.none()
)
),
Stream.runCollect
)
assert.deepStrictEqual(Array.from(result), [4, 8])
}))

it.effect("filterMapEffectOption - with failure", () =>
Effect.gen(function*($) {
const result = yield* $(
Stream.make(1, 2, 3),
Stream.filterMapEffectOption((n) =>
n === 2
? Effect.fail("boom")
: Effect.succeed(Option.some(n))
),
Stream.runCollect,
Effect.either
)
assert.deepStrictEqual(result, Either.left("boom"))
}))
})
Loading