-
Notifications
You must be signed in to change notification settings - Fork 45
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
Showing
17 changed files
with
509 additions
and
5 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
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,43 @@ | ||
import 'dart:math'; | ||
|
||
import 'package:fpdart/fpdart.dart'; | ||
|
||
int? nullable() => Random().nextBool() ? 10 : null; | ||
|
||
void main(List<String> args) { | ||
/// [Option] <-> `int?` | ||
int? value1 = 10.toOption().map((t) => t + 10).toNullable(); | ||
|
||
bool? value2 = value1?.isEven; | ||
|
||
/// `bool?` -> [Either] -> `int?` | ||
int? value3 = value2 | ||
.toEither(() => "Error") | ||
.flatMap((a) => a ? right<String, int>(10) : left<String, int>("None")) | ||
.toNullable(); | ||
|
||
/// `int?` -> [Option] | ||
Option<int> value4 = (value3?.abs().round()).toOption().flatMap(Option.of); | ||
|
||
Option<int> value = (10 | ||
.toOption() | ||
.map((t) => t + 10) | ||
.toNullable() | ||
|
||
/// Null safety 🎯 | ||
?.ceil() | ||
|
||
/// Null safety 🎯 | ||
.isEven | ||
.toEither(() => "Error") | ||
.flatMap((a) => right<String, int>(10)) | ||
.toNullable() | ||
|
||
/// Null safety 🎯 | ||
?.abs() | ||
|
||
/// Null safety 🎯 | ||
.round()) | ||
.toOption() | ||
.flatMap(Option.of); | ||
} |
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,64 @@ | ||
// ignore_for_file: unchecked_use_of_nullable_value, undefined_getter | ||
import 'package:fpdart/fpdart.dart'; | ||
import 'package:glados/glados.dart'; | ||
|
||
int doSomething(String str) => str.length + 10 * 2; | ||
int doSomethingElse(int number) => number + 10 * 2; | ||
|
||
void main(List<String> args) { | ||
int? nullableInt = 10; | ||
if (nullableInt == null) { | ||
print("Missing ‼️"); | ||
} else { | ||
print("Found $nullableInt 🎯"); | ||
} | ||
|
||
/// 👆 Exactly the same as 👇 | ||
Option<int> optionInt = Option.of(10); | ||
optionInt.match(() { | ||
print("Missing ‼️"); | ||
}, (t) { | ||
print("Found $nullableInt 🎯"); | ||
}); | ||
|
||
/// Null safety and `Option` save you from `null` 🚀 | ||
String? str = Random().nextBool() ? "string" : null; | ||
Option<String> optionStr = Random().nextBool() ? some("string") : none(); | ||
|
||
/// ⛔️ The property 'toLowerCase' can't be unconditionally accessed because the receiver can be 'null'. | ||
str.toLowerCase; | ||
|
||
/// ⛔️ The getter 'toLowerCase' isn't defined for the type 'Option<String>'. | ||
optionStr.toLowerCase; | ||
|
||
/// Option has methods that makes it more powerful (chain methods) ⛓ | ||
String? strNullable = Random().nextBool() ? "string" : null; | ||
Option<String> optionNullable = some("string"); | ||
|
||
/// Declarative API: more readable and composable 🎉 | ||
Option<double> optionIntNullable = optionNullable | ||
.map(doSomething) | ||
.alt(() => some(20)) | ||
.map(doSomethingElse) | ||
.flatMap((t) => some(t / 2)); | ||
|
||
/// Not really clear what is going on here 🤔 | ||
double? intNullable = (strNullable != null | ||
? doSomethingElse(doSomething(strNullable)) | ||
: doSomethingElse(20)) / | ||
2; | ||
|
||
if (optionNullable.isSome()) { | ||
/// Still type `Option<int>`, not `Some<int>` 😐 | ||
optionIntNullable; | ||
} | ||
|
||
if (strNullable != null) { | ||
/// This is now `String` 🤝 | ||
strNullable; | ||
} | ||
|
||
List<int>? list = Random().nextBool() ? [1, 2, 3, 4] : null; | ||
list.map((e) => /** What type is `e`? 😐 */ null); | ||
} |
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,46 @@ | ||
import 'dart:math'; | ||
|
||
import 'package:fpdart/fpdart.dart'; | ||
|
||
int? nullable() => Random().nextBool() ? 10 : null; | ||
|
||
String takesNullable(int? nullInt) => "$nullInt"; | ||
|
||
void main(List<String> args) { | ||
int noNull = 10; | ||
int? canBeNull = nullable(); | ||
|
||
/// `bool` | ||
final noNullIsEven = noNull.isEven; | ||
|
||
/// final canBeNullIsEven = canBeNull.isEven; ⛔️ | ||
/// `bool?` | ||
final canBeNullIsEven = canBeNull?.isEven; | ||
|
||
/// ☑️ | ||
takesNullable(canBeNull); | ||
|
||
/// ☑️ | ||
takesNullable(noNull); | ||
|
||
/// ☑️ | ||
noNull.abs(); | ||
|
||
/// ☑️ | ||
canBeNull?.abs(); | ||
|
||
Option<int> optionInt = Option.of(10); | ||
int? nullInt = nullable(); | ||
|
||
nullInt?.abs(); | ||
optionInt.map((t) => t.abs()); | ||
|
||
nullInt?.isEven; | ||
optionInt.map((t) => t.isEven); | ||
|
||
takesNullable(nullInt); | ||
|
||
/// takesNullable(optionInt); ⛔️ | ||
takesNullable(optionInt.toNullable()); | ||
} |
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
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,46 @@ | ||
import 'either.dart'; | ||
import 'io_either.dart'; | ||
import 'option.dart'; | ||
import 'task.dart'; | ||
import 'task_either.dart'; | ||
import 'task_option.dart'; | ||
|
||
/// `fpdart` extension to work on nullable values `T?` | ||
extension FpdartOnNullable<T> on T? { | ||
/// Convert a nullable type `T?` to [Option]: | ||
/// {@template fpdart_on_nullable_to_option} | ||
/// - [Some] if the value is **not** `null` | ||
/// - [None] if the value is `null` | ||
/// {@endtemplate} | ||
Option<T> toOption() => Option.fromNullable(this); | ||
|
||
/// Convert a nullable type `T?` to [Either]: | ||
/// {@template fpdart_on_nullable_to_either} | ||
/// - [Right] if the value is **not** `null` | ||
/// - [Left] containing the result of `onNull` if the value is `null` | ||
/// {@endtemplate} | ||
Either<L, T> toEither<L>(L Function() onNull) => | ||
Either.fromNullable(this, onNull); | ||
|
||
/// Convert a nullable type `T?` to [TaskOption]: | ||
/// {@macro fpdart_on_nullable_to_option} | ||
TaskOption<T> toTaskOption() => TaskOption.fromNullable(this); | ||
|
||
/// Convert a nullable type `T?` to [IOEither]. | ||
IOEither<L, T> toIOEither<L>(L Function() onNull) => | ||
IOEither.fromNullable(this, onNull); | ||
|
||
/// Convert a nullable type `T?` to [TaskEither] using a **sync function**: | ||
/// {@macro fpdart_on_nullable_to_either} | ||
/// | ||
/// If you want to run an **async** function `onNull`, use `toTaskEitherAsync`. | ||
TaskEither<L, T> toTaskEither<L>(L Function() onNull) => | ||
TaskEither.fromNullable(this, onNull); | ||
|
||
/// Convert a nullable type `T?` to [TaskEither] using an **async function**: | ||
/// {@macro fpdart_on_nullable_to_either} | ||
/// | ||
/// If you want to run an **sync** function `onNull`, use `toTaskEither`. | ||
TaskEither<L, T> toTaskEitherAsync<L>(Task<L> onNull) => | ||
TaskEither.fromNullableAsync(this, onNull); | ||
} |
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
Oops, something went wrong.