Skip to content

Add Effect.transposeFlatMapOption #4904

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

Conversation

vinassefranche
Copy link
Contributor

@vinassefranche vinassefranche commented May 19, 2025

Type

  • Refactor
  • Feature
  • Bug Fix
  • Optimization
  • Documentation Update

Description

This adds Effect.transposeFlatMapOption:

import { Effect, Option, pipe } from "effect"
//          ┌─── Effect<Option<number>, never, never>>
//          ▼
const noneResult = pipe(
  Option.none(),
  Effect.transposeFlatMapOption(() => Effect.succeedSome(42)) // will not be executed
)
console.log(Effect.runSync(noneResult))
// Output: { _id: 'Option', _tag: 'None' }
//          ┌─── Effect<Option<number>, never, never>>
//          ▼
const someSuccessResult = pipe(
  Option.some(42),
  Effect.transposeMapOption((value) => Effect.succeedSome(value 2))
)
console.log(Effect.runSync(someSuccessResult))
// Output: { _id: 'Option', _tag: 'Some', value: 84 }
//          ┌─── Effect<Option<number>, never, never>>
//          ▼
const someSuccessResult = pipe(
  Option.some(42),
  Effect.transposeMapOption((value) => Effect.succeedNone())
)
console.log(Effect.runSync(someSuccessResult))
// Output: { _id: 'Option', _tag: 'None' }

Related

  • Related Issue #
  • Closes #

@github-project-automation github-project-automation bot moved this to Discussion Ongoing in PR Backlog May 19, 2025
Copy link

changeset-bot bot commented May 19, 2025

🦋 Changeset detected

Latest commit: d0fba6d

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 32 packages
Name Type
effect Minor
@effect/cli Major
@effect/cluster Major
@effect/experimental Major
@effect/opentelemetry Major
@effect/platform-browser Major
@effect/platform-bun Major
@effect/platform-node-shared Major
@effect/platform-node Major
@effect/platform Major
@effect/printer-ansi Major
@effect/printer Major
@effect/rpc Major
@effect/sql-clickhouse Major
@effect/sql-d1 Major
@effect/sql-drizzle Major
@effect/sql-kysely Major
@effect/sql-libsql Major
@effect/sql-mssql Major
@effect/sql-mysql2 Major
@effect/sql-pg Major
@effect/sql-sqlite-bun Major
@effect/sql-sqlite-do Major
@effect/sql-sqlite-node Major
@effect/sql-sqlite-react-native Major
@effect/sql-sqlite-wasm Major
@effect/sql Major
@effect/typeclass Major
@effect/vitest Major
@effect/ai Major
@effect/ai-anthropic Major
@effect/ai-openai Major

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@effect-bot effect-bot changed the base branch from main to next-minor May 19, 2025 15:15
@effect-bot effect-bot force-pushed the next-minor branch 2 times, most recently from 6cb2e94 to ca6c582 Compare May 19, 2025 22:16
@tim-smart
Copy link
Contributor

Not sure about this, we are trying to get away from auto flattening now.

@effect-bot effect-bot force-pushed the next-minor branch 2 times, most recently from 336cc43 to aba8571 Compare May 19, 2025 23:26
@gcanti
Copy link
Contributor

gcanti commented May 20, 2025

Yeah, automatic flattening is almost always an unexpected behavior and should be avoided.

@vinassefranche
Copy link
Contributor Author

Ok. I get the point.
@tim-smart or @gcanti would you be ok with a transposeFlatMapOption then or is it not an option?

@effect-bot effect-bot force-pushed the next-minor branch 2 times, most recently from 1d3e3be to 9c3f578 Compare May 22, 2025 10:06
@vinassefranche vinassefranche force-pushed the improve-transpose-map-option branch from c06437a to 67530c3 Compare May 22, 2025 12:45
@vinassefranche vinassefranche changed the title Flatten options when returning an option in Effect.transposeMapOption Add Effect.transposeFlatMapOption May 22, 2025
@vinassefranche
Copy link
Contributor Author

@tim-smart and @gcanti I did the change to Effect.transposeFlatMapOption. Tell me if you think it's a good idea or if I should discard the pull request completely

@effect-bot effect-bot force-pushed the next-minor branch 3 times, most recently from aa2ec73 to c051853 Compare May 23, 2025 01:29
@vinassefranche vinassefranche force-pushed the improve-transpose-map-option branch from 9b92093 to bf30ec1 Compare May 23, 2025 10:09
@effect-bot effect-bot force-pushed the next-minor branch 3 times, most recently from c6bd1f8 to 07444af Compare May 23, 2025 18:43
@vinassefranche vinassefranche force-pushed the improve-transpose-map-option branch from bf30ec1 to 7379442 Compare May 23, 2025 20:37
@effect-bot effect-bot force-pushed the next-minor branch 4 times, most recently from 59ef614 to 7d8c110 Compare May 24, 2025 09:18
@vinassefranche vinassefranche force-pushed the improve-transpose-map-option branch from 7379442 to e317b2f Compare May 26, 2025 06:42
@vinassefranche vinassefranche force-pushed the improve-transpose-map-option branch from e317b2f to b13285d Compare May 26, 2025 16:50
@effect-bot effect-bot force-pushed the next-minor branch 3 times, most recently from bbba077 to 6c5dc32 Compare May 27, 2025 04:10
@vinassefranche vinassefranche force-pushed the improve-transpose-map-option branch from b13285d to d0fba6d Compare May 27, 2025 06:53
@gcanti
Copy link
Contributor

gcanti commented May 27, 2025

@vinassefranche I'm not sure this is a common enough use case to justify a dedicated API

import { Effect, Option, pipe } from "effect"

//     ┌─── Effect<Option<number>, never, never>>
//     ▼
const res = pipe(
  Option.some(2),
  Effect.transposeMapOption((n) => Effect.succeedSome(n * 3)),
  Effect.map(Option.flatten)                                   
)

@vinassefranche
Copy link
Contributor Author

@vinassefranche I'm not sure this is a common enough use case to justify a dedicated API

import { Effect, Option, pipe } from "effect"

//     ┌─── Effect<Option<number>, never, never>>
//     ▼
const res = pipe(
  Option.some(2),
  Effect.transposeMapOption((n) => Effect.succeedSome(n * 3)),
  Effect.map(Option.flatten)                                   
)

Alright. We have this helper in our code base anyway. This is something we do pretty often so it's worth for us but I know you want to limit the api surface so I won't push it :-)

@github-project-automation github-project-automation bot moved this from Discussion Ongoing to Done in PR Backlog May 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

4 participants