github.com/synesissoftware/ANGoLS@v0.0.0-20190330004400-955d82dbf73b/select_slice.go (about) 1 /* ///////////////////////////////////////////////////////////////////////// 2 * File: select_slice.go 3 * 4 * Purpose: SelectSlice*() 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 ) 46 47 // ///////////////////////////////////////////////////////////////////////// 48 // Select* 49 50 func SelectSliceOfInt(input_slice []int, selector func(index int, input_item int) (bool, error)) ([]int, error) { 51 52 input_len := len(input_slice) 53 result := make([]int, input_len) 54 result_len := 0 55 56 for i, v := range input_slice { 57 58 selected, err := selector(i, v) 59 if err != nil { 60 61 return nil, err 62 } 63 64 if selected { 65 66 result[result_len] = v 67 result_len++ 68 } 69 } 70 71 return result[0:result_len], nil 72 } 73 74 func SelectSliceOfUInt(input_slice []uint, selector func(index int, input_item uint) (bool, error)) ([]uint, error) { 75 76 input_len := len(input_slice) 77 result := make([]uint, input_len) 78 result_len := 0 79 80 for i, v := range input_slice { 81 82 selected, err := selector(i, v) 83 if err != nil { 84 85 return nil, err 86 } 87 88 if selected { 89 90 result[result_len] = v 91 result_len++ 92 } 93 } 94 95 return result[0:result_len], nil 96 } 97 98 func SelectSliceOfString(input_slice []string, selector func(index int, input_item string) (bool, error)) ([]string, error) { 99 100 input_len := len(input_slice) 101 result := make([]string, input_len) 102 result_len := 0 103 104 for i, v := range input_slice { 105 106 selected, err := selector(i, v) 107 if err != nil { 108 109 return nil, err 110 } 111 112 if selected { 113 114 result[result_len] = v 115 result_len++ 116 } 117 } 118 119 return result[0:result_len], nil 120 } 121 122 /* ///////////////////////////// end of file //////////////////////////// */ 123 124