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([][]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([]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([]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]
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([]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([]int{1, 5, 6}, 4)
// => false
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([]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([]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([]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([]string{"a", "a", "b", "b", "c"})
// => [a b c]
reduceSlice([]int{1, 2, 3, 4, 5}, func(n int, acc int) int {
return acc + n
}, 0)
// => 15
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([]int{1, 2, 3, 4, 5})
// => [5 4 3 2 1]
differenceSlice([]int{1, 1, 2, 2, 3, 3, 4, 5}, []int{1, 2, 4})
// => [3 3 5]
intersectionSlice([]int{1, 2, 3}, []int{0, 1, 2})
// => [1 2]
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]
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([]int{1, 2, 3})
[]bool{
set.Contains(2),
set.Contains(4),
}
// => [true false]
set := makeSet[int](3)
set.Add(1)
set.Add(2)
set.Add(3)
[]bool{
set.Contains(2),
set.Contains(4),
}
// => [true false]
pool := newSyncPool(func() int { return 1 })
v := pool.Get()
defer pool.Put(v)
v
// => 1
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]