github.com/cs3org/reva/v2@v2.27.7/pkg/utils/list/list.go (about) 1 // Copyright 2018-2023 CERN 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 // In applying this license, CERN does not waive the privileges and immunities 16 // granted to it by virtue of its status as an Intergovernmental Organization 17 // or submit itself to any jurisdiction. 18 19 package list 20 21 // Map returns a list constructed by appling a function f 22 // to all items in the list l. 23 func Map[T, V any](l []T, f func(T) V) []V { 24 m := make([]V, 0, len(l)) 25 for _, e := range l { 26 m = append(m, f(e)) 27 } 28 return m 29 } 30 31 // Remove removes the element in position i from the list. 32 // It does not preserve the order of the original slice. 33 func Remove[T any](l []T, i int) []T { 34 l[i] = l[len(l)-1] 35 return l[:len(l)-1] 36 } 37 38 // TakeFirst returns the first elemen, if any, that satisfies 39 // the predicate p. 40 func TakeFirst[T any](l []T, p func(T) bool) (T, bool) { 41 for _, e := range l { 42 if p(e) { 43 return e, true 44 } 45 } 46 var z T 47 return z, false 48 } 49 50 // ToMap returns a map from l where the keys are obtainined applying 51 // the func k to the elements of l. 52 func ToMap[K comparable, T any](l []T, k func(T) K) map[K]T { 53 m := make(map[K]T, len(l)) 54 for _, e := range l { 55 m[k(e)] = e 56 } 57 return m 58 }