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

Do notation #97

Merged
merged 24 commits into from
May 5, 2023
Merged

Do notation #97

merged 24 commits into from
May 5, 2023

Conversation

SandroMaglione
Copy link
Owner

@SandroMaglione SandroMaglione commented Mar 6, 2023

This PR introduces the Do notation in fpdart. This is the result of the discussion in #26.

The Do notation aims to simplify (flatten) long chains to a more readable and linear code, while still keeping the functional programming advantages and guarantees.

Below and example of refactoring for the Option type:

/// Without the Do notation
String goShopping() => goToShoppingCenter()
    .alt(goToLocalMarket)
    .flatMap(
      (market) => market.buyBanana().flatMap(
            (banana) => market.buyApple().flatMap(
                  (apple) => market.buyPear().flatMap(
                        (pear) => Option.of('Shopping: $banana, $apple, $pear'),
                      ),
                ),
          ),
    )
    .getOrElse(
      () => 'I did not find 🍌 or 🍎 or 🍐, so I did not buy anything 🤷‍♂️',
    );
/// Using the Do notation
String goShoppingDo() => Option.Do(
      ($) {
        final market = $(goToShoppingCenter().alt(goToLocalMarket));
        final amount = $(market.buyAmount());

        final banana = $(market.buyBanana());
        final apple = $(market.buyApple());
        final pear = $(market.buyPear());

        return 'Shopping: $banana, $apple, $pear';
      },
    ).getOrElse(
      () => 'I did not find 🍌 or 🍎 or 🍐, so I did not buy anything 🤷‍♂️',
    );

Status

  • Option
  • Either
  • TaskEither
  • Task
  • IO
  • IOEither
  • TaskOption

Note: The Do Notation will be released in a beta version for testing, before landing in an official fpdart release. We are looking for feedback 🤝

@SandroMaglione SandroMaglione linked an issue Mar 6, 2023 that may be closed by this pull request
@SandroMaglione SandroMaglione self-assigned this Mar 6, 2023
@SandroMaglione SandroMaglione added the enhancement New feature or request label Mar 6, 2023
lib/src/task_either.dart Outdated Show resolved Hide resolved
lib/src/option.dart Outdated Show resolved Hide resolved
lib/src/either.dart Outdated Show resolved Hide resolved
Copy link
Contributor

@tim-smart tim-smart left a comment

Choose a reason for hiding this comment

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

Changes look good 👍🏻

test/src/io_either_test.dart Outdated Show resolved Hide resolved
@SandroMaglione
Copy link
Owner Author

Released preview version v0.6.0-dev.1

@SandroMaglione SandroMaglione merged commit 55630dc into main May 5, 2023
@SandroMaglione SandroMaglione deleted the do-notation-async-await branch May 5, 2023 15:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Do Notation
2 participants