Skip to content

Commit

Permalink
Change type name Maybe to Option [Some/None]
Browse files Browse the repository at this point in the history
  • Loading branch information
SandroMaglione committed Jun 13, 2021
1 parent bdbb318 commit 5404317
Show file tree
Hide file tree
Showing 26 changed files with 1,150 additions and 1,148 deletions.
2 changes: 1 addition & 1 deletion .packages
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#
# For more info see: https://dart.dev/go/dot-packages-deprecation
#
# Generated by pub on 2021-06-13 09:41:57.111321.
# Generated by pub on 2021-06-13 21:30:13.314180.
_fe_analyzer_shared:file:///C:/Users/Sandro%20Maglione/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/_fe_analyzer_shared-22.0.0/lib/
analyzer:file:///C:/Users/Sandro%20Maglione/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/analyzer-1.7.0/lib/
args:file:///C:/Users/Sandro%20Maglione/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/args-2.1.0/lib/
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# v0.0.3 - 13 June 2021

- Changed name of type `Maybe` to `Option` to be inline with fp-ts, cats, and dartz [**BREAKING CHANGE**]

# v0.0.2 - 13 June 2021

First major release:
Expand Down
47 changes: 24 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<img src="https://img.shields.io/github/stars/SandroMaglione/fpdart?logo=github" />
</a>
<img src="https://img.shields.io/github/license/SandroMaglione/fpdart?logo=github" />
<img src="https://img.shields.io/badge/version-0.0.2-blue.svg" />
<img src="https://img.shields.io/badge/version-0.0.3-blue.svg" />
<!-- <img src="https://img.shields.io/badge/flutter-v2.0.2-blue.svg" /> -->
<img src="https://img.shields.io/badge/dart-v2.13.1-blue.svg" />
<a href="https://github.com/SandroMaglione">
Expand All @@ -30,7 +30,7 @@ Fpdart is inspired by [fp-ts](https://gcanti.github.io/fp-ts/), [cats](https://t

## Types

- [x] `Maybe` (`Option`)
- [x] `Option`
- [x] `Either`
- [x] `Unit`
- [x] `Task`
Expand All @@ -43,7 +43,7 @@ Fpdart is inspired by [fp-ts](https://gcanti.github.io/fp-ts/), [cats](https://t
- [ ] `IOEither`
- [ ] `Writer`
- [ ] `Lens`
- [ ] `TaskMaybe`
- [ ] `TaskOption`
- [ ] `ReaderEither`
- [ ] `ReaderTask`
- [ ] `ReaderTaskEither`
Expand All @@ -54,40 +54,40 @@ Fpdart is inspired by [fp-ts](https://gcanti.github.io/fp-ts/), [cats](https://t
```yaml
# pubspec.yaml
dependencies:
fpdart: ^0.0.2 # Check out the latest version
fpdart: ^0.0.3 # Check out the latest version
```

## Examples

### [Maybe](https://github.com/SandroMaglione/fpdart/blob/9da7cae3b9f9dc690ff3255004393c4b979183e9/lib/src/maybe.dart#L40)
### [Option](https://github.com/SandroMaglione/fpdart/blob/9da7cae3b9f9dc690ff3255004393c4b979183e9/lib/src/option.dart#L40)

```dart
/// Create an instance of [Just]
final maybe = Maybe.of(10);
/// Create an instance of [Some]
final option = Option.of(10);
/// Create an instance of [Nothing]
final nothing = Maybe<int>.nothing();
/// Create an instance of [None]
final none = Option<int>.none();
/// Map [int] to [String]
final map = maybe.map((a) => '$a');
final map = option.map((a) => '$a');
/// Extract the value from [Maybe]
final value = maybe.getOrElse(() => -1);
/// Extract the value from [Option]
final value = option.getOrElse(() => -1);
/// Pattern matching
final match = maybe.match(
(just) => print('Just($just)'),
() => print('Nothing'),
final match = option.match(
(a) => print('Some($a)'),
() => print('None'),
);
/// Convert to [Either]
final either = maybe.toEither(() => 'missing');
final either = option.toEither(() => 'missing');
/// Chain computations
final flatMap = maybe.flatMap((a) => Maybe.of(a + 10));
final flatMap = option.flatMap((a) => Option.of(a + 10));
/// Return [Nothing] if the function throws an error
final tryCatch = Maybe.tryCatch(() => int.parse('invalid'));
/// Return [None] if the function throws an error
final tryCatch = Option.tryCatch(() => int.parse('invalid'));
```

### [Either](https://github.com/SandroMaglione/fpdart/blob/9da7cae3b9f9dc690ff3255004393c4b979183e9/lib/src/either.dart#L16)
Expand Down Expand Up @@ -124,8 +124,8 @@ final match = right.match(
(r) => print('Right($r)'),
);
/// Convert to [Maybe]
final maybe = right.toMaybe();
/// Convert to [Option]
final option = right.toOption();
```

### [Reader](https://github.com/SandroMaglione/fpdart/blob/9da7cae3b9f9dc690ff3255004393c4b979183e9/lib/src/reader.dart#L5)
Expand All @@ -150,7 +150,7 @@ Many non-functional languages are slowly adopting patterns from functional langu

Many packages are bringing functional patterns to dart, like the amazing [freezed](https://pub.dev/packages/freezed) for unions/pattern matching.

Fpdart aims to provide all the main types found in functional languages to dart. Types like `Maybe` (handle missing values without `null`), `Either` (handle errors and error messages), `Task` (composable async computations), and more.
Fpdart aims to provide all the main types found in functional languages to dart. Types like `Option` (handle missing values without `null`), `Either` (handle errors and error messages), `Task` (composable async computations), and more.

### Goal

Expand Down Expand Up @@ -188,14 +188,15 @@ In general, any contribution or feedback is welcome (and encouraged!).

## Versioning

- v0.0.3 - 13 June 2021
- v0.0.2 - 13 June 2021
- v0.0.1 - 28 May 2021

## Support

Currently the best way to support me would be to follow me on my [**Twitter**](https://twitter.com/SandroMaglione).

Another option (or `Maybe`) would be to buy me a coffee.
Another option (or `Option`) would be to buy me a coffee.

<a href="https://www.buymeacoffee.com/sandromaglione">
<img src="https://shields.io/badge/sandromaglione-Support--me-FFDD00?logo=buy-me-a-coffee&style=for-the-badge&link=https://www.buymeacoffee.com/sandromaglione" />
Expand Down
34 changes: 17 additions & 17 deletions example/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,33 @@ import 'package:fpdart/fpdart.dart';

void main() {}

void maybe() {
/// Create an instance of [Just]
final maybe = Maybe.of(10);
void option() {
/// Create an instance of [Some]
final option = Option.of(10);

/// Create an instance of [Nothing]
final nothing = Maybe<int>.nothing();
/// Create an instance of [None]
final none = Option<int>.none();

/// Map [int] to [String]
final map = maybe.map((a) => '$a');
final map = option.map((a) => '$a');

/// Extract the value from [Maybe]
final value = maybe.getOrElse(() => -1);
/// Extract the value from [Option]
final value = option.getOrElse(() => -1);

/// Pattern matching
final match = maybe.match(
(just) => print('Just($just)'),
() => print('Nothing'),
final match = option.match(
(a) => print('Some($a)'),
() => print('None'),
);

/// Convert to [Either]
final either = maybe.toEither(() => 'missing');
final either = option.toEither(() => 'missing');

/// Chain computations
final flatMap = maybe.flatMap((a) => Maybe.of(a + 10));
final flatMap = option.flatMap((a) => Option.of(a + 10));

/// Return [Nothing] if the function throws an error
final tryCatch = Maybe.tryCatch(() => int.parse('invalid'));
/// Return [None] if the function throws an error
final tryCatch = Option.tryCatch(() => int.parse('invalid'));
}

void either() {
Expand Down Expand Up @@ -63,6 +63,6 @@ void either() {
(r) => print('Right($r)'),
);

/// Convert to [Maybe]
final maybe = right.toMaybe();
/// Convert to [Option]
final option = right.toOption();
}
4 changes: 2 additions & 2 deletions example/src/either/overview.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ void main() {
(r) => print('Right($r)'),
);

/// Convert to [Maybe]
final maybe = right.toMaybe();
/// Convert to [Option]
final option = right.toOption();
}
30 changes: 0 additions & 30 deletions example/src/maybe/overview.dart

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ void main() {
printString(str);
}

final Maybe<String> mStr = Maybe.of('name');
final Option<String> mStr = Option.of('name');

/// Using [Maybe] you are required to specify every possible case.
/// Using [Option] you are required to specify every possible case.
/// The type system helps you to find and define edge-cases and avoid errors.
mStr.match(
printString,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ import 'package:fpdart/fpdart.dart';
double sumToDouble(int a, int b) => (a + b).toDouble();

void main() {
final a = Maybe.of(10);
final b = Maybe.of(20);
final a = Option.of(10);
final b = Option.of(20);

/// `map` takes one parameter [int] and returns `sumToDouble`.
/// We therefore have a function inside a [Maybe] that we want to
/// We therefore have a function inside a [Option] that we want to
/// apply to another value!
final Maybe<double Function(int)> map = a.map(
final Option<double Function(int)> map = a.map(
(a) => (int b) => sumToDouble(a, b),
);

/// Using `ap`, we get the final `Maybe<double>` that we want 🚀
/// Using `ap`, we get the final `Option<double>` that we want 🚀
final result = b.ap(map);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ double? intToDoubleNull(int a) {
}
}

Maybe<int> stringToInt(String a) => Maybe.fromPredicateMap<String, int>(
Option<int> stringToInt(String a) => Option.fromPredicateMap<String, int>(
a,
(str) => str.isNotEmpty,
(str) => str.length,
);

Maybe<double> intToDouble(int a) =>
Maybe.fromPredicateMap<int, double>(a, (v) => v != 0, (v) => v / 2);
Option<double> intToDouble(int a) =>
Option.fromPredicateMap<int, double>(a, (v) => v != 0, (v) => v / 2);

void main() {
/// Using `null`, you are required to check that the value is not
Expand All @@ -40,10 +40,10 @@ void main() {
/// Using `flatMap`, you can forget that the value may be missing and just
/// use it as if it was there.
///
/// In case one of the values is actually missing, you will get a [Nothing]
/// In case one of the values is actually missing, you will get a [None]
/// at the end of the chain ⛓
final a = Maybe.of('name');
final Maybe<double> result = a.flatMap(
final a = Option.of('name');
final Option<double> result = a.flatMap(
(s) => stringToInt(s).flatMap(
(i) => intToDouble(i),
),
Expand Down
30 changes: 30 additions & 0 deletions example/src/option/overview.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import 'package:fpdart/fpdart.dart';

void main() {
/// Create an instance of [Some]
final option = Option.of(10);

/// Create an instance of [None]
final none = Option<int>.none();

/// Map [int] to [String]
final map = option.map((a) => '$a');

/// Extract the value from [Option]
final value = option.getOrElse(() => -1);

/// Pattern matching
final match = option.match(
(a) => print('Some($a)'),
() => print('None'),
);

/// Convert to [Either]
final either = option.toEither(() => 'missing');

/// Chain computations
final flatMap = option.flatMap((a) => Option.of(a + 10));

/// Return [None] if the function throws an error
final tryCatch = Option.tryCatch(() => int.parse('invalid'));
}
2 changes: 1 addition & 1 deletion lib/fpdart.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export 'src/either.dart';
export 'src/function.dart';
export 'src/ilist.dart';
export 'src/maybe.dart';
export 'src/option.dart';
export 'src/reader.dart';
export 'src/state.dart';
export 'src/task.dart';
Expand Down
Loading

0 comments on commit 5404317

Please sign in to comment.