From 252ac83f78ef0361eef2d9a0baa084c7201ed026 Mon Sep 17 00:00:00 2001 From: jcdang Date: Mon, 5 Dec 2022 17:17:15 -0600 Subject: [PATCH 1/2] SandroMaglione/fpdart#71 divide examples --- .gitignore | 3 ++- example/src/either/overview.dart | 19 +++++++++---------- example/src/option/overview.dart | 18 +++++++----------- 3 files changed, 18 insertions(+), 22 deletions(-) diff --git a/.gitignore b/.gitignore index cb80d170..9bddde4c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .dart_tool/ -.packages \ No newline at end of file +.packages +.idea/ \ No newline at end of file diff --git a/example/src/either/overview.dart b/example/src/either/overview.dart index 3c30ddb7..54b7fb4d 100644 --- a/example/src/either/overview.dart +++ b/example/src/either/overview.dart @@ -1,21 +1,20 @@ import 'package:fpdart/fpdart.dart'; /// Don't do that! ⚠ -double divideI(int x, int y) { - if (y == 0) { - throw Exception('Cannot divide by 0!'); - } +int divideI(int x, int y) => x ~/ y; // this will throw if y == 0 - return x / y; -} - -/// Error handling using [Either] 🎉 -Either divideF(int x, int y) { +/// Error handling without exceptions using [Either] 🎉 +Either divideF(int x, int y) { if (y == 0) { return left('Cannot divide by 0'); } + return right(x ~/ y); +} - return right(x / y); +/// Error handling with exceptions using [Either] 🎉 +Either divide2F(int x, int y) { + // Easy way with caveat: first param type 'object' due to dart limitation + return Either.tryCatch(() => x ~/ y, (o, s) => o.toString()); } void main() { diff --git a/example/src/option/overview.dart b/example/src/option/overview.dart index 80a773f4..f0fcc0d9 100644 --- a/example/src/option/overview.dart +++ b/example/src/option/overview.dart @@ -1,23 +1,19 @@ import 'package:fpdart/fpdart.dart'; /// Don't do that! ⚠ -double divideI(int x, int y) { - if (y == 0) { - throw Exception('Cannot divide by 0!'); - } - - return x / y; -} +int divideI(int x, int y) => x ~/ y; // this will throw if y == 0 -/// Error handling using [Option] 🎉 -Option divideF(int x, int y) { +/// Error handling without exceptions using [Option] 🎉 +Option divideF(int x, int y) { if (y == 0) { return none(); } - - return some(x / y); + return some(x ~/ y); } +/// Error handling with exceptions using [Option] 🎉 +Option divide2F(int x, int y) => Option.tryCatch(() => x ~/ y); + void main() { // --- Initialize an Option 👇 --- // const someInit = Some(10); From 1ac7f32f7b6357c69fb40cca05a60da7c5b82862 Mon Sep 17 00:00:00 2001 From: jcdang Date: Tue, 6 Dec 2022 00:03:21 -0600 Subject: [PATCH 2/2] converted comments to doc comments --- example/src/either/overview.dart | 2 +- example/src/option/overview.dart | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/example/src/either/overview.dart b/example/src/either/overview.dart index 54b7fb4d..e52b3fb7 100644 --- a/example/src/either/overview.dart +++ b/example/src/either/overview.dart @@ -13,7 +13,7 @@ Either divideF(int x, int y) { /// Error handling with exceptions using [Either] 🎉 Either divide2F(int x, int y) { - // Easy way with caveat: first param type 'object' due to dart limitation + /// Easy way with caveat: first param type 'object' due to dart limitation return Either.tryCatch(() => x ~/ y, (o, s) => o.toString()); } diff --git a/example/src/option/overview.dart b/example/src/option/overview.dart index f0fcc0d9..c00b7101 100644 --- a/example/src/option/overview.dart +++ b/example/src/option/overview.dart @@ -1,7 +1,7 @@ import 'package:fpdart/fpdart.dart'; /// Don't do that! ⚠ -int divideI(int x, int y) => x ~/ y; // this will throw if y == 0 +int divideI(int x, int y) => x ~/ y; /// this will throw if y == 0 /// Error handling without exceptions using [Option] 🎉 Option divideF(int x, int y) {