fpdart 0.0.2+1 copy "fpdart: ^0.0.2+1" to clipboard
fpdart: ^0.0.2+1 copied to clipboard

outdated

Functional programming in Dart and Flutter. All the main functional programming types and patterns fully documented, tested, and with examples.

Fpdart #

GitHub: SandroMaglione Twitter: SandroMaglione

Functional programming in Dart and Flutter. All the main functional programming types and patterns fully documented, tested, and with examples.

Fpdart is fully documented. You do not need to have any previous experience with functional programming to start using fpdart. Give it a try!

Fpdart is inspired by fp-ts, cats, and dartz.

Note: The package is still in early development. The API may change frequently and there will be many breaking changes. The documentation and testing is currently under development, but it is coming soon and fast. Follow my Twitter for daily updates.

Types #

  • Maybe (Option)
  • Either
  • Unit
  • Task
  • TaskEither
  • State
  • Reader
  • Tuple
  • IList
  • IO
  • IOEither
  • Writer
  • Lens
  • TaskMaybe
  • ReaderEither
  • ReaderTask
  • ReaderTaskEither
  • StateReaderTaskEither

Installation #

# pubspec.yaml
dependencies:
  fpdart: ^0.0.2 # Check out the latest version

Examples #

Maybe #

/// Create an instance of [Just]
final maybe = Maybe.of(10);

/// Create an instance of [Nothing]
final nothing = Maybe<int>.nothing();

/// Map [int] to [String]
final map = maybe.map((a) => '$a');

/// Extract the value from [Maybe]
final value = maybe.getOrElse(() => -1);

/// Pattern matching
final match = maybe.match(
  (just) => print('Just($just)'),
  () => print('Nothing'),
);

/// Convert to [Either]
final either = maybe.toEither(() => 'missing');

/// Chain computations
final flatMap = maybe.flatMap((a) => Maybe.of(a + 10));

/// Return [Nothing] if the function throws an error
final tryCatch = Maybe.tryCatch(() => int.parse('invalid'));

Either #

/// Create an instance of [Right]
final right = Either<String, int>.of(10);

/// Create an instance of [Left]
final left = Either<String, int>.left('none');

/// Map the right value to a [String]
final mapRight = right.map((a) => '$a');

/// Map the left value to a [int]
final mapLeft = right.mapLeft((a) => a.length);

/// Return [Left] if the function throws an error.
/// Otherwise return [Right].
final tryCatch = Either.tryCatch(
  () => int.parse('invalid'),
  (e, s) => 'Error: $e',
);

/// Extract the value from [Either]
final value = right.getOrElse((l) => -1);

/// Chain computations
final flatMap = right.flatMap((a) => Either.of(a + 10));

/// Pattern matching
final match = right.match(
  (l) => print('Left($l)'),
  (r) => print('Right($r)'),
);

/// Convert to [Maybe]
final maybe = right.toMaybe();

Reader #

View the example folder for an explained usecase example.

State #

View the example folder for an explained usecase example.

More #

Many more examples are coming soon. Check out my website and my Twitter for daily updates.


Motivation #

Functional programming is becoming more and more popular, and for good reasons.

Many non-functional languages are slowly adopting patterns from functional languages, dart included. Dart already supports higher-order functions, generic types, type inference. Other functional programming features are coming to the language, like pattern matching, destructuring, multiple return values, higher-order types.

Many packages are bringing functional patterns to dart, like the amazing freezed for unions/pattern matching.

Fpdart aims to provide all the main types found in functional languages to dart. Types like Maybe (handle missing values without null), Either (handle errors and error messages), Task (composable async computations), and more.

Goal #

Differently from many other functional programming packages, fpdart aims to introduce functional programming to every developer. For this reason, every type and method is commented and documented directly in the code.

You do not need to have any previous experience with functional programming to start using fpdart.

Fpdart also provides real-world examples of why a type is useful and how it can be used in your application. Check out my website for blog posts and articles.

Comparison with dartz #

One of the major pain points of dartz has always been is lack of documentation. This is a huge issue for people new to functional programming to attempt using the package.

dartz was released in 2016, initially targeting Dart 1.

dartz is also missing some features and types (Reader, TaskEither, and others).

Fpdart is a rewrite based on fp-ts and cats. The main differences are:

  • Fpdart is fully documented.
  • Fpdart implements higher-kinded types using defunctionalization.
  • Fpdart is based on Dart 2.
  • Fpdart is completely null-safe from the beginning.
  • Fpdart has a richer API.
  • Fpdart implements some missing types in dartz.
  • Fpdart (currently) does not provide implementation for immutable collections (ISet, IMap, IHashMap, AVLTree).

Roadmap #

The long-term goal is to provide all the main types and typeclasses available in other functional programming languages and packages. All the types should be completely documented and fully tested.

A well explained documentation is the key for the long-term success of the project. Any article, blog post, or contribution is welcome.

In general, any contribution or feedback is welcome (and encouraged!).

Versioning #

  • v0.0.2 - 13 June 2021
  • v0.0.1 - 28 May 2021

Support #

Currently the best way to support me would be to follow me on my Twitter.

Another option (or Maybe) would be to buy me a coffee.

License #

MIT License, see the LICENSE.md file for details.

746
likes
0
pub points
98%
popularity

Publisher

verified publishersandromaglione.com

Functional programming in Dart and Flutter. All the main functional programming types and patterns fully documented, tested, and with examples.

Homepage
Repository (GitHub)
View/report issues

License

unknown (LICENSE)

More

Packages that depend on fpdart