Source file src/reflect/iter.go

     1  // Copyright 2024 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package reflect
     6  
     7  import "iter"
     8  
     9  func rangeNum[T int8 | int16 | int32 | int64 | int | uint8 | uint16 | uint32 | uint64 | uint | uintptr, N int64 | uint64](v N) iter.Seq[Value] {
    10  	return func(yield func(v Value) bool) {
    11  		// cannot use range T(v) because no core type.
    12  		for i := T(0); i < T(v); i++ {
    13  			if !yield(ValueOf(i)) {
    14  				return
    15  			}
    16  		}
    17  	}
    18  }
    19  
    20  // Seq returns an iter.Seq[Value] that loops over the elements of v.
    21  // If v's kind is Func, it must be a function that has no results and
    22  // that takes a single argument of type func(T) bool for some type T.
    23  // If v's kind is Pointer, the pointer element type must have kind Array.
    24  // Otherwise v's kind must be Int, Int8, Int16, Int32, Int64, Uint, Uint8, Uint16, Uint32, Uint64, Uintptr,
    25  // Array, Chan, Map, Slice, or String.
    26  func (v Value) Seq() iter.Seq[Value] {
    27  	if canRangeFunc(v.typ()) {
    28  		return func(yield func(Value) bool) {
    29  			rf := MakeFunc(v.Type().In(0), func(in []Value) []Value {
    30  				return []Value{ValueOf(yield(in[0]))}
    31  			})
    32  			v.Call([]Value{rf})
    33  		}
    34  	}
    35  	switch v.Kind() {
    36  	case Int:
    37  		return rangeNum[int](v.Int())
    38  	case Int8:
    39  		return rangeNum[int8](v.Int())
    40  	case Int16:
    41  		return rangeNum[int16](v.Int())
    42  	case Int32:
    43  		return rangeNum[int32](v.Int())
    44  	case Int64:
    45  		return rangeNum[int64](v.Int())
    46  	case Uint:
    47  		return rangeNum[uint](v.Uint())
    48  	case Uint8:
    49  		return rangeNum[uint8](v.Uint())
    50  	case Uint16:
    51  		return rangeNum[uint16](v.Uint())
    52  	case Uint32:
    53  		return rangeNum[uint32](v.Uint())
    54  	case Uint64:
    55  		return rangeNum[uint64](v.Uint())
    56  	case Uintptr:
    57  		return rangeNum[uintptr](v.Uint())
    58  	case Pointer:
    59  		if v.Elem().kind() != Array {
    60  			break
    61  		}
    62  		return func(yield func(Value) bool) {
    63  			v = v.Elem()
    64  			for i := range v.Len() {
    65  				if !yield(ValueOf(i)) {
    66  					return
    67  				}
    68  			}
    69  		}
    70  	case Array, Slice:
    71  		return func(yield func(Value) bool) {
    72  			for i := range v.Len() {
    73  				if !yield(ValueOf(i)) {
    74  					return
    75  				}
    76  			}
    77  		}
    78  	case String:
    79  		return func(yield func(Value) bool) {
    80  			for i := range v.String() {
    81  				if !yield(ValueOf(i)) {
    82  					return
    83  				}
    84  			}
    85  		}
    86  	case Map:
    87  		return func(yield func(Value) bool) {
    88  			i := v.MapRange()
    89  			for i.Next() {
    90  				if !yield(i.Key()) {
    91  					return
    92  				}
    93  			}
    94  		}
    95  	case Chan:
    96  		return func(yield func(Value) bool) {
    97  			for value, ok := v.Recv(); ok; value, ok = v.Recv() {
    98  				if !yield(value) {
    99  					return
   100  				}
   101  			}
   102  		}
   103  	}
   104  	panic("reflect: " + v.Type().String() + " cannot produce iter.Seq[Value]")
   105  }
   106  
   107  // Seq2 returns an iter.Seq2[Value, Value] that loops over the elements of v.
   108  // If v's kind is Func, it must be a function that has no results and
   109  // that takes a single argument of type func(K, V) bool for some type K, V.
   110  // If v's kind is Pointer, the pointer element type must have kind Array.
   111  // Otherwise v's kind must be Array, Map, Slice, or String.
   112  func (v Value) Seq2() iter.Seq2[Value, Value] {
   113  	if canRangeFunc2(v.typ()) {
   114  		return func(yield func(Value, Value) bool) {
   115  			rf := MakeFunc(v.Type().In(0), func(in []Value) []Value {
   116  				return []Value{ValueOf(yield(in[0], in[1]))}
   117  			})
   118  			v.Call([]Value{rf})
   119  		}
   120  	}
   121  	switch v.Kind() {
   122  	case Pointer:
   123  		if v.Elem().kind() != Array {
   124  			break
   125  		}
   126  		return func(yield func(Value, Value) bool) {
   127  			v = v.Elem()
   128  			for i := range v.Len() {
   129  				if !yield(ValueOf(i), v.Index(i)) {
   130  					return
   131  				}
   132  			}
   133  		}
   134  	case Array, Slice:
   135  		return func(yield func(Value, Value) bool) {
   136  			for i := range v.Len() {
   137  				if !yield(ValueOf(i), v.Index(i)) {
   138  					return
   139  				}
   140  			}
   141  		}
   142  	case String:
   143  		return func(yield func(Value, Value) bool) {
   144  			for i, v := range v.String() {
   145  				if !yield(ValueOf(i), ValueOf(v)) {
   146  					return
   147  				}
   148  			}
   149  		}
   150  	case Map:
   151  		return func(yield func(Value, Value) bool) {
   152  			i := v.MapRange()
   153  			for i.Next() {
   154  				if !yield(i.Key(), i.Value()) {
   155  					return
   156  				}
   157  			}
   158  		}
   159  	}
   160  	panic("reflect: " + v.Type().String() + " cannot produce iter.Seq2[Value, Value]")
   161  }
   162  

View as plain text