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

[Question] is there any better way to combine two Reader<Deps1, Result1> and Reader<Deps2, Result2>? #29

Closed
apiep opened this issue Nov 19, 2021 · 2 comments
Labels
question Asking about fpdart and functional programming

Comments

@apiep
Copy link

apiep commented Nov 19, 2021

So, I have a function that return Reader<Deps1, Task<bool>> called waitForAuthenticated() and a second function that return Reader<Deps2, Task<R>> called getSomething(), is there a better way to compose both reader?

I have tried to use

var task1 = waitForAuthenticated().run(_deps1);
var task2 = getSomething().run(_deps2);

// ...
// At the outside part of my code / public facing API
task1(task2).run();

But then, I kind of "dislike" calling .run() method on reader as it make my reader not lazy now, is there a better way to do this without calling .run() method on my reader?

@SandroMaglione
Copy link
Owner

Hi @apiep

Am I correct in assuming that you just want to execute task1 and task2 one after the other, without dependencies between the two?

In that case you can use local to change the context from Deps1 to Deps2:

import 'package:fpdart/fpdart.dart';

class Deps1 {
  final String dep;
  const Deps1(this.dep);
}

class Deps2 {
  final String dep;
  const Deps2(this.dep);
}

Reader<Deps1, Task<bool>> waitForAuthenticated() => Reader(
      (deps1) {
        print('Execute waitForAuthenticated: ${deps1.dep}');
        return Task.of(true);
      },
    );

Reader<Deps2, Task<String>> getSomething() => Reader(
      (deps2) {
        print('Execute getSomething: ${deps2.dep}');
        return Task.of("");
      },
    );

void main() async {
  const _deps1 = Deps1('I am Deps1');
  const _deps2 = Deps2('I am Deps2');
  final pipe = waitForAuthenticated()
      .local<Deps2>(constF(_deps1))
      .andThen(getSomething)
      .run(_deps2);

  final result = await pipe.run();
}

This code chains the calls to waitForAuthenticated and getSomething by calling them one after the other. The result is the following:

Execute waitForAuthenticated: I am Deps1
Execute getSomething: I am Deps2

Is this what you where looking for?

@SandroMaglione SandroMaglione added the question Asking about fpdart and functional programming label Nov 25, 2021
@apiep
Copy link
Author

apiep commented Nov 29, 2021

Yes it is something like that, that I wanted. Thank you :)

I noticed local method before, but because it has a parameter with it, that's the part what confuse me

@apiep apiep closed this as completed Nov 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Asking about fpdart and functional programming
Projects
None yet
Development

No branches or pull requests

2 participants