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.
functional programming
and fpdart
�� learn 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:
- fpdart, functional programming in dart and flutter
- how to use fpdart functional programming in your dart and flutter app
- pure functional app in flutter – pokemon app using fpdart and functional programming
- functional programming option type – introduction
- chain functions using option type – functional programming
- practical functional programming – part 1
- practical functional programming – part 2
- 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.
dartz
comparison with 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):
option
implementationdocumentationtesting
either
implementationdocumentationtesting
unit
implementationdocumentationtesting
task
implementationdocumentationtesting
taskeither
implementationdocumentationtesting
tuple
implementationdocumentationtesting
state
implementationdocumentationtesting
reader
implementationdocumentationtesting
io
implementationdocumentationtesting
ioeither
implementationdocumentationtesting
taskoption
implementationdocumentationtesting
readereither
- implementation
- documentation
- testing
readertask
- implementation
- documentation
- testing
readertaskeither
- implementation
- documentation
- testing
statereadertaskeither
- implementation
- documentation
- testing
writer
- implementation
- documentation
- testing
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.
Comments are closed.