go-generics

command module
v0.0.0-...-0f04b6a Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2021 License: MIT Imports: 14 Imported by: 0

README

Go Generics By Example

Slice utils

mapSlice
mapSlice([]int{1, 2, 3, 4}, func(v int) int {
	return v * 2
})
// => [2 4 6 8]
mapSlice([]string{"foobar", "baz"}, func(v string) int {
	return len(v)
})
// => [6 3]
flatMapSlice
flatMapSlice([][]int{{1, 2}, {3, 4}}, func(v []int) []int {
	results := []int{}
	results = append(results, v...)
	results = append(results, 100)
	return results
})
// => [1 2 100 3 4 100]
filterSlice
filterSlice([]int{1, 2, 3, 4, 5, 6}, func(v int) bool {
	return v/2 == 0
})
// => [1]
type User struct {
	ID    int
	Name  string
	Admin bool
}
users := []*User{
	{ID: 1, Name: "Alice"},
	{ID: 2, Name: "Bob", Admin: true},
	{ID: 3, Name: "Carol"},
}
nonAdminUsers := filterNotSlice(
	users,
	func(user *User) bool { return user.Admin },
)
mapSlice(
	nonAdminUsers,
	func(user *User) string { return user.Name },
)
// => [Alice Carol]
filterMapSlice
filterMapSlice([]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, func(v int) (int, bool) {
	return v * 2, v%2 == 0
})
// => [4 8 12 16 20]
findSlice
type User struct {
	Name string
	Age  int
}
teens, _ := findSlice([]*User{{Name: "Alice", Age: 18}, {Name: "Bob", Age: 27}, {Name: "Carol", Age: 24}}, func(u *User) bool {
	return u.Age >= 20
})
teens
// => &{Name:Bob Age:27}
indexSlice / lastIndexSlice
indexSlice([]string{"foo", "bar", "baz"}, func(v string) bool {
	return strings.HasPrefix(v, "b")
})
// => 1
indexSlice([]string{"foo", "bar", "baz"}, func(v string) bool {
	return strings.HasPrefix(v, "a")
})
// => -1
lastIndexSlice([]string{"foo", "bar", "baz"}, func(v string) bool {
	return strings.HasPrefix(v, "b")
})
// => 2
lastIndexSlice([]string{"foo", "bar", "baz"}, func(v string) bool {
	return strings.HasPrefix(v, "a")
})
// => -1
containsSlice
containsSlice([]int{1, 5, 6}, 4)
// => false
allSlice
allSlice([]string{"ant", "bear", "cat"}, func(v string) bool {
	return len(v) >= 3
})
// => true
allSlice([]string{"ant", "bear", "cat"}, func(v string) bool {
	return len(v) >= 4
})
// => false
someSlice
someSlice([]string{"ant", "bear", "cat"}, func(v string) bool {
	return len(v) >= 4
})
// => true
someSlice([]string{"ant", "bear", "cat"}, func(v string) bool {
	return len(v) >= 5
})
// => false
maxSlice / minSlice
maxSlice([]string{"albatross", "dog", "horse"}, func(v string) int {
	return len(v)
})
// => albatross
minSlice([]string{"albatross", "dog", "horse"}, func(v string) int {
	return len(v)
})
// => dog
compactSlice
compactSlice([]string{"foo", "bar", "", "baz", ""})
// => [foo bar baz]
type User struct {
	ID   int
	Name string
}
mapSlice(
	compactSlice([]User{{ID: 1, Name: "Alice"}, {}, {ID: 2, Name: "Bob"}}),
	func(user User) string { return user.Name },
)
// => [Alice Bob]
uniqSlice
uniqSlice([]string{"a", "a", "b", "b", "c"})
// => [a b c]
reduceSlice
reduceSlice([]int{1, 2, 3, 4, 5}, func(n int, acc int) int {
	return acc + n
}, 0)
// => 15
groupSliceBy
groupSliceBy([]int{1, 2, 3, 4, 5, 6}, func(v int) int { return v % 3 })
// => map[0:[3 6] 1:[1 4] 2:[2 5]]
reverseSlice
reverseSlice([]int{1, 2, 3, 4, 5})
// => [5 4 3 2 1]
differenceSlice
differenceSlice([]int{1, 1, 2, 2, 3, 3, 4, 5}, []int{1, 2, 4})
// => [3 3 5]
intersectionSlice
intersectionSlice([]int{1, 2, 3}, []int{0, 1, 2})
// => [1 2]

Map utils

mapKeys / mapValues
mapKeys(map[string]int{"foo": 1, "bar": 2, "baz": 3})
// => [foo bar baz]
mapValues(map[string]int{"foo": 1, "bar": 2, "baz": 3})
// => [1 2 3]
mapTransformKeys / mapTransformValues
transformMapKeys(map[string]int{"foo": 1, "bar": 2, "baz": 3}, func(k string) string {
	return k + k
})
// => map[barbar:2 bazbaz:3 foofoo:1]
transformMapValues(map[string]int{"foo": 1, "bar": 2, "baz": 3}, func(v int) int {
	return v * 2
})
// => map[bar:4 baz:6 foo:2]

Set

setFromSlice
set := setFromSlice([]int{1, 2, 3})
[]bool{
	set.Contains(2),
	set.Contains(4),
}
// => [true false]
makeSet
set := makeSet[int](3)
set.Add(1)
set.Add(2)
set.Add(3)
[]bool{
	set.Contains(2),
	set.Contains(4),
}
// => [true false]

Sync

syncMap
syncPool
pool := newSyncPool(func() int { return 1 })
v := pool.Get()
defer pool.Put(v)
v
// => 1
singleflight

Sqlx utils

SqlxSelect
type User struct {
	ID   uint64
	Name string
}
ctx := context.Background()
users, err := sqlxSelect[*User](ctx, db, "SELECT * FROM users")
if err != nil {
	panic(err)
}
users
// => [0xc00048d500]

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

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