Skip to content

Commit 4f0d555

Browse files
examples of Task in README
1 parent caf451a commit 4f0d555

2 files changed

Lines changed: 47 additions & 4 deletions

File tree

README.md

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ Fpdart is inspired by [fp-ts](https://gcanti.github.io/fp-ts/), [cats](https://t
5353

5454
Would you like to know more about functional programming, fpdart, and how to use the package?
5555

56-
[**Collection of tutorials on fpdart**](https://www.sandromaglione.com/course/fpdart-functional-programming-dart-and-flutter)
56+
📚 [**Collection of tutorials on fpdart**](https://www.sandromaglione.com/course/fpdart-functional-programming-dart-and-flutter)
5757

5858
Check out also this series of articles about functional programming with `fpdart`:
5959

@@ -81,7 +81,10 @@ dependencies:
8181
8282
## ✨ Examples
8383
84-
### [Option](https://github.com/SandroMaglione/fpdart/blob/540431746d616d30fadf36cc9d1a77c14baf35f4/lib/src/option.dart#L40)
84+
### [Option](./lib/src/option.dart)
85+
Used when a return value can be missing.
86+
> For example, when parsing a `String` to `int`, since not all `String`
87+
> can be converted to `int`
8588

8689
```dart
8790
/// Create an instance of [Some]
@@ -112,7 +115,10 @@ final flatMap = option.flatMap((a) => Option.of(a + 10));
112115
final tryCatch = Option.tryCatch(() => int.parse('invalid'));
113116
```
114117

115-
### [Either](https://github.com/SandroMaglione/fpdart/blob/9da7cae3b9f9dc690ff3255004393c4b979183e9/lib/src/either.dart#L16)
118+
### [Either](./lib/src/either.dart)
119+
Used to handle errors (instead of `Exception`s).
120+
> `Either<L, R>`: `L` is the type of the error (for example a `String` explaining
121+
> the problem), `R` is the return type when the computation is successful
116122

117123
```dart
118124
/// Create an instance of [Right]
@@ -150,6 +156,27 @@ final match = right.match(
150156
final option = right.toOption();
151157
```
152158

159+
### [Task](./lib/src/task.dart)
160+
Wrapper around an async function (`Future`). Allows to compose asynchronous functions **that never fail**.
161+
162+
```dart
163+
/// Create instance of [Task] from a value
164+
final Task<int> task = Task.of(10);
165+
166+
/// Create instance of [Task] from an async function
167+
final taskRun1 = Task(() async => 10);
168+
final taskRun2 = Task(() => Future.value(10));
169+
170+
/// Map [int] to [String]
171+
final Task<String> map = task.map((a) => '$a');
172+
173+
/// Extract the value inside [Task] by running its async function
174+
final int value = await task.run();
175+
176+
/// Chain another [Task] based on the value of the current [Task]
177+
final flatMap = task.flatMap((a) => Task.of(a + 10));
178+
```
179+
153180
### [Reader](https://github.com/SandroMaglione/fpdart/blob/9da7cae3b9f9dc690ff3255004393c4b979183e9/lib/src/reader.dart#L5)
154181

155182
View the [example folder for an explained usecase example](https://github.com/SandroMaglione/fpdart/tree/main/example/src/reader).

example/src/task/overview.dart

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,20 @@ Task<int> asyncF() {
1010
return Task(() async => 10).map((a) => a * 10);
1111
}
1212

13-
void main() {}
13+
Future<void> main() async {
14+
/// Create instance of [Task] from a value
15+
final Task<int> task = Task.of(10);
16+
17+
/// Create instance of [Task] from an async function
18+
final taskRun1 = Task(() async => 10);
19+
final taskRun2 = Task(() => Future.value(10));
20+
21+
/// Map [int] to [String]
22+
final Task<String> map = task.map((a) => '$a');
23+
24+
/// Extract the value inside [Task] by running its async function
25+
final int value = await task.run();
26+
27+
/// Chain another [Task] based on the value of the current [Task]
28+
final flatMap = task.flatMap((a) => Task.of(a + 10));
29+
}

0 commit comments

Comments
 (0)