Effect CommunityEC
Effect Community3y ago
28 replies
Great Britton

Converting to Effect Version: Extracting Keys from an Array

Is this the best way to do this to convert to effect version from this:
export const extractKeys = (inputs: unknown[]) =>
  Effect.succeed([
    ...new Set(
      inputs.flatMap((input) =>
        typeof input === "object" && !Array.isArray(input)
          ? input !== null
            ? Object.keys(input)
            : []
          : []
      )
    ),
  ]);

To this effect version or is this just over complicating it for no reason?
class NotAnObject extends Data.TaggedClass("NotAnObject")<{ error: unknown }> {}

export const extractKeysEffect = (inputs: unknown[]) =>
  pipe(
    Effect.forEach(inputs, (input) =>
      pipe(
        Effect.try({
          try: () => Object.keys(input as object),
          catch: (error) => new NotAnObject({ error }),
        }),
        Effect.catchTag("NotAnObject", Console.log)
      )
    ),
    Effect.map(HashSet.fromIterable)
  );
Was this page helpful?