Skip to content

Commit

Permalink
Make delAll simpler
Browse files Browse the repository at this point in the history
  • Loading branch information
andpol committed Jan 29, 2019
1 parent 07c5f2e commit ac6a71b
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 42 deletions.
23 changes: 5 additions & 18 deletions check/int32_slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,11 @@ func contains(a []int32, el int32) bool {
return false
}

func indexOf(a []int32, el int32) (int, bool) {
for i, e := range a {
if e == el {
return i, true
func delAll(a []int32, el int32) (ret []int32) {
for _, ael := range a {
if ael != el {
ret = append(ret, ael)
}
}
return -1, false
}

func delAt(a []int32, i int) []int32 {
copy(a[i:], a[i+1:])
a[len(a)-1] = 0
return a[:len(a)-1]
}

func delAll(a []int32, el int32) []int32 {
for i, ok := indexOf(a, el); ok; i, ok = indexOf(a, el) {
a = delAt(a, i)
}
return a
return
}
24 changes: 0 additions & 24 deletions check/int32_slice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,6 @@ func Test_Contains_ReturnsTrue(t *testing.T) {
}
}

// indexOf() tests

func Test_IndexOf_ValueExists(t *testing.T) {
i, result := indexOf([]int32{1, 2, 3}, 2)
if i != 1 || result != true {
t.Error()
}
}

func Test_IndexOf_ValueDoesntExists(t *testing.T) {
i, result := indexOf([]int32{1, 2, 3}, 4)
if i != -1 || result != false {
t.Error()
}
}

// delAt() tests

func Test_DelAt_ValueExists(t *testing.T) {
if !reflect.DeepEqual(delAt([]int32{1, 2, 3}, 1), []int32{1, 3}) {
t.Error()
}
}

// delAll() tests

func Test_DelAll_Single(t *testing.T) {
Expand Down

0 comments on commit ac6a71b

Please sign in to comment.