Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD

fpdart

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.

�� learn functional programming and fpdart

would you like to know more about functional programming, fpdart, and how to use the package? check out this series of articles about functional programming with fpdart:

  1. fpdart, functional programming in dart and flutter
  2. how to use fpdart functional programming in your dart and flutter app
  3. pure functional app in flutter – pokemon app using fpdart and functional programming
  4. functional programming option type – introduction
  5. chain functions using option type – functional programming
  6. practical functional programming – part 1
  7. practical functional programming – part 2
  8. practical functional programming – part 3

�� types

  • option
  • either
  • unit
  • task
  • taskeither
  • state
  • stateasync
  • reader
  • tuple
  • io
  • iterable (list) extension
  • map extension
  • ioeither
  • taskoption
  • predicate
  • readereither
  • readertask
  • readertaskeither
  • statereadertaskeither
  • lens
  • writer

�� installation

# pubspec.yaml
dependencies:
  fpdart: ^0.0.11 # check out the latest version

✨ examples

option

/// create an instance of [some]
final option = option.of(10);

/// create an instance of [none]
final none = option<int>.none();

/// map [int] to [string]
final map = option.map((a) => '$a');

/// extract the value from [option]
final value = option.getorelse(() => -1);

/// pattern matching
final match = option.match(
  (a) => print('some($a)'),
  () => print('none'),
);

/// convert to [either]
final either = option.toeither(() => 'missing');

/// chain computations
final flatmap = option.flatmap((a) => option.of(a + 10));

/// return [none] if the function throws an error
final trycatch = option.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 [option]
final option = right.tooption();

reader

view the example folder for an explained usecase example.

state

view the example folder for an explained usecase example.

�� immutable collections

fpdart provides some extension methods on iterable (list) and map that extend the methods available by providing some functional programming signatures (safe methods that never mutate the original collection and that never throw exceptions).

integrations for immutable collections (ilist, iset, imap, etc.) are still being discussed with the community. fpdart does not want to be another immutable collection solution in the ecosystem. that is why we are working to integrate fpdart with other more mature packages that already implements immutable collections. stay tuned!

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 option (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

being documentation and stability important goals of the package, every type will go through an implementation-documentation-testing cycle before being considered as ‘stable’.

the roadmap for types development is highlighted below (breaking changes to ‘stable’ types are to be expected in this early stages):

  1. option
    • implementation
    • documentation
    • testing
  2. either
    • implementation
    • documentation
    • testing
  3. unit
    • implementation
    • documentation
    • testing
  4. task
    • implementation
    • documentation
    • testing
  5. taskeither
    • implementation
    • documentation
    • testing
  6. tuple
    • implementation
    • documentation
    • testing
  7. state
    • implementation
    • documentation
    • testing
  8. reader
    • implementation
    • documentation
    • testing
  9. io
    • implementation
    • documentation
    • testing
  10. ioeither
    • implementation
    • documentation
    • testing
  11. taskoption
    • implementation
    • documentation
    • testing
  12. readereither
    • implementation
    • documentation
    • testing
  13. readertask
    • implementation
    • documentation
    • testing
  14. readertaskeither
    • implementation
    • documentation
    • testing
  15. statereadertaskeither
    • implementation
    • documentation
    • testing
  16. writer
    • implementation
    • documentation
    • testing
  17. lens
    • implementation
    • documentation
    • testing

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!).

�� license

mit license, see the license.md file for details.


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD

Comments are closed.