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

What is the inverse operation of A.sequenceT? #1860

Open
Carsten-Leue opened this issue May 3, 2023 · 1 comment
Open

What is the inverse operation of A.sequenceT? #1860

Carsten-Leue opened this issue May 3, 2023 · 1 comment

Comments

@Carsten-Leue
Copy link

The operation A.sequenceT allows to convert a sequence of typed Monads into a Monad of a typed tuple, such as in this Option example:

        const sequenceO = A.sequenceT(O.Apply);

        const o1: Option<number> = O.some(1);
        const o2: Option<string> = O.some("1");

        const o12: Option<[number, string]> = sequenceO(o1, o2);

what is the best representation of the inverse operation?

The best I could do as a special case for Option was:

        const unsequenceO = O.fold<[number, string], [Option<number>, Option<string>]>(() => [O.none, O.none], ([s1, s2]) => [O.some(s1), O.some(s2)]);

        const [u1, u2] = unsequenceO(o12);

but I wonder if there exists a concept (and more generic implementation) for this.

@ENvironmentSet
Copy link

It’s not possible for arbitrary monad to have inverse function of ‘sequence’ since there is no deterministic(or even possible) way to decompose given monadic value with single effect into monadic value with serveral, chained effects. Just as it’s not possible to know what values were used to calculate given value that forms monoid. (1 + 9 is 10, but 2 + 8 and 3 + 7 is also 10, it’s not possible to naturally determine combination to be used as result of inverse function)

PS. Even for Option monad, it’s still not possible to implement inverse function of sequence.

sequenceO(none, some(1)) is none, but sequenceO(none, none) is none too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants