-
-
Notifications
You must be signed in to change notification settings - Fork 2
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
11 changed files
with
162 additions
and
87 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# MyQuran Dart Either | ||
|
||
## Either Class Implementation | ||
|
||
This Dart code provides an implementation of the `Either` type, a common functional programming pattern. The `Either` type represents a value that can be one of two types: `Right` or `Left`. This is useful for handling operations that can return two types of results, such as a successful value or an error. | ||
|
||
### Either<R, L> | ||
|
||
An abstract class for values that can be either `Right` or `Left`. | ||
|
||
- `fold<B>(B Function(L l) ifLeft, B Function(R r) ifRight)`: Executes one of the given functions based on the type of value. | ||
|
||
### Right<R, L> | ||
|
||
Represents a `Right` value. | ||
|
||
- `Right(this.value)`: Holds the `Right` value. | ||
- `fold<B>(B Function(L l) ifLeft, B Function(R r) ifRight)`: Executes the `ifRight` function with the `Right` value. | ||
|
||
### Left<R, L> | ||
|
||
Represents a `Left` value. | ||
|
||
- `Left(this.value)`: Holds the Left value. | ||
- `fold<B>(B Function(L l) ifLeft, B Function(R r) ifRight)`: Executes the `ifLeft` function with the `Left` value. | ||
|
||
### Usage | ||
|
||
Example usage of `Either`, `Right`, and `Left` classes: | ||
|
||
```dart | ||
void main() { | ||
final rightValue = Right<int, String>(42); | ||
final leftValue = Left<int, String>('Error'); | ||
final rightResult = rightValue.fold( | ||
(error) => 'Left: $error', | ||
(value) => 'Right: $value', | ||
); | ||
print(rightResult); // Output: "Right: 42" | ||
final leftResult = leftValue.fold( | ||
(error) => 'Left: $error', | ||
(value) => 'Right: $value', | ||
); | ||
print(leftResult); // Output: "Left: Error" | ||
} | ||
``` |
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,6 @@ | ||
include: package:very_good_analysis/analysis_options.6.0.0.yaml | ||
|
||
linter: | ||
rules: | ||
public_member_api_docs: false | ||
lines_longer_than_80_chars: false |
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,4 @@ | ||
/// MyQuran Dart Either | ||
library; | ||
|
||
export 'src/mq_either.dart'; |
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,50 @@ | ||
import 'package:meta/meta.dart'; | ||
|
||
/// An abstract class representing a value that can be either Right or Left. | ||
@immutable | ||
sealed class Either<R, L> { | ||
const Either(); | ||
|
||
/// Performs operations based on Right and Left values. | ||
B fold<B>(B Function(L l) ifLeft, B Function(R r) ifRight); | ||
} | ||
|
||
/// Represents the Right value of an Either. | ||
@immutable | ||
final class Right<R, L> extends Either<R, L> { | ||
const Right(this.value); | ||
|
||
/// Holds the Right value. | ||
final R value; | ||
|
||
/// Invokes the ifRight function using the Right value. | ||
@override | ||
B fold<B>(B Function(L l) ifLeft, B Function(R r) ifRight) => ifRight(value); | ||
|
||
/// Equality and hashCode overrides. | ||
@override | ||
bool operator ==(Object other) => other is Right && other.value == value; | ||
|
||
@override | ||
int get hashCode => value.hashCode; | ||
} | ||
|
||
/// Represents the Left value of an Either. | ||
@immutable | ||
final class Left<R, L> extends Either<R, L> { | ||
const Left(this.value); | ||
|
||
/// Holds the Left value. | ||
final L value; | ||
|
||
/// Invokes the ifLeft function using the Left value. | ||
@override | ||
B fold<B>(B Function(L l) ifLeft, B Function(R r) ifRight) => ifLeft(value); | ||
|
||
/// Equality and hashCode overrides. | ||
@override | ||
bool operator ==(Object other) => other is Left && other.value == value; | ||
|
||
@override | ||
int get hashCode => value.hashCode; | ||
} |
7 changes: 5 additions & 2 deletions
7
packages/mq_remote_client/pubspec.yaml → packages/mq_either/pubspec.yaml
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,49 @@ | ||
import 'package:mq_either/mq_either.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
void main() { | ||
group('Either', () { | ||
test('Return type is correctly', () { | ||
final rightResult = _successMethod(); | ||
final failedResult = _failedMethod(); | ||
|
||
final rightResultValue = switch (rightResult) { | ||
Right() => 'Test Value ${rightResult.value}', | ||
Left() => 'Test Value ${rightResult.value}', | ||
}; | ||
|
||
final failedResultValue = switch (failedResult) { | ||
Right() => 'Test Value ${failedResult.value}', | ||
Left() => 'Test Value ${failedResult.value}', | ||
}; | ||
|
||
expect(rightResultValue, 'Test Value 17'); | ||
expect(failedResultValue, 'Test Value Error'); | ||
}); | ||
|
||
test('Right value should fold correctly', () { | ||
const rightValue = Right<int, String>(42); | ||
|
||
final result = rightValue.fold( | ||
(error) => 'Left: $error', | ||
(value) => 'Right: $value', | ||
); | ||
|
||
expect(result, 'Right: 42'); | ||
}); | ||
|
||
test('Left value should fold correctly', () { | ||
const leftValue = Left<int, String>('Error'); | ||
|
||
final result = leftValue.fold( | ||
(error) => 'Left: $error', | ||
(value) => 'Right: $value', | ||
); | ||
|
||
expect(result, 'Left: Error'); | ||
}); | ||
}); | ||
} | ||
|
||
Either<int, String> _successMethod() => const Right(17); | ||
Either<int, String> _failedMethod() => const Left('Error'); |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.