Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ookami-kb/dfunc
Functional batteries for Dart programming language.
https://github.com/ookami-kb/dfunc
dart dart-library dart-package dart2 dartlang functional-batteries
Last synced: 13 days ago
JSON representation
Functional batteries for Dart programming language.
- Host: GitHub
- URL: https://github.com/ookami-kb/dfunc
- Owner: ookami-kb
- License: bsd-2-clause
- Created: 2019-09-07T17:52:07.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2024-06-03T16:29:10.000Z (5 months ago)
- Last Synced: 2024-10-12T18:06:42.993Z (27 days ago)
- Topics: dart, dart-library, dart-package, dart2, dartlang, functional-batteries
- Language: Dart
- Homepage: https://pub.dev/packages/dfunc
- Size: 166 KB
- Stars: 20
- Watchers: 4
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
![](https://github.com/ookami-kb/dfunc/workflows/Dart%20CI/badge.svg)
[![Pub Version](https://img.shields.io/pub/v/dfunc)](https://pub.dev/packages/dfunc)
[![codecov](https://codecov.io/gh/ookami-kb/dfunc/branch/master/graph/badge.svg?token=K70NTIMBR5)](https://codecov.io/gh/ookami-kb/dfunc)Lightweight and practical functional library for Dart programming language.
The goal of this library is not having deep and hardcore FP features. If you need them, take a look
at [dartz](https://pub.dev/packages/dartz) package.`dfunc` instead, provides some practical features based on FP principles, that we use extensively in our production
codebase. We try to keep the library aligned with "Dart way" of doing things, instead of turning it into Haskell or
Scala :)## Optional
There's no `Optional` type. Since nullsafety is introduced, nullable types cover the majority of use-cases for them.
Instead, `dfunc` provides you some extensions for nullable types.### maybeMap and maybeFlatMap
```dart
void main() {
String? x = null;
x.maybeMap((x) => x.toUpperCase()); // nullx = 'test';
x.maybeMap((x) => x.toUpperCase()); // 'TEST'x.maybeFlatMap((x) => x == 'test' ? 'OK' : null); // 'OK'
}
```Both `maybeMap` and `maybeFlatMap` can be replaced with `let` if you prefer Kotlin-style:
```dart
void main() {
String? x = null;
x?.let((x) => x.toUpperCase()); // nullx = 'test';
x?.let((x) => x.toUpperCase()); // 'TEST'x?.let((x) => x == 'test' ? 'OK' : null); // 'OK'
}
```The differences are:
- you don't need `?.` with `maybe*` functions, they unwrap the value automatically;
- you can enforce the function in `maybeMap` to return non-nullable result.### maybeWhere
Same as you can filter the list with `where` method to remove undesired values (and probably get an empty list), you can
use `maybeWhere` to "filter" optional value. As a result you will get either the value itself, or null if the condition
is not satisfied:```dart
void main() {
final List a = [1];
a.where((e) => e == 1); // [1]
a.where((e) => e == 2); // []final int b = 1;
b.maybeWhere((e) => e == 1); // 1
b.maybeWhere((e) => e == 2); // null
}
```## Either
`TBD`
## Utilities
`TBD`
## Features and bugs
Please file feature requests and bugs at the [issue tracker](https://github.com/ookami-kb/dfunc/issues).