-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
587cd6e
commit 6675e62
Showing
3 changed files
with
154 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import 'package:fpdart/fpdart.dart'; | ||
|
||
/// Helper functions ⚙️ (sync) | ||
String addNamePrefix(String name) => "Mr. $name"; | ||
String addEmailPrefix(String email) => "mailto:$email"; | ||
String decodeName(int code) => "$code"; | ||
|
||
/// API functions 🔌 (async) | ||
Future<String> getUsername() => Future.value("Sandro"); | ||
Future<int> getEncodedName() => Future.value(10); | ||
|
||
Future<String> getEmail() => Future.value("@"); | ||
|
||
Future<bool> sendInformation(String usernameOrName, String email) => | ||
Future.value(true); | ||
|
||
Future<bool> withFuture() async { | ||
late String usernameOrName; | ||
late String email; | ||
|
||
try { | ||
usernameOrName = await getUsername(); | ||
} catch (e) { | ||
try { | ||
usernameOrName = decodeName(await getEncodedName()); | ||
} catch (e) { | ||
throw Exception("Missing both username and name"); | ||
} | ||
} | ||
|
||
try { | ||
email = await getEmail(); | ||
} catch (e) { | ||
throw Exception("Missing email"); | ||
} | ||
|
||
try { | ||
final usernameOrNamePrefix = addNamePrefix(usernameOrName); | ||
final emailPrefix = addEmailPrefix(email); | ||
return await sendInformation(usernameOrNamePrefix, emailPrefix); | ||
} catch (e) { | ||
throw Exception("Error when sending information"); | ||
} | ||
} | ||
|
||
TaskEither<String, bool> withTask() => TaskEither.tryCatch( | ||
getUsername, | ||
(_, __) => "Missing username", | ||
) | ||
.alt( | ||
() => TaskEither.tryCatch( | ||
getEncodedName, | ||
(_, __) => "Missing name", | ||
).map( | ||
decodeName, | ||
), | ||
) | ||
.map( | ||
addNamePrefix, | ||
) | ||
.flatMap( | ||
(usernameOrNamePrefix) => TaskEither.tryCatch( | ||
getEmail, | ||
(_, __) => "Missing email", | ||
) | ||
.map( | ||
addEmailPrefix, | ||
) | ||
.flatMap( | ||
(emailPrefix) => TaskEither.tryCatch( | ||
() => sendInformation(usernameOrNamePrefix, emailPrefix), | ||
(_, __) => "Error when sending information", | ||
), | ||
), | ||
); | ||
|
||
Task<int> getTask() => Task(() async { | ||
print("I am running [Task]..."); | ||
return 10; | ||
}); | ||
|
||
Future<int> getFuture() async { | ||
print("I am running [Future]..."); | ||
return 10; | ||
} | ||
|
||
void main() { | ||
Task<int> taskInt = getTask(); | ||
Future<int> futureInt = getFuture(); | ||
|
||
// Future<int> taskRun = taskInt.run(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import 'package:fpdart/fpdart.dart'; | ||
|
||
late Future<Map<String, Object>?> example; | ||
|
||
final taskOp = TaskOption.flatten( | ||
(TaskOption.fromTask( | ||
Task<Map<String, Object>?>( | ||
() => example, | ||
).map( | ||
(ex) => Option.fromNullable(ex).toTaskOption(), | ||
), | ||
)), | ||
); | ||
|
||
/// New API `toOption`: from `Map<String, Object>?` to `Option<Map<String, Object>>` | ||
final taskOpNew = TaskOption<Map<String, Object>>( | ||
() async => (await example).toOption(), | ||
); | ||
|
||
/// Using `Option.fromNullable`, the [Future] cannot fail | ||
final taskOpNoFail = TaskOption<Map<String, Object>>( | ||
() async => Option.fromNullable(await example), | ||
); | ||
|
||
/// Using `Option.fromNullable` when the [Future] can fail | ||
final taskOpFail = TaskOption<Map<String, Object>?>.tryCatch( | ||
() => example, | ||
).flatMap<Map<String, Object>>( | ||
(r) => Option.fromNullable(r).toTaskOption(), | ||
); |