github.com/searKing/golang/go@v1.2.74/container/slice/map.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  // MapFunc returns a slice consisting of the results of applying the given
    12  // function to the elements of this slice.
    13  func MapFunc(s interface{}, f func(interface{}) interface{}) interface{} {
    14  	return normalizeSlice(mapFunc(Of(s), f), s)
    15  }
    16  
    17  // mapFunc is the same as MapFunc
    18  func mapFunc(s []interface{}, f func(interface{}) interface{}) []interface{} {
    19  	object.RequireNonNil(s, "mapFunc called on nil slice")
    20  	object.RequireNonNil(f, "mapFunc called on nil callfn")
    21  
    22  	var sMapped = []interface{}{}
    23  	for _, r := range s {
    24  		sMapped = append(sMapped, f(r))
    25  	}
    26  	return sMapped
    27  }