Effect CommunityEC
Effect Community•3y ago•
17 replies
Great Britton

Pyramid of Doom with Effect.Do() and Effect.flatMap() in Program Creation

Hey everyone, I'm assuming I'm not doing this correctly, but for a program I'm making, I am running into a pyramid of doom when I am using Effect.Do() to bind 2 string[] variables both of which I am using an Effect.flatMap(() => Effect.forEach(... to map each one. I know I could break out the pipe to it's own function, but I wanted to keep it in a single function for readability sake if I can. Also wondering if all the flatMaps are best practice or if I should wrap each of the functions themselves for readability? Here's the code:

export const makeDirectories = pipe(
  Effect.Do(),
  Effect.bind("languages", () =>
    fetchJSONLocal<string[]>(
      paths.ddragon.languages.localPath,
      paths.ddragon.languages.type
    )
  ),
  Effect.bind("versions", () =>
    fetchJSONLocal<string[]>(
      paths.ddragon.versions.localPath,
      paths.ddragon.versions.type
    )
  ),
  Effect.flatMap(({ languages, versions }) =>
    pipe(
      Effect.unit(),
      Effect.flatMap(() => mkDirFrontendAssets),
      Effect.flatMap(() => mkDirBackendAssets),
      Effect.flatMap(() =>
        Effect.forEach(languages, (language) =>
          pipe(
            pipe(
              Effect.unit(),
              Effect.flatMap(() => mkDirFrontendDataLanguage(language)),
              Effect.flatMap(() => mkDirCDragonLanguage(language)),
              Effect.flatMap(() => mkDirDDragonLanguage(language))
            ),
            //slice used to avoid making directories for the versions before data collection
            Effect.flatMap(() =>
              Effect.forEach(versions.slice(0, -420), (version) =>
                pipe(
                  Effect.unit(),
                  Effect.flatMap(() => mkDirFrontendDataVersion(language, version)
                  ), ...
Was this page helpful?