No commit activity in last 3 years
No release in over 3 years
This gem takes an array of elements, groups them by their ordered closest neighbors into n groups.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 2.1.4
~> 4.7.3
~> 10.0
~> 3.0
~> 0.89.0
~> 0.21.2
 Project Readme

ClosestNeighbours

Gem Version Build Status

This gem takes an array of elements, groups them by their ordered closest neighbors into n groups.

Origin Story:

Friend:

"here's another one for you"

"I have an array of DateTime objects, I want to match them up into groups of 4 that are the closest to each other"

Me:

🤔 *That's an interesting problem. Maybe put this in a little gem*

Later that night

Me:

"Here is what I have so far..."

The algorithm to determine the closest neighbor is to look at the element before and after to determine which one is closer.

E.g. Given the sequence

input:        1   5    20
               \ / \  /
separated by:   4   14

1 and 5 (separated by 4) are closer together than 5 and 20 (separated by 14), so 5 is grouped with 1

groups:

  1. 1, 5
  2. 20

Installation

Add this line to your application's Gemfile:

gem 'closest_neighbours'

And then execute:

bundle

Or install it yourself as:

gem install closest_neighbours

Usage

Require the gem

require 'closest_neighbours'

Use the module:

ClosestNeighbours.group(2, [1, 4, 3, 2])
# => [[1, 2], [3, 4]]
ClosestNeighbours.group(2, [10.days.ago, 9.days.ago, 1.day.ago])
# => [[10.days.ago, 9.days.ago], [1.day.ago]]

ClosestNeighbours.ordered_group allows you to squeeze additional performance out of the module by providing an ordered set of inputs. If in doubt, use ClosestNeighbours.group.

ClosestNeighbours.ordered_group(2, [1, 2, 3, 4])
# => [[1, 2], [3, 4]]

Development

Setup

After checking out the repo, run setup to install dependencies.

bin/setup

Testing

Then, to run the test suite.

bundle exec rake spec

To run tests when a file changes.

bundle exec guard

Console

You can also run an interactive prompt that will allow you to experiment.

bin/console

Install

To install this gem onto your local machine.

bundle exec rake install

Release

To release a new version, update the version number in version.rb, and then run,

bundle exec rake release

This will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

To experiment with that code, run bin/console for an interactive prompt.

Bug reports and pull requests are welcome on GitHub at https://github.com/aussidavid/closest_neighbours. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the ClosestNeighbours project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.