Skip to content

A toolset for working with slices

License

Notifications You must be signed in to change notification settings

PlanitarInc/slc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

slc GoDoc CI Coverage Status

A generics-based toolset for working with slices.

Advantages:

  • Simple and clear API
  • Compatible with GO standard library

Yet to be done:

  • Improve performance. Some functions are not optimal and should not be used in critical paths.

Usage

Download the package

go get github.com/PlanitarInc/slc

Example

Playground link: https://go.dev/play/p/qu0t390uxt_p

package main

import (
	"fmt"

	"github.com/PlanitarInc/slc"
)

func main() {
	nums := []int{1, 2, 3, 4, 5}

	fmt.Println(slc.Includes(nums, 4)) // true
	fmt.Println(slc.Includes(nums, 9)) // false

	fmt.Println(slc.Every(nums, func(n int) bool {
		return n > 0
	})) // true
	fmt.Println(slc.Every(nums, func(n int) bool {
		return n%2 == 1
	})) // false

	type C struct {
		S string
		N int
	}

	objs := []C{
		{"a", 9},
		{"b", 8},
		{"c", 7},
	}
	fmt.Println(slc.Filter(objs, func(o C) bool {
		return o.N > 7
	})) // [{a 9}, {b 8}]
	fmt.Println(slc.FilterOut(objs, func(o C) bool {
		return o.S == "b"
	})) // [{a 9}, {c 7}]
}

Development

Feel free to modify the library and submit your changes. The main requirements are some reasonable asks for a library:

  • 100% test coverage
  • 100% documentation for public API (types, functions, constants)
  • readable code