Comments (5)
I think dart_either
has a practical implementation.
fp_dart
already has TaskEither.tryCatch
which is generate ADT from Effect, so TaskEither.tryCatchStream
would be have.
https://github.com/hoc081098/dart_either/blob/master/lib/src/dart_either.dart#L349-L384
/// Transforms data events to [Right]s and error events to [Left]s.
///
/// When the source stream emits a data event, the result stream will emit
/// a [Right] wrapping that data event.
///
/// When the source stream emits a error event, calling [errorMapper] with that error
/// and the result stream will emits a [Left] wrapping the result.
///
/// The done events will be forwarded.
///
/// ### Example
/// ```dart
/// final Stream<int> s = Stream.fromIterable([1, 2, 3, 4]);
/// final Stream<Either<Object, int>> eitherStream = Either.catchStreamError((e, s) => e, s);
///
/// eitherStream.listen(print); // prints Either.Right(1), Either.Right(2),
/// // Either.Right(3), Either.Right(4),
/// ```
///
/// ```dart
/// final Stream<int> s = Stream.error(Exception());
/// final Stream<Either<Object, int>> eitherStream = Either.catchStreamError((e, s) => e, s);
///
/// eitherStream.listen(print); // prints Either.Left(Exception)
/// ```
static Stream<Either<L, R>> catchStreamError<L, R>(
ErrorMapper<L> errorMapper,
Stream<R> stream,
) =>
stream.transform(
StreamTransformer<R, Either<L, R>>.fromHandlers(
handleData: (data, sink) => sink.add(Either.right(data)),
handleError: (e, s, sink) =>
sink.add(Either.left(errorMapper(e.throwIfFatal(), s))),
),
);
/// Map [error] and [stackTrace] to a [T] value.
typedef ErrorMapper<T> = T Function(Object error, StackTrace stackTrace);
from fpdart.
I have been working on a alternative to Stream
that is more functional in nature:
https://pub.dev/packages/offset_iterator
I have recently added OffsetIterable.fromStreamEither
which might be of interest! You can see it in action in this test case:
https://github.com/tim-smart/offset_iterator/blob/main/packages/offset_iterator/test/offset_iterator_test.dart#L111
from fpdart.
Streams are a very interesting (and complex) usecase
There are some patterns used in functional programming to deal with Streams. Adding them to fpdart I think would be interesting but also hard work!
Curious to hear any opinion about this
from fpdart.
I would like such a feature
Currently, I rely on RxDart to add more functionality to the stream such as the .onErrorReturnWith()
which gives me the correct effect, but can maybe be packaged a little better
from fpdart.
@SandroMaglione
It would be interesting to prioritize this part, since Future and Streams are commonly used. As in the case of my application
from fpdart.
Related Issues (20)
- fpdart sortBy is very slow and time processing scales very quickly HOT 3
- Add methods to convert `IO` to other classes (`TaskEither`, `TaskOption`, `IOEither`)
- Llinks are not working HOT 1
- fpdart v1.0.0 HOT 20
- Add getOrThrow for Either TaskEither HOT 1
- slight typo in lib/src/class/applicative.dart
- catch only specified exceptions vs all HOT 1
- Difference between `IOEither` and `Either` HOT 1
- change match/fold extension for bools to named parameters HOT 2
- Show example in docs of mixing functional packages and renaming classes HOT 2
- Add an extension on`Function` which convert a function that returns `Either` to async one HOT 1
- How to use chainEither on taskEither HOT 3
- Implement built-in conversions to Option and Either (e.g. `toIntOption()`) HOT 3
- Getter properties to simplify syntax HOT 1
- Does fpdart have an `orElse` for IO ? HOT 1
- Either of void HOT 3
- How to return Unit HOT 2
- Option losing alt and foldable HOT 1
- Issue: Version 0.4.1 creates Type Exception on getOrElse HOT 2
- foldLeft documentation is incorrect HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fpdart.