Giter VIP home page Giter VIP logo

go-collections's Introduction

version Go Reference commit stage Go Report Card codecov issues

Collections

Blazingly fast Collections package. Inspired by Laravel, written in Go.

Overview

go-collections offers a variety of methods and types to be used with slices and maps. Initially, its interface was based on Laravel Collections, but many more methods and functionalities will be added.

This package is still under development, there might be breaking changes introduced. Use with caution!

Pull requests are welcome. See Contributing

Generic functions

There are just functions with a slice type parameter to help the usage of slices. The main disadvantage here is the lack of piping.

func genericFunction() {
	Each(
		func(_, v int) {
			fmt.Println(v) // 2, 3, 4, 5
		},
		Map(
			func(_, v int) int {
				fmt.Println(v) // 1, 2, 3, 4
				return v + 1
			},
			[]int{1, 2, 3, 4}i
		),
	)
}

Slice collection

Slice collection is a custom slice type with embedded methods. Most of the available methods here are just calls to the base generic functions, with the advantage of piping calls instead of nesting functions.

func sliceCollection() {
	Collect(1, 2, 3, 4).
		Map(func(_, v int) int {
			fmt.Println(v) // 1, 2, 3, 4
			return v + 1
		}).
		Each(func(_, v int) {
			fmt.Println(v) // 2, 3, 4, 5
		})
}

KV (map) collections

The KV collection is a bit more complex. It uses composed of two structs: the map, holding the keys and values, and a slice of keys, used to enable the collection to be ordered in any way needed by the user. This is important due to the lack of ordering on Go maps. The usage is similar to the Slice collection, with the ability to add comparable keys.

func mapCollection() {
	CollectMap(map[string]int{"a": 1, "b": 2, "c": 3, "d": 4}).
		Sort(collections.Asc[int]()).
		Map(func(k string, v int) int {
			fmt.Printf("%s: %v\n", k, v) // a: 1, b: 2, c: 3, d: 4
			return v + 1
		}).
		Each(func(k string, v int) {
			fmt.Printf("%s: %v\n", k, v) // a: 2, b: 3, c: 4, d: 5
		})
}

Why?

Before type parameters (aka generics), Go lacked a way to do even trivial collection stuff without cumbersome boilerplate. After 1.18 was released, a myriad of possibilities was open. There are many available options right now, such as the awesome samber/lo. That said, none of the available options were what we (i.e. maintainers of this package) were looking for... So go-collection was born. The aim is to not only offer generic methods to be used with slices and maps but also offer custom types to manage collections in a pipeable fashion.

Major goals

The major goals for this package are:

  • offer a good, clear interface to interact with slices and maps
  • use no external dependencies
  • good resource usage without micro-optimizations - this is not intended to be used by real-time or performance critical applications

Limitations

Some of the methods available on the Laravel Collections are - by design - impossible to be implemented in Go. These methods were either adapted to be used in a way that made sense, or ignored completely.

Available types and methods

The complete reference of all types and their documentation is listed in their respective godoc. Below is a quick reference for each one of them.

Generic

Types

Functions

Slice collection

Most methods of the slice collection are just calls to the generic functions passing the collection as the slice argument and returning the result to allow piping. In addition, some methods are available:

Key/Value collection

Generic

Numeric

The numeric collection includes all methods from the generic kv collection and the additional methods listed bellow:

Performance

Despite the main description, this is not supposed to be a blazingly fast repository. Rather, it's intended to offer a good interface without deprecating performance. Benchmarks were made comparing the main methods to their respective raw versions using only the native data struct (e.g. slice or map). The full result will always be available as a build artifact of the respective release, but below is some of the main methods compared.

Benchmarks

TODO

Contributing

TODO

go-collections's People

Contributors

nightkari avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.