Skip to content

Instantly share code, notes, and snippets.

@patroza
Last active May 26, 2020 15:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save patroza/c9783cb20e45ad181b9103378caf6387 to your computer and use it in GitHub Desktop.
Save patroza/c9783cb20e45ad181b9103378caf6387 to your computer and use it in GitHub Desktop.
type MyA = {}
type SomeF<A, B> = (a: A) => [A, B]
type MyF = SomeF<MyA, boolean>
declare const f1: MyF
declare const f2: MyF
declare const f3: MyF
function imperative() {
const a: MyA = {}
const [newA1, B1] = f1(a)
const [newA2, B2] = f2(newA1)
const [newA3, B3] = f3(newA2)
// ...N any arbitrary amount
return [newA3, B1 || B2 || B3]
}
// Desired outcome:
const [AResult, BResult] = magic(f1, f2, f3)(a)
type SomeFEff<S, R, E, A, B> = (a: A) => T.Effect<S, R, E, [A, B]>
type MyFEff = SomeFEff<
unknown,
unknown,
unknown,
MyA,
boolean
>
declare const f1e: MyFEff
declare const f2e: MyFEff
declare const f3e: MyFEff
function imperativeWithEffect() {
const a: MyA = {}
return T.effect.chain(f1e(a), ([newA1, B1]) =>
T.effect.chain(f2e(newA1), ([newA2, B2]) =>
T.effect.map(f3e(newA2), ([newA3, B3]) =>
// ...N any arbitrary amount
[newA3, B1 || B2 || B3]
)
)
)
}
// Desired outcome:
T.effect.map(magic(f1, f2, f3)(a), ([AResult, BResult]) => ...)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment