Awesome
🍕 github.com/elliotchance/pie
Enjoy a slice! pie
is a library of utility functions for common operations
on slices and maps.
Quick Start
If you are using (or require) Go 1.17 or below, you will have to use v1.
pie
can be used in two ways, the first is to use the regular
parameterized functions:
package main
import (
"fmt"
"strings"
"github.com/elliotchance/pie/v2"
)
func main() {
names := pie.FilterNot([]string{"Bob", "Sally", "John", "Jane"},
func(name string) bool {
return strings.HasPrefix(name, "J")
})
fmt.Println(names) // "[Bob Sally]"
}
Or, if you need to chain multiple operations you can use one of:
pie.Of
- works with any element type, but functions are limited.pie.OfOrdered
- only works with numbers and strings, but has more functions.pie.OfNumeric
- only works with numbers, but has all functions.
package main
import (
"fmt"
"strings"
"github.com/elliotchance/pie/v2"
)
func main() {
name := pie.Of([]string{"Bob", "Sally", "John", "Jane"}).
FilterNot(func(name string) bool {
return strings.HasPrefix(name, "J")
}).
Map(strings.ToUpper).
Last()
fmt.Println(name) // "SALLY"
}
You can find the full documentation here.
FAQ
What are the requirements?
pie
v2 only supports Go 1.18+. If you have an older version you can
use v1.
What are the goals of pie
?
-
Type safety. I never want to hit runtime bugs because I could pass in the wrong type, or perform an invalid type case out the other end.
-
Performance. The functions need to be as fast as native Go implementations otherwise there's no point in this library existing.
-
Nil-safe. All of the functions will happily accept nil and treat them as empty slices. Apart from less possible panics, it makes it easier to chain.
-
Immutable. Functions never modify inputs (except in cases where it would be illogical), unlike some built-ins such as
sort.Strings
.
How do I contribute a function?
Pull requests are always welcome.
Here is a comprehensive list of steps to follow to add a new function:
-
Create a new file for your function (tip: copy an existing file can be quicker). Add your implmentation and comment.
-
Create appropriate tests.
-
If your function accepts a slice, it should also be added to the
OfSlice
API (seeof.go
).
Why is the emoji a slice of pizza instead of a pie?
I wanted to pick a name for the project that was short and had an associated emoji. I liked pie, but then I found out that the pie emoji is not fully supported everywhere. I didn't want to change the name of the project to cake, but pizza pie still made sense. I'm not sure if I will change it back to a pie later.