Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add slice.HeadTail #128

Merged
merged 1 commit into from
Feb 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,9 @@ Some utilities that are convenient when working with slices.
- `slice.Take[A any](as []A, n int) []A` - Take returns at most n elements.
- `slice.Drop[A any](as []A, n int) []A` - Drop removes initial n elements.
- `slice.Reverse[A any](as []A) (res []A)` - Reverse creates a new slice with elements reversed.
- `slice.HeadTail[A any](as []A) (A, []A)` - HeadTail returns head, tail. Panics with ErrHeadOfEmptySlice when slice is empty.
- `slice.Head[A any](as []A) (a A)` - Head returns head of the slice. Panics with `ErrHeadOfEmptySlice` when slice is empty.
- `slice.Tail[A any](as []A) []A` - Tail returns tail of the slice.

We can convert a slice to a set:

Expand Down
28 changes: 28 additions & 0 deletions slice/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
package slice

import (
"errors"

"github.com/primetalk/goio/fun"
"github.com/primetalk/goio/maps"
"github.com/primetalk/goio/option"
Expand Down Expand Up @@ -298,3 +300,29 @@ func Union[A comparable](as []A, as2 []A) (res []A) {
sr := ToSet(as)
return append(as, FilterNot(as2, set.Contains(sr))...)
}

// ErrHeadOfEmptySlice is thrown when there's an attempt to take head of an empty slice.
var ErrHeadOfEmptySlice = errors.New("head of empty slice")

// HeadTail returns head, tail.
// panics with ErrHeadOfEmptySlice when slice is empty.
func HeadTail[A any](as []A) (A, []A) {
switch len(as) {
case 0:
panic(ErrHeadOfEmptySlice)
default:
return as[0], as[1:]
}
}

// Head returns head of the slice.
// panics with ErrHeadOfEmptySlice when slice is empty.
func Head[A any](as []A) (a A) {
a, _ = HeadTail(as)
return
}

// Tail returns tail of the slice.
func Tail[A any](as []A) []A {
return Drop(as, 1)
}
25 changes: 25 additions & 0 deletions slice/slice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,28 @@ func TestUnion(t *testing.T) {
znats15 := slice.Range(0, 15)
assert.ElementsMatch(t, znats15, slice.Union(znats10, znats515))
}

func TestHeadTail(t *testing.T) {
znats5 := slice.Range(0, 5)
z, nats5 := slice.HeadTail(znats5)
assert.Equal(t, 0, z)
assert.ElementsMatch(t, slice.Range(1, 5), nats5)
}

func TestHeadTailPanic(t *testing.T) {
defer func() {
if r := recover(); r != slice.ErrHeadOfEmptySlice {
t.Errorf("expected panic ErrHeadOfEmptySlice")
}
}()
znats5 := slice.Range(0, 0)
slice.HeadTail(znats5)
}

func TestHeadAndTail(t *testing.T) {
znats5 := slice.Range(0, 5)
z := slice.Head(znats5)
nats5 := slice.Tail(znats5)
assert.Equal(t, 0, z)
assert.ElementsMatch(t, slice.Range(1, 5), nats5)
}