godash

package module
v1.0.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 3, 2024 License: MIT Imports: 2 Imported by: 0

README

godash

Go Reference

hatena/godash is a minimal wrapper of https://github.com/samber/lo.

Motivation

https://github.com/samber/lo is a nice library, but in some cases teams do not want to have all of the helper functions available. Some helper functions defined in samber/lo are actually already available as part of the official slices package.

hatena/godash is meant to only provide a small portion of samber/lo's functions:

  • functions that are not available via Go's standard library
  • functions that are not "too much", such as "Conditional helpers" or "Error handling"

Usage

Install the package to your repo with go get.

go get -u github.com/hatena/godash

Sample usage:

package main

import (
	"fmt"

	"github.com/hatena/godash"
)

func main() {
	integers := []int{0, 1, 1, 3, 2, 1, 1, 0}
	fmt.Println(godash.Chunk(integers, 3))
}

Check out more examples at godash package - github.com/hatena/godash - Go Packages.

Since this package aims to only present a subset of functions from samber/lo, it is recommended to add linter settings to deny importing directly from samber/lo. (If you want to use samber/lo directly, then there is no meaning to use hatena/godash).

Below is a minimal .golangci.yml configuration to deny samber/lo imports.

linters:
  enable:
    - depguard

linters-settings:
  depguard:
    rules:
      deny-samber-lo:
        deny:
          - pkg: "github.com/samber/lo"
            desc: Use github.com/hatena/godash instead.

Once you have .golangci.yml, make sure to run golangci-lint via CI. Below is an example for GitHub Actions.

name: CI

on:
  push:
    branches: main
  pull_request:

permissions:
  contents: read
  pull-requests: read

jobs:
  lint:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-go@v4
        with:
          go-version-file: "go.mod"
      - name: golangci-lint
        uses: golangci/golangci-lint-action@v3
        with:
          version: v1.54.2
          only-new-issues: true

See Introduction | golangci-lint for more details.

Documentation

Overview

Package godash implements slice/map related methods and is a wrapper of the github.com/samber/lo package.

This package focuses on only presenting a subset of functions from github.com/samber/lo, in order to make sure that the user takes advantage of Go's standard methods from the slice package etc. If any of the functions presented here gets included in the standard library, that function will be marked deprecated (the function will not get removed, but will point the user to the standard library).

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Associate added in v1.0.3

func Associate[T any, K comparable, V any](collection []T, transform func(item T) (K, V)) map[K]V
Example
package main

import (
	"fmt"

	"github.com/hatena/godash"
)

func main() {
	type User struct {
		name string
		age  int
	}
	users := []User{
		{"foo", 20},
		{"bar", 25},
		{"baz", 40},
	}
	fmt.Println(godash.Associate(users, func(u User) (string, int) {
		return u.name, u.age
	}))
}
Output:

map[bar:25 baz:40 foo:20]

func Chunk

func Chunk[T any](collection []T, size int) [][]T

Chunk receives the collection and chunks it into a slice of slices each of the given size.

Example
package main

import (
	"fmt"

	"github.com/hatena/godash"
)

func main() {
	integers := []int{0, 1, 1, 3, 2, 1, 1, 0}
	fmt.Println(godash.Chunk(integers, 3))
}
Output:

[[0 1 1] [3 2 1] [1 0]]

func EveryBy

func EveryBy[T any](collection []T, predicate func(item T) bool) bool

EveryBy returns whether or not all the values within the collection meet the predicate.

Example
package main

import (
	"fmt"

	"github.com/hatena/godash"
)

func main() {
	type User struct {
		name string
		age  int
	}
	users := []User{
		{"foo", 20},
		{"bar", 25},
		{"baz", 40},
	}
	fmt.Println(godash.EveryBy(users, func(u User) bool {
		return u.age >= 20
	}))
}
Output:

true

func Filter

func Filter[V any](collection []V, predicate func(item V, index int) bool) []V

Filter iterates through the collection and returns a slice with only the values that match the predicate.

Example
package main

import (
	"fmt"

	"github.com/hatena/godash"
)

func main() {
	integers := []int{-2, 0, 19, 5, 42}
	fmt.Println(godash.Filter(integers, func(x int, _ int) bool {
		return x%2 == 0
	}))
}
Output:

[-2 0 42]

func Find

func Find[T any](collection []T, predicate func(item T) bool) (T, bool)

Find returns the first value that meets the predicate, as well as whether or not a match was found.

Example
package main

import (
	"fmt"

	"github.com/hatena/godash"
)

func main() {
	type User struct {
		name string
		age  int
	}
	users := []User{
		{"foo", 20},
		{"bar", 25},
		{"baz", 40},
	}
	fmt.Println(godash.Find(users, func(u User) bool {
		return u.age > 24
	}))
}
Output:

{bar 25} true

func FlatMap added in v1.0.4

func FlatMap[T any, R any](collection []T, iteratee func(item T, index int) []R) []R

FlatMap iterates through the collection and returns a slice with the values converted through the iteratee and flattened.

Example
package main

import (
	"fmt"

	"github.com/hatena/godash"
)

func main() {
	integers := []int{0, 1, 2, 3}
	fmt.Println(godash.FlatMap(integers, func(x int, _ int) []int {
		return []int{2 * x, 2*x + 1}
	}))
}
Output:

[0 1 2 3 4 5 6 7]

func Flatten

func Flatten[T any](collection [][]T) []T

Flatten flattens a slice of slices to a single slice.

Example
package main

import (
	"fmt"

	"github.com/hatena/godash"
)

func main() {
	integers := [][]int{
		{1, 2, 5, 10},
		{9, 8, 1},
		{0},
		{42, 3},
	}
	fmt.Println(godash.Flatten(integers))
}
Output:

[1 2 5 10 9 8 1 0 42 3]

func GroupBy

func GroupBy[T any, U comparable](collection []T, iteratee func(item T) U) map[U][]T

GroupBy groups the values of the collection using the iteratee, and returns it as a map.

Example
package main

import (
	"fmt"

	"github.com/hatena/godash"
)

func main() {
	type User struct {
		id   int
		name string
	}
	users := []User{
		{1, "foo"},
		{2, "foobar"},
		{3, "foobar"},
		{4, "foo"},
		{5, "bar"},
	}
	fmt.Println(godash.GroupBy(users, func(u User) string {
		return u.name
	}))
}
Output:

map[bar:[{5 bar}] foo:[{1 foo} {4 foo}] foobar:[{2 foobar} {3 foobar}]]

func KeyBy

func KeyBy[K comparable, V any](collection []V, iteratee func(item V) K) map[K]V

KeyBy iterates through the collection and returns a map with the key generated via the iteratee.

Example
package main

import (
	"fmt"

	"github.com/hatena/godash"
)

func main() {
	type User struct {
		id   int
		name string
	}
	users := []User{
		{1, "foo"},
		{2, "foobar"},
		{3, "foobar"},
		{4, "foo"},
		{5, "bar"},
	}
	fmt.Println(godash.KeyBy(users, func(u User) int {
		return u.id
	}))
}
Output:

map[1:{1 foo} 2:{2 foobar} 3:{3 foobar} 4:{4 foo} 5:{5 bar}]

func Keys

func Keys[K comparable, V any](in map[K]V) []K

Keys returns the keys of the map as a slice.

Example
package main

import (
	"fmt"
	"slices"

	"github.com/hatena/godash"
)

func main() {
	m := map[int]string{
		1: "foo",
		2: "bar",
		3: "baz",
	}
	keys := godash.Keys(m)
	slices.Sort(keys)
	fmt.Println(keys)
}
Output:

[1 2 3]

func Map

func Map[T any, R any](collection []T, iteratee func(item T, index int) R) []R

Map iterates through the collection and returns a slice with the values converted though the iteratee.

Example
package main

import (
	"fmt"

	"github.com/hatena/godash"
)

func main() {
	integers := []int{-2, 0, 19, 5, 42}
	fmt.Println(godash.Map(integers, func(x int, _ int) int {
		return x * 2
	}))
}
Output:

[-4 0 38 10 84]

func NoneBy

func NoneBy[T any](collection []T, predicate func(item T) bool) bool

NoneBy returns whether or not all the values within the collection do not meet the predicate.

Example
package main

import (
	"fmt"

	"github.com/hatena/godash"
)

func main() {
	type User struct {
		name string
		age  int
	}
	users := []User{
		{"foo", 20},
		{"bar", 25},
		{"baz", 40},
	}
	fmt.Println(godash.NoneBy(users, func(u User) bool {
		return u.age > 40
	}))
}
Output:

true

func PartitionBy

func PartitionBy[T any, K comparable](collection []T, iteratee func(item T) K) [][]T

PartitionBy partitions the collection by the iteratee. This is similar to Chunk, but instead of a given size, a function can be applied. Use GroupBy if you want a map of slices.

Example
package main

import (
	"fmt"

	"github.com/hatena/godash"
)

func main() {
	type User struct {
		id   int
		name string
	}
	users := []User{
		{1, "foo"},
		{2, "foobar"},
		{3, "foobar"},
		{4, "foo"},
		{5, "bar"},
	}
	fmt.Println(godash.PartitionBy(users, func(u User) string {
		return u.name
	}))
}
Output:

[[{1 foo} {4 foo}] [{2 foobar} {3 foobar}] [{5 bar}]]

func PickByKeys added in v1.0.5

func PickByKeys[K comparable, V any, Map ~map[K]V](in Map, keys []K) Map

PickByKeys returns same map type filtered by given keys.

Example
package main

import (
	"fmt"

	"github.com/hatena/godash"
)

func main() {
	kv := map[string]int{"foo": 1, "bar": 2, "baz": 3}
	result := godash.PickByKeys(kv, []string{"foo", "baz"})
	fmt.Printf("%v", result)
}
Output:

map[baz:3 foo:1]

func Reduce

func Reduce[T any, R any](collection []T, accumulator func(agg R, item T, index int) R, initial R) R

Reduce iterates through the collection and reduces it to one value, using the accumulator.

Example
package main

import (
	"fmt"

	"github.com/hatena/godash"
)

func main() {
	integers := []int{-2, 0, 19, 5, 42}
	fmt.Println(godash.Reduce(integers, func(agg int, x int, _ int) int {
		return agg + x
	}, 0))
}
Output:

64

func SomeBy added in v1.0.4

func SomeBy[T any](collection []T, predicate func(item T) bool) bool

SomeBy returns whether or not any of the values within the collection meet the predicate.

Example
package main

import (
	"fmt"

	"github.com/hatena/godash"
)

func main() {
	type User struct {
		name string
		age  int
	}
	users := []User{
		{"foo", 20},
		{"bar", 25},
		{"baz", 40},
	}
	fmt.Println(godash.SomeBy(users, func(u User) bool {
		return u.age >= 30
	}))
}
Output:

true

func Sum

func Sum[T constraints.Float | constraints.Integer | constraints.Complex](collection []T) T

Sum returns a sum of the values within the slice. The values of the slice need to be numbers.

Example
package main

import (
	"fmt"

	"github.com/hatena/godash"
)

func main() {
	numbers := []float32{1, 5, 2, 3.2, 42}
	fmt.Println(godash.Sum(numbers))
}
Output:

53.2

func SumBy

func SumBy[T any, R constraints.Float | constraints.Integer | constraints.Complex](collection []T, iteratee func(item T) R) R

SumBy returns a sum of the values within the slice, added using the specified iteratee function. The values of the slice need to be numbers.

Example
package main

import (
	"fmt"

	"github.com/hatena/godash"
)

func main() {
	type User struct {
		name string
		age  int
	}
	users := []User{
		{"foo", 20},
		{"bar", 25},
		{"baz", 40},
	}
	fmt.Println(godash.SumBy(users, func(u User) int {
		return u.age
	}))
}
Output:

85

func Uniq

func Uniq[T comparable](collection []T) []T

Uniq returns a slice with only unique values.

Example
package main

import (
	"fmt"

	"github.com/hatena/godash"
)

func main() {
	integers := []int{0, 1, 1, 3, 2, 1, 1, 0}
	fmt.Println(godash.Uniq(integers))
}
Output:

[0 1 3 2]

func UniqBy

func UniqBy[T any, U comparable](collection []T, iteratee func(item T) U) []T

UniqBy returns a slice with only unique values. The uniqueness is determined by the iteratee.

Example
package main

import (
	"fmt"

	"github.com/hatena/godash"
)

func main() {
	type User struct {
		id   int
		name string
	}
	users := []User{
		{1, "foo"},
		{2, "foobar"},
		{2, "foobar"},
		{3, "foo"},
	}
	fmt.Println(godash.UniqBy(users, func(u User) int {
		return u.id
	}))
}
Output:

[{1 foo} {2 foobar} {3 foo}]

func Values

func Values[K comparable, V any](in map[K]V) []V

Values returns the values of the map as a slice.

Example
package main

import (
	"fmt"
	"slices"

	"github.com/hatena/godash"
)

func main() {
	m := map[int]string{
		1: "foo",
		2: "bar",
		3: "baz",
	}
	values := godash.Values(m)
	slices.Sort(values)
	fmt.Println(values)
}
Output:

[bar baz foo]

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL