Module: ClosestNeighbours

Defined in:
lib/closest_neighbours.rb,
lib/closest_neighbours/grouper.rb,
lib/closest_neighbours/indexer.rb,
lib/closest_neighbours/version.rb,
lib/closest_neighbours/orderers/sorted_order.rb,
lib/closest_neighbours/orderers/default_order.rb,
lib/closest_neighbours/errors/non_integer_groups_error.rb,
lib/closest_neighbours/errors/insufficient_groups_error.rb,
lib/closest_neighbours/errors/incomparable_element_error.rb,
lib/closest_neighbours/errors/non_enumberable_argument_error.rb

Overview

Top level model for Closest Neighbours gem

Defined Under Namespace

Classes: DefaultOrder, Grouper, IncomparableElementError, Indexer, InsufficientGroupsError, NonEnumberableArgumentError, NonIntegerGroupsError, SortedOrder

Constant Summary collapse

VERSION =
'0.2.1'.freeze

Class Method Summary collapse

Class Method Details

.group(groups, data) ⇒ Array

Split an Enumerable into specified number of groups containing the closest elements in each group using the most general purpose algorithm.

Examples:


ClosestNeighbours.group(2, [10, 2, 11, 5])
# => [[2, 5], [10, 11]]

Parameters:

  • groups (Integer)

    The number of groups to create

  • data (Enumerable)

    The data set of items to group

Returns:

  • (Array)

    An array of ‘groups` groups



31
32
33
# File 'lib/closest_neighbours.rb', line 31

def self.group(groups, data)
  Grouper.new(orderer: SortedOrder.new).call(groups, data)
end

.ordered_group(groups, ordered_data) ⇒ Array

Split a sorted Enumerable into specified number of groups containing the closest elements in each group.

Examples:


ClosestNeighbours.ordered_group(2, [2, 5, 10, 11])
# => [[2, 5], [10, 11]]

Parameters:

  • groups (Integer)

    The number of groups to create

  • ordered_data (Enumerable)

    The data set of items to group

Returns:

  • (Array)

    An array of ‘groups` groups



48
49
50
# File 'lib/closest_neighbours.rb', line 48

def self.ordered_group(groups, ordered_data)
  Grouper.new(orderer: DefaultOrder.new).call(groups, ordered_data)
end