github.com/synesissoftware/ANGoLS@v0.0.0-20190330004400-955d82dbf73b/generate_slice.go (about)

     1  /* /////////////////////////////////////////////////////////////////////////
     2   * File:        generate_slice.go
     3   *
     4   * Purpose:     GenerateSlice*() 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  	"errors"
    46  )
    47  
    48  var SkipOneElement			=	errors.New("Skip one element")
    49  var SkipRemainingElements	=	errors.New("Skip remaining elements")
    50  
    51  // GenerateSliceOfInt() creates a slice of a given size and populates its
    52  // values with the given generator (which may be nil)
    53  func GenerateSliceOfInt(size int, generator func(index int) (result int, err error)) (result []int, err error) {
    54  
    55  	result	=	make([]int, size)
    56  
    57  	if generator != nil {
    58  
    59  		for i := 0; size != i; i++ {
    60  
    61  			value, err := generator(i)
    62  
    63  			if err == nil {
    64  
    65  				result[i] = value
    66  			} else {
    67  
    68  				if SkipOneElement == err {
    69  
    70  				} else if SkipRemainingElements == err {
    71  
    72  					break
    73  				} else {
    74  
    75  					return nil, err
    76  				}
    77  			}
    78  		}
    79  	}
    80  
    81  	return
    82  }
    83  
    84  // GenerateSliceOfUInt() creates a slice of a given size and populates its
    85  // values with the given generator (which may be nil)
    86  func GenerateSliceOfUInt(size int, generator func(index int) (result uint, err error)) (result []uint, err error) {
    87  
    88  	result	=	make([]uint, size)
    89  
    90  	if generator != nil {
    91  
    92  		for i := 0; size != i; i++ {
    93  
    94  			value, err := generator(i)
    95  
    96  			if err == nil {
    97  
    98  				result[i] = value
    99  			} else {
   100  
   101  				if SkipOneElement == err {
   102  
   103  				} else if SkipRemainingElements == err {
   104  
   105  					break
   106  				} else {
   107  
   108  					return nil, err
   109  				}
   110  			}
   111  		}
   112  	}
   113  
   114  	return
   115  }
   116  
   117  // GenerateSliceOfString() creates a slice of a given size and populates its
   118  // values with the given generator (which may be nil)
   119  func GenerateSliceOfString(size int, generator func(index int) (result string, err error)) (result []string, err error) {
   120  
   121  	result	=	make([]string, size)
   122  
   123  	if generator != nil {
   124  
   125  		for i := 0; size != i; i++ {
   126  
   127  			value, err := generator(i)
   128  
   129  			if err == nil {
   130  
   131  				result[i] = value
   132  			} else {
   133  
   134  				if SkipOneElement == err {
   135  
   136  				} else if SkipRemainingElements == err {
   137  
   138  					break
   139  				} else {
   140  
   141  					return nil, err
   142  				}
   143  			}
   144  		}
   145  	}
   146  
   147  	return
   148  }
   149  
   150  /* ///////////////////////////// end of file //////////////////////////// */
   151  
   152