kseo / tap

Ruby's Object#tap for Dart

Home Page:https://pub.dartlang.org/packages/tap

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

tap

This package provides Ruby's Object#tap method for Dart.

Build Status Coverage Status

Usage

The primary purpose of this method is to “tap into” a method chain, in order to perform operations on intermediate results within the chain.

import 'package:quiver_collection/collection.dart';
import 'package:tap/tap.dart';

List<int> gen(int n) => new List<int>.generate(n, (i) => i);

class MyList<E> extends DelegatingList<E> with TapMixin<MyList<E>> {
  final List<E> _base = new List<E>();

  @override
  List<E> get delegate => _base;

  @override
  String toString() => _base.toString();
}

main() {
  final xs = new MyList<int>()..addAll(gen(10));
  xs
    ..tap((xs) => print('original: $xs'))
    ..removeWhere((x) => x % 2 == 0)
    ..tap((xs) => print('removed: $xs'))
    ..shuffle()
    ..tap((xs) => print('shuffled: $xs'))
    ..sort()
    ..tap((xs) => print('sorted: $xs'));
  // original: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  // removed: [1, 3, 5, 7, 9]
  // shuffled: [3, 1, 7, 9, 5] (can differ)
  // sorted: [1, 3, 5, 7, 9]
}

Features and bugs

Please file feature requests and bugs at the issue tracker.

About

Ruby's Object#tap for Dart

https://pub.dartlang.org/packages/tap

License:BSD 3-Clause "New" or "Revised" License


Languages

Language:Dart 79.7%Language:Shell 20.3%