Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added EachWithIndex to Slice

  • Loading branch information...
commit 74cc65058ba4c9f63fc2626ecaba90eaa1454e48 1 parent 22d3e95
@feyeleanor authored
Showing with 15 additions and 0 deletions.
  1. +6 −0 slice.go
  2. +9 −0 slice_test.go
View
6 slice.go
@@ -35,6 +35,12 @@ func (s Slice) Each(f func(interface{})) {
}
}
+func (s Slice) EachWithIndex(f func(int, interface{})) {
+ for i, v := range s {
+ f(i, v)
+ }
+}
+
func (s Slice) String() (t string) {
for _, v := range s {
if len(t) > 0 {
View
9 slice_test.go
@@ -96,6 +96,15 @@ func TestSliceEach(t *testing.T) {
})
}
+func TestSliceEachWithIndex(t *testing.T) {
+ c := SList(0, 1, 2, 3, 4, 5, 6, 7, 8 ,9)
+ c.EachWithIndex(func(index int, i interface{}) {
+ if i != index {
+ t.Fatalf("element %v erroneously reported as %v", index, i)
+ }
+ })
+}
+
func TestSliceBlockCopy(t *testing.T) {
ConfirmBlockCopy := func(s *Slice, destination, source, count int, r *Slice) {
s.BlockCopy(destination, source, count)
Please sign in to comment.
Something went wrong with that request. Please try again.