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

Implement Sequence method for Either type only #44

Closed
wants to merge 4 commits into from

Conversation

richardhp
Copy link

Context

Coming from a fp-ts background, I'm accustomed to be able to perform operations like sequenceT:

const prog = pipe(
  arrayOfItems.map((item) => {
    return TE.tryCatch(
      async () => {
        return await fetch(`/some/api?input=${item}`);
      },
      (e) => new Error('Error')
    );
  }),
  TE.sequenceT
);

In other words, maybe I need to hit an API multiple times with a bunch of inputs, so this is an array of TaskEithers, but at the end of the day, I just want to abstract that out, await this task once, and get back the array of responses.

I looked through the library for quite a long time and couldn't find anything like that in there yet, so I presumed that it's either called something else, or can be constructed somehow using Dart type magic, or just hasn't been implemented yet.

Approach

I'm quite new to Dart and am not fully up to speed with the Type system yet.

My understanding is that a better long-term solution is to implement some kind of abstract interface for sequence, then each type (like Either, TaskEither, Option etc) would provide some kind of method for how to implement a particular step in the sequence combinator.

I don't know how to do this yet, and also for my project I just need to unwrap Lists of Eithers at the moment. So I will be using this function in my code until this has been resolved in this project.

It's more of a suggestion at this stage, and I'm happy to take some feedback as to how to better integrate this feature into the package and work on a more comprehensive solution.

import 'package:test/test.dart';
import 'package:fpdart/fpdart.dart';
import 'package:fpdart/src/typeclass/traversable.dart';

Copy link
Author

@richardhp richardhp Aug 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I realise this is probably not the most appropriate place for this code / tests yet but I just stuck it here to begin with and get the ball rolling

@SandroMaglione
Copy link
Owner

Hi @richardhp

You are right, sequence is currently missing in fpdart.

Thanks for your suggestion and for taking the time to write and send this PR 🙏🏼

Based on your proposal, I am going to implement a more generic solution which fits well in the fpdart structure. If you would like to advance this further, I could you take a look at how fp-ts implements this:

@richardhp
Copy link
Author

@SandroMaglione

Ok when I get some time I'll take a look at this and see if I can move this PR further along

@SandroMaglione
Copy link
Owner

Hi @richardhp

#55 introduces both traverse and sequence to fpdart. Let me know if you have some feedback or suggestions.

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

Successfully merging this pull request may close these issues.

None yet

2 participants