...
Tawesoft Logo

Source file src/tawesoft.co.uk/go/variadic/flatten.go

Documentation: src/tawesoft.co.uk/go/variadic/flatten.go

     1  package variadic
     2  
     3  import (
     4      "reflect"
     5  )
     6  
     7  // Flatten takes a variable number of arguments and returns an array
     8  // containing, in sequence, each argument or - if an argument is an array or
     9  // a slice - that argument's contents.
    10  //
    11  // e.g. Flatten(1, []int{2, 3, 4}, 5) => 1, 2, 3, 4, 5
    12  func Flatten(args ... interface{}) []interface{} {
    13      result := make([]interface{}, 0)
    14      
    15      for _, arg := range args {
    16          v := reflect.ValueOf(arg)
    17          vk := v.Kind()
    18          
    19          if (vk == reflect.Array) || (vk == reflect.Slice) {
    20              for i := 0; i < v.Len(); i++ {
    21                  result = append(result, v.Index(i).Interface())
    22              }
    23          } else {
    24              result = append(result, arg)
    25          }
    26      }
    27      
    28      return result
    29  }
    30  
    31  // FlattenRecursive takes a variable number of arguments and returns an array
    32  // containing, in sequence, each argument or - if an argument is an array or
    33  // a slice - that argument's contents, flattened recursively.
    34  //
    35  // e.g. FlattenRecursive([]interface{}{1, 2, []int{3, 4}}, 5) => 1, 2, 3, 4, 5
    36  func FlattenRecursive(args ... interface{}) []interface{} {
    37      result := make([]interface{}, 0)
    38      
    39      for _, arg := range args {
    40          v := reflect.ValueOf(arg)
    41          vk := v.Kind()
    42          
    43          if (vk == reflect.Array) || (vk == reflect.Slice) {
    44              for i := 0; i < v.Len(); i++ {
    45                  result = append(result, FlattenRecursive(v.Index(i).Interface())...)
    46              }
    47          } else {
    48              result = append(result, arg)
    49          }
    50      }
    51      
    52      return result
    53  }
    54  
    55  // FlattenExcludingNils takes a variable number of arguments and returns an
    56  // array containing, in sequence, each non-nil argument or - if an argument is
    57  // an array or a slice - that argument's non-nil contents.
    58  //
    59  // e.g. FlattenExcludingNils(1, nil, []int{2, nil, 3, 4}, 5) => 1, 2, 3, 4, 5
    60  func FlattenExcludingNils(args ... interface{}) []interface{} {
    61      result := make([]interface{}, 0)
    62      
    63      for _, arg := range args {
    64          if arg == nil { continue }
    65          
    66          v := reflect.ValueOf(arg)
    67          vk := v.Kind()
    68          
    69          if (vk == reflect.Array) || (vk == reflect.Slice) {
    70              for i := 0; i < v.Len(); i++ {
    71                  element := v.Index(i)
    72                  if element.IsNil() { continue }
    73                  result = append(result, element.Interface())
    74              }
    75          } else {
    76              result = append(result, arg)
    77          }
    78      }
    79      
    80      return result
    81  }
    82  

View as plain text