Skip to content

Commit

Permalink
Add First() and Last() in chain operation (#183)
Browse files Browse the repository at this point in the history
This also improves example in the README
  • Loading branch information
jerome-laforge authored Dec 12, 2022
1 parent 3ba5af1 commit ab43b76
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Or, if you need to chain multiple operations you can use one of:
- [`pie.OfOrdered`](https://pkg.go.dev/github.com/elliotchance/pie/v2#OfOrdered) - only works with numbers and strings, but has more functions.
- [`pie.OfNumeric`](https://pkg.go.dev/github.com/elliotchance/pie/v2#OfNumeric) - only works with numbers, but has all functions.

[Run this program](https://go.dev/play/p/cDLBYzKJ9ld)
[Run this program](https://go.dev/play/p/4IhVbw0koxg)

```go
package main
Expand All @@ -67,7 +67,7 @@ func main() {
return strings.HasPrefix(name, "J")
}).
Map(strings.ToUpper).
LastOr("")
Last()

fmt.Println(name) // "SALLY"
}
Expand Down
8 changes: 8 additions & 0 deletions v2/of.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ func (o OfSlice[T]) FindFirstUsing(fn func(value T) bool) int {
return FindFirstUsing(o.Result, fn)
}

func (o OfSlice[T]) First() T {
return First(o.Result)
}

func (o OfSlice[T]) FirstOr(defaultValue T) T {
return FirstOr(o.Result, defaultValue)
}
Expand All @@ -57,6 +61,10 @@ func (o OfSlice[T]) Insert(index int, values ...T) OfSlice[T] {
return OfSlice[T]{Insert(o.Result, index, values...)}
}

func (o OfSlice[T]) Last() T {
return Last(o.Result)
}

func (o OfSlice[T]) LastOr(defaultValue T) T {
return LastOr(o.Result, defaultValue)
}
Expand Down
8 changes: 8 additions & 0 deletions v2/of_numeric.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ func (o OfNumericSlice[T]) FindFirstUsing(fn func(value T) bool) int {
return FindFirstUsing(o.Result, fn)
}

func (o OfNumericSlice[T]) First() T {
return First(o.Result)
}

func (o OfNumericSlice[T]) FirstOr(defaultValue T) T {
return FirstOr(o.Result, defaultValue)
}
Expand Down Expand Up @@ -124,6 +128,10 @@ func (o OfNumericSlice[T]) JSONStringIndent(prefix, indent string) string {
return JSONStringIndent(o.Result, prefix, indent)
}

func (o OfNumericSlice[T]) Last() T {
return Last(o.Result)
}

func (o OfNumericSlice[T]) LastOr(defaultValue T) T {
return LastOr(o.Result, defaultValue)
}
Expand Down
8 changes: 8 additions & 0 deletions v2/of_ordered.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ func (o OfOrderedSlice[T]) FindFirstUsing(fn func(value T) bool) int {
return FindFirstUsing(o.Result, fn)
}

func (o OfOrderedSlice[T]) First() T {
return First(o.Result)
}

func (o OfOrderedSlice[T]) FirstOr(defaultValue T) T {
return FirstOr(o.Result, defaultValue)
}
Expand Down Expand Up @@ -120,6 +124,10 @@ func (o OfOrderedSlice[T]) JSONStringIndent(prefix, indent string) string {
return JSONStringIndent(o.Result, prefix, indent)
}

func (o OfOrderedSlice[T]) Last() T {
return Last(o.Result)
}

func (o OfOrderedSlice[T]) LastOr(defaultValue T) T {
return LastOr(o.Result, defaultValue)
}
Expand Down

0 comments on commit ab43b76

Please sign in to comment.