github.com/searKing/golang/go@v1.2.74/container/slice/takewhile.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 // TakeWhileFunc returns, if this slice is ordered, a slice consisting of the longest 12 // prefix of elements taken from this slice that match the given predicate. 13 // Otherwise returns, if this slice is unordered, a slice consisting of a 14 // subset of elements taken from this slice that match the given predicate. 15 func TakeWhileFunc(s interface{}, f func(interface{}) bool) interface{} { 16 return normalizeSlice(takeWhileFunc(Of(s), f, true), s) 17 } 18 19 // takeWhileFunc is the same as TakeWhileFunc. 20 func takeWhileFunc(s []interface{}, f func(interface{}) bool, truth bool) []interface{} { 21 object.RequireNonNil(s, "takeWhileFunc called on nil slice") 22 object.RequireNonNil(f, "takeWhileFunc called on nil callfn") 23 24 var sTaken = []interface{}{} 25 for _, r := range s { 26 if f(r) == truth { 27 sTaken = append(sTaken, r) 28 continue 29 } 30 break 31 } 32 return sTaken 33 }