GithubHelp home page GithubHelp logo

Streams in FPDart about fpdart HOT 5 OPEN

sandromaglione avatar sandromaglione commented on May 19, 2024 3
Streams in FPDart

from fpdart.

Comments (5)

dfdgsdfg avatar dfdgsdfg commented on May 19, 2024 2

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.

tim-smart avatar tim-smart commented on May 19, 2024 1

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.

SandroMaglione avatar SandroMaglione commented on May 19, 2024

Hi @lloydrichards

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.

lloydrichards avatar lloydrichards commented on May 19, 2024

I would like such a feature 😁 my app uses streams nearly constantly for database reading so being able to abstract my left and right makes it much easier for my application layer to return the correct state.

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.

TheHypnoo avatar TheHypnoo commented on May 19, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.