elliotchance / pie

🍕 Enjoy a slice! A utility library for dealing with slices and maps that focuses on type safety and performance.
https://pkg.go.dev/github.com/elliotchance/pie/v2
MIT License
1.96k stars 91 forks source link

Add First() #181

Closed jerome-laforge closed 1 year ago

jerome-laforge commented 1 year ago

Is it possible with pie v2 to add First[T any](ss []T)?

// First returns the first element or a zero value if there are no
// elements.
func First[T any](ss []T) T {
    if len(ss) == 0 {
        var zeroValue T

        return zeroValue
    }

    return ss[0]
}
jerome-laforge commented 1 year ago

add the same with Last[T any](ss []T)

elliotchance commented 1 year ago

Sure, that makes sense. Feel free to put in a PR for these.