github.com/synesissoftware/ANGoLS@v0.0.0-20190330004400-955d82dbf73b/collect_slice.go (about) 1 /* ///////////////////////////////////////////////////////////////////////// 2 * File: collect_slice.go 3 * 4 * Purpose: CollectSlice*() functions 5 * 6 * Created: 1st March 2019 7 * Updated: 11th March 2019 8 * 9 * Home: http://github.com/synesissoftware/ANGOLS 10 * 11 * Copyright (c) 2019, Matthew Wilson and Synesis Software 12 * All rights reserved. 13 * 14 * Redistribution and use in source and binary forms, with or without 15 * modification, are permitted provided that the following conditions are 16 * met: 17 * 18 * - Redistributions of source code must retain the above copyright notice, 19 * this list of conditions and the following disclaimer. 20 * - Redistributions in binary form must reproduce the above copyright 21 * notice, this list of conditions and the following disclaimer in the 22 * documentation and/or other materials provided with the distribution. 23 * - Neither the names of Matthew Wilson, Synesis Software nor 24 * the names of any contributors may be used to endorse or promote products 25 * derived from this software without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 28 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 29 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 31 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 32 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 33 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 34 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 35 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 36 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 37 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 38 * 39 * ////////////////////////////////////////////////////////////////////// */ 40 41 package angols 42 43 import ( 44 45 "fmt" 46 "reflect" 47 ) 48 49 // ///////////////////////////////////////////////////////////////////////// 50 // Collect* 51 52 // This function maps an input slice of arbitrary type to an output slice 53 // of type []interface{} 54 func CollectSlice(input_slice interface{}, fn func(input_item interface{}) (interface{}, error)) (interface{}, error) { 55 56 sl_t := reflect.TypeOf(input_slice) 57 58 if reflect.Slice != sl_t.Kind() { 59 60 msg := fmt.Sprintf("CollectSlice() called with input_slice of type %T; slice required", input_slice) 61 62 panic(msg) 63 } 64 65 sl_v := reflect.ValueOf(input_slice) 66 len := sl_v.Len() 67 68 result := make([]interface{}, len) 69 70 for i := 0; len != i; i++ { 71 72 p := sl_v.Index(i) 73 v := p.Interface() 74 75 r, e := fn(v) 76 if e != nil { 77 78 return nil, e 79 } 80 81 result[i] = r 82 } 83 84 return result, nil 85 } 86 87 // CollectSliceOfInt 88 func CollectSliceOfInt(input_slice []int, fn func(input_item int) int) (result_slice []int) { 89 90 result_slice = make([]int, len(input_slice)) 91 92 for i, v := range input_slice { 93 94 result := fn(v) 95 96 result_slice[i] = result 97 } 98 99 return 100 } 101 102 // CollectSliceOfFloat64 103 func CollectSliceOfFloat64(input_slice []float64, fn func(input_item float64) float64) (result_slice []float64) { 104 105 result_slice = make([]float64, len(input_slice)) 106 107 for i, v := range input_slice { 108 109 result := fn(v) 110 111 result_slice[i] = result 112 } 113 114 return 115 } 116 117 // CollectSliceOfString 118 func CollectSliceOfString(input_slice []string, fn func(input_item string) string) (result_slice []string) { 119 120 result_slice = make([]string, len(input_slice)) 121 122 for i, s := range input_slice { 123 124 result_slice[i] = fn(s) 125 } 126 127 return 128 } 129 130 /* ///////////////////////////// end of file //////////////////////////// */ 131 132