Add Cards.Remove

This commit is contained in:
Trevor Slocum 2020-05-29 09:43:55 -07:00
parent 8fff7bea9c
commit 1391a03546
5 changed files with 20 additions and 12 deletions

View File

@ -9,12 +9,6 @@ fmt:
- gofmt -l -s -e .
- exit $(gofmt -l -s -e . | wc -l)
lint:
stage: validate
script:
- go get -u golang.org/x/lint/golint
- golint -set_exit_status
vet:
stage: validate
script:

View File

@ -1,3 +1,6 @@
0.1.3:
- Add Cards.Remove
0.1.2:
- Add Parse function
- Add Permutations method to Cards

View File

@ -1,5 +1,5 @@
# joker
[![GoDoc](https://godoc.org/gitlab.com/tslocum/joker?status.svg)](https://godoc.org/gitlab.com/tslocum/joker)
[![GoDoc](https://gitlab.com/tslocum/godoc-static/-/raw/master/badge.svg)](https://docs.rocketnine.space/gitlab.com/tslocum/joker)
[![CI status](https://gitlab.com/tslocum/joker/badges/master/pipeline.svg)](https://gitlab.com/tslocum/joker/commits/master)
[![Donate](https://img.shields.io/liberapay/receives/rocketnine.space.svg?logo=liberapay)](https://liberapay.com/rocketnine.space)
@ -13,7 +13,7 @@ The following libraries extend joker:
## Documentation
Documentation is available on [godoc](https://godoc.org/gitlab.com/tslocum/joker).
Documentation is available on [godoc](https://docs.rocketnine.space/gitlab.com/tslocum/joker).
## Support

View File

@ -80,8 +80,19 @@ func (c Cards) Copy() Cards {
return cc
}
// Remove returns the supplied cards excluding the card at the specified index.
func (c Cards) Remove(i int) Cards {
// Remove returns the supplied cards with the specified card removed one time.
func (c Cards) Remove(card Card) Cards {
cc := c.Copy()
for i, searchCard := range c {
if searchCard == card {
return append(cc[:i], cc[i+1:]...)
}
}
return cc
}
// RemoveIndex returns the supplied cards excluding the card at the specified index.
func (c Cards) RemoveIndex(i int) Cards {
cc := c.Copy()
return append(cc[:i], cc[i+1:]...)
}

View File

@ -53,12 +53,12 @@ func TestCards(t *testing.T) {
t.Errorf("failed to copy cards: expected %s, got %s", c.Cards, copied)
}
removedFirst := c.Cards.Remove(0)
removedFirst := c.Cards.RemoveIndex(0)
if !reflect.DeepEqual(removedFirst, c.RemovedFirst) {
t.Errorf("failed to remove first card: expected %s, got %s", c.RemovedFirst, removedFirst)
}
removedLast := c.Cards.Remove(len(c.Cards) - 1)
removedLast := c.Cards.Remove(copied[len(c.Cards)-1])
if !reflect.DeepEqual(removedLast, c.RemovedLast) {
t.Errorf("failed to remove first card: expected %s, got %s", c.RemovedLast, removedLast)
}