github.com/searKing/golang/go@v1.2.74/container/slice/peek.go (about)

     1  // Copyright 2020 The searKing Author. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package slice
     6  
     7  import (
     8  	"github.com/searKing/golang/go/util/object"
     9  )
    10  
    11  // PeekFunc returns a slice consisting of the elements of this slice, additionally
    12  // performing the provided action on each element as elements are consumed
    13  // from the resulting slice.
    14  func PeekFunc(s interface{}, f func(interface{})) interface{} {
    15  	return normalizeSlice(peekFunc(Of(s), f), s)
    16  
    17  }
    18  
    19  // peekFunc is the same as PeekFunc.
    20  func peekFunc(s []interface{}, f func(interface{})) []interface{} {
    21  	object.RequireNonNil(s, "peekFunc called on nil slice")
    22  	object.RequireNonNil(f, "peekFunc called on nil callfn")
    23  
    24  	for _, r := range s {
    25  		f(r)
    26  	}
    27  	return s
    28  }