(Translated by https://www.hiragana.jp/)
GitHub - agilogy/groupable: A group function that relies on the element order and preserves it
Skip to content

A group function that relies on the element order and preserves it

Notifications You must be signed in to change notification settings

agilogy/groupable

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

groupable

Build Status Coverage Status

A group method on TraversableOnce (and subclasses) that relies on the element order and preserves it.

import com.agilogy.utils.Groupable._
val people: Seq[Person] = ???
val peopleByName:Seq[(Name,Seq[Person])] = people.group(by = _.name)

It behaves somewhat like the standard groupBy but:

  • It relies on the order of the traversable creating a group for each consecutive element that returns the same group key
  • It preserves the order of the returned groups
  • It returns a Seq of pairs key -> Seq(value) instead of a Map.
    • I could have used a ListMap, but the seq makes it evident that lookup time is linear.

The method is added to TraversableOnce and subclasses via Pimp my Library Pattern.

Installation

resolvers += "Agilogy GitLab" at "https://gitlab.com/api/v4/groups/583742/-/packages/maven"

libraryDependencies += "com.agilogy" %% "groupable" % "1.2"

Usage

Import Groupable._:

import com.agilogy.utils.Groupable._

Now, two new methods group are available on TraversableOnce[T]:

def group[GT, RT](by: ((T) => GT)): Seq[(GT, Seq[T])]
def group[GT, RT](by: (T) ⇒ GT, as: (T) ⇒ RT): Seq[(GT, Seq[RT])]

Example (from the tests):

it should "group mapping values" in {
  val s = Seq("email" -> "j@example.com", "email" -> "k@example.com", "name" -> "John")
  val res = s.group(by = _._1, as = _._2)
  assert(res === Seq("email" -> Seq("j@example.com", "k@example.com"), "name" -> Seq("John")))
}

Roadmap

  • Maybe we could use a typeclass so that not only TraversableOnes are groupables. As an example, it could be interesting for ResultSets.

Publishing

To publish this package to Agilogy's Package Registry, set the GITLAB_DEPLOY_TOKEN environment variable and then run the following command in sbt:

sbt:groupable> +publish

Copyright

Copyright 2015 Agilogy

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

About

A group function that relies on the element order and preserves it

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages