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

Traverse & Sequence #55

Merged
merged 24 commits into from
Oct 7, 2022
Merged

Traverse & Sequence #55

merged 24 commits into from
Oct 7, 2022

Conversation

SandroMaglione
Copy link
Owner

Added traverse and sequence methods to fpdart.

Specifically, this PR introduces the following methods:

  • traverseList
  • traverseListWithIndex
  • sequenceList
  • traverseListSeq [Task]
  • traverseListWithIndexSeq [Task]
  • sequenceListSeq [Task]

traverse and sequence allow to convert a List of a typeclass to a typeclass of List. These methods have been added to the following classes:

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

These methods have been implemented as static methods. Furthermore, an extension method to List has been also added.

For example, using traverse on Option is possible to validate a List:

/// "a40" is invalid 💥
final inputValues = ["10", "20", "30", "a40"];

/// Verify that all the values can be converted to [int] 🔐
///
/// If **any** of them is invalid, then the result is [None] 🙅‍♂️
final traverseOption = inputValues.traverseOption(
  (a) => Option.tryCatch(
    /// If `a` does not contain a valid integer literal a [FormatException] is thrown
    () => int.parse(a),
  ),
);

The function uses Option.tryCatch to map each element of the List to Option, resulting in List<Option>. traverse then converts List<Option> to Option<List>.

@SandroMaglione SandroMaglione self-assigned this Oct 7, 2022
@SandroMaglione SandroMaglione added the enhancement New feature or request label Oct 7, 2022
@SandroMaglione SandroMaglione changed the title Traversable & Sequence Traverse & Sequence Oct 7, 2022
@SandroMaglione SandroMaglione merged commit 8772ab1 into main Oct 7, 2022
@SandroMaglione SandroMaglione deleted the traversable branch October 7, 2022 03:45
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.

1 participant