BallotLines<TCandidate extends Comparable>.fromBallots constructor

BallotLines<TCandidate extends Comparable>.fromBallots(
  1. Iterable<RankedBallot<TCandidate>> ballots, {
  2. String candidateToText(
    1. TCandidate
    ) = _defaultCandidateText,
})

Implementation

factory BallotLines.fromBallots(
  Iterable<RankedBallot<TCandidate>> ballots, {
  String Function(TCandidate) candidateToText = _defaultCandidateText,
}) {
  final grouped = ballots.fold<Map<RankedBallot<TCandidate>, int>>(
    <RankedBallot<TCandidate>, int>{},
    (map, ballot) {
      map[ballot] = (map[ballot] ?? 0) + 1;
      return map;
    },
  );

  return BallotLines(
    grouped.entries
        .map((e) => BallotLine(e.value, e.key.rank))
        .toList(growable: false)
          ..sort(),
    candidateToText: candidateToText,
  );
}