dart_collection_sampler 1.0.7

dart_collection_sampler #

Pub Package CircleCI

a small library implementing methods to sample items from collections

Usage #

A simple usage example:

import 'package:args/args.dart';

import 'dart:io';
import 'package:dart_collection_sampler/dart_collection_sampler.dart';

main(List<String> arguments) {

  var argParser = ArgParser()
      ..addOption("num", abbr: "n", help: "items to pick from rest of command line", defaultsTo: "1");

  var results = argParser.parse(arguments);

  exit(
    roll(int.parse(results["num"]), results.rest)
  );
}

int roll(int n, List<String> items) {
  if (items.isEmpty) {
    print ("you must supply one or more items as input");
    return 1;
  }
  print ("Picking $n from $items\n");

  if (n == 1) {
    print ("Selected item: ${new CollectionSampler().pick(items)}");
  } else {
    print ("Selected items: ${new CollectionSampler().pickN(items, n)}");
  }
  return 0;
}


Features and bugs #

Please file feature requests and bugs at the issue tracker.

Changelog #

1.0.7 #

  • update dep on dice-parser

1.0.6 #

  • update dep on dice-parser to avoid dart sdk 2.3.0

1.0.5 #

  • move reservoir sampler to its own function outside of CollectionSampler

1.0.4 #

  • make code more idiomatic
  • changes to reservoir sampling algorithm

1.0.3 #

  • more analysis cleanup

1.0.0 #

  • upgrade deps, including to sdk 2.2.2
  • clean up analysis probs

0.6.1 #

update deps

0.6.0 #

update to dart 2

0.5.1 #

  • fix problem introduced by 0.5.0 changes for fixed-length reservoir

0.5.0 #

  • initialize reservoir size
  • clarify some details of reservoir sampling algorithm in method doc

0.4.0 #

  • clarify which selection methods return unique and non-unique

0.3.0 #

  • fix dumb comment in example

0.2.0 #

  • clean up all samplers to use reservoir sampling
  • add example code

0.1.0 #

  • fix up interfaces to use List, implement ReservoirSampler

0.0.1 #

  • Initial version, created by Steve Christensen

example/main.dart

import 'dart:io';

import 'package:args/args.dart';
import 'package:dart_collection_sampler/dart_collection_sampler.dart';

void main(List<String> arguments) {
  var argParser = ArgParser()
    ..addOption("num",
        abbr: "n",
        help: "items to pick from rest of command line",
        defaultsTo: "1")
    ..addFlag("unique",
        abbr: "u",
        help: "select unique results",
        defaultsTo: false,
        negatable: false);

  var results = argParser.parse(arguments);

  exit(deal(int.parse(results["num"]), results.rest, results['unique']));
}

int deal(int n, List<String> items, bool unique) {
  if (items.isEmpty) {
    print("you must supply one or more items as input");
    return 1;
  }
  print("Picking $n from $items (unique: $unique)\n");

  if (n == 1) {
    print("Selected item: ${CollectionSampler().pick(items)}");
  } else {
    print(
        "Selected items: ${CollectionSampler().pickN(items, n, unique: unique)}");
  }
  return 0;
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  dart_collection_sampler: ^1.0.7

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:dart_collection_sampler/dart_collection_sampler.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
25
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
63
Learn more about scoring.

We analyzed this package on Jul 22, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.4.0
  • pana: 0.12.19

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in primary library package:dart_collection_sampler/dart_collection_sampler.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.2.2 <3.0.0
dart_dice_parser ^1.4.2 1.4.2
Transitive dependencies
logging 0.11.3+2
petitparser 2.2.1 2.4.0
Dev dependencies
args ^1.5.2
mockito ^4.1.0
test ^1.6.4

Admin