github.com/searKing/golang/go@v1.2.74/container/slice/concat.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  	"reflect"
     9  
    10  	"github.com/searKing/golang/go/util/object"
    11  )
    12  
    13  // ConcatFunc creates a lazily concatenated stream whose elements are all the
    14  // elements of the first stream followed by all the elements of the
    15  // second stream.  The resulting stream is ordered if both
    16  // of the input streams are ordered, and parallel if either of the input
    17  // streams is parallel.  When the resulting stream is closed, the close
    18  // handlers for both input streams are invoked.
    19  func ConcatFunc(s1, s2 interface{}) interface{} {
    20  	return concatFunc(s1, s2)
    21  }
    22  
    23  // concatFunc is the same as ConcatFunc
    24  func concatFunc(s1, s2 interface{}) interface{} {
    25  	object.RequireNonNil(s1, "concatFunc called on nil slice")
    26  	object.RequireNonNil(s2, "concatFunc called on nil callfn")
    27  	typ1 := reflect.ValueOf(s1).Kind()
    28  	typ2 := reflect.ValueOf(s2).Kind()
    29  	object.RequireEqual(typ1, typ2)
    30  	if typ1 == reflect.String {
    31  		return s1.(string) + s2.(string)
    32  	}
    33  
    34  	var sConcated = []interface{}{}
    35  	for _, r := range Of(s1) {
    36  		sConcated = append(sConcated, r)
    37  	}
    38  	for _, r := range Of(s2) {
    39  		sConcated = append(sConcated, r)
    40  	}
    41  	return sConcated
    42  }