Code Monkey home page Code Monkey logo

go-slices's Introduction

Go slices

Go Reference

Functions for slices using Go generics and (sometimes) specification pattern.

Documentation

See full documentation here.

Install

$ go get github.com/romain-jeannoutot/go-slices

Usage

Examples are based on example types, variables and functions definition.

func Every

func Every[T any](items []T, specification Specification) bool

The Every() func returns true if all items satisfy specification. Else, return false.

Example :

goslices.Every(users, NewHasFirstnameSpecification()) // true
goslices.Every(users, NewFirstnameSpecification("John")) // false

func Filter

func Filter[T any](items []T, specification Specification) []T

The Filter() func returns a new slice with items satisfying specification.

Example :

goslices.Filter(users, NewFirstnameSpecification("John")) // John Doe, John Travis
goslices.Filter(users, NewFirstnameSpecification("Mike")) // []

func Find

func Find[T any](items []T, specification Specification) T

The Find() func returns the first item in slice satisfying specification. Else, return empty value.

Example :

goslices.Find(users, NewFirstnameSpecification("John")) // John Doe
goslices.Find(users, NewFirstnameSpecification("Mike")) // Empty User

func FindIndex

func FindIndex[T any](items []T, specification Specification) int

The FindIndex() func returns index of the first item in slice satisfying specification. Else, return -1.

Example :

goslices.FindIndex(users, NewFirstnameSpecification("John")) // 0
goslices.FindIndex(users, NewFirstnameSpecification("Mike")) // -1

func Includes

func Includes[T comparable](items []T, searchElement T) bool

The Includes() func returns true if at least one item in slice is equal to searchElement. Else, return false.

Example :

goslices.Includes(jobs, "developer") // true
goslices.Includes(jobs, "postman") // false

func Map

func Map[T any, S any](items []T, callback func(item T, idx int, items []T) S) []S

The Map() func creates a new slice populated with elements returned by function called on each slice element.

Example :

goslices.Map(users, func(user User, _ int, _ []User) Employee {
  return NewEmployee(user.firstname, user.lastname, goslices.Rand(jobs))
}) // []Employee

func Rand

func Rand[T any](items []T) T

The Rand() func returns a random element from slice. Empty value if slice is empty.

Example :

goslices.Rand(jobs) // string

func Reduce

func Reduce[T any, S any](items []T, callback func(acc S, item T, idx int, items []T) S, initialValue S) S

The Reduce() func executes a reducer function on each slice element, passing in the return value from the calculation on the preceding element.

Example :

goslices.Reduce(users, func(acc map[string]int, user User, _ int, _ []User) map[string]int {
  acc[user.firstname]++
  return acc
}, map[string]int{}) // map[Aliyah:1 Arnas:1 John:2 Marcos:1]

func Some

func Some[T any](items []T, specification Specification) bool

The Some() func returns true if at least one item satisfy specification. Else, return false.

Example :

goslices.Some(users, NewFirstnameSpecification("John")) // true
goslices.Some(users, NewFirstnameSpecification("Mike")) // false

Licence

This package is licensed under MIT license. See LICENSE for details.

go-slices's People

Contributors

romain-jeannoutot avatar

Stargazers

 avatar  avatar  avatar  avatar  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.