computables 0.0.4 copy "computables: ^0.0.4" to clipboard
computables: ^0.0.4 copied to clipboard

Computables are composable, streamable values.

Computables #

Computables are composable, streamable values.

Streamable #

import 'package:computables/computables.dart';

Computable(2).stream().listen((value) {
  print(value);
  // 2
});

Computable.fromIterable([1,2,3]).stream().listen((value) {
  print(value);
  // 1
  // 2
  // 3
});

## Readable

```dart
final computable = Computable(2);
print(computable.get()) // 2
computable.add(3);
print(computable.get()) // 3

Composable #

Computables can be composed together to

final computation = Computable.compute2(
  Computable.fromStream(
    Stream.value(1),
    initialValue: 0,
  ),
  Computable.fromFuture(
    Future.value(2),
    initialValue: 0,
  ),
  (input1, input2) => input1 + input2,
);

computation.stream().listen((value) {
  print(value);
  // 0
  // 1
  // 3
});
final computation = Computable.compute2(
  Computable.fromStream(
    Stream.value(1),
    initialValue: 0,
  ),
  Computable.fromFuture(
    Future.value(2),
    initialValue: 0,
  ),
  (input1, input2) => input1 + input2,
);

Computable.compute2(
  computation,
  Computable(1),
).stream().listen((value) {
  print(value);
  // 1
  // 2
  // 4
});

Transformable #

Computable inputs can be combined and transformed into an output computable:

final computable = Computable(1);
final computable2 = Computable(2);

final computation = Computable.transform2(
  computable,
  computable2,
  (input1, input2) {
    final fill = input2 - input1;
    return Computable.fromIterable(
      List.filled(fill, 0)
    );
  },
).stream().listen((value) {
  print('Value: $value');
});
// Value: 0

computable2.add(5);

// Value: 0
// Value: 0
// Value: 0
// Value: 0

Forwardable #

It can be helpful to subscribe computables to other inputs like a [Future] or [Stream].

final computable = Computable.subscriber(initialValue: 0);
final stream = Stream.fromIterable([1, 2, 3]);
computable.forwardStream(stream);

computable.stream().listen((value) {
  print(value);
  // 0
  // 1
  // 2
  // 3
})

Enjoyable? #

Reach out if there's any functionality you'd like added and happy coding!

1
likes
130
pub points
0%
popularity

Publisher

unverified uploader

Computables are composable, streamable values.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

flutter

More

Packages that depend on computables