Source file test/typeparam/issue50486.dir/goerror_fp.go

     1  // Copyright 2022 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 goerror_fp
     6  
     7  type Seq[T any] []T
     8  
     9  func (r Seq[T]) Size() int {
    10  	return len(r)
    11  }
    12  
    13  func (r Seq[T]) Append(items ...T) Seq[T] {
    14  	tail := Seq[T](items)
    15  	ret := make(Seq[T], r.Size()+tail.Size())
    16  
    17  	for i := range r {
    18  		ret[i] = r[i]
    19  	}
    20  
    21  	for i := range tail {
    22  		ret[i+r.Size()] = tail[i]
    23  	}
    24  
    25  	return ret
    26  }
    27  
    28  func (r Seq[T]) Iterator() Iterator[T] {
    29  	idx := 0
    30  
    31  	return Iterator[T]{
    32  		IsHasNext: func() bool {
    33  			return idx < r.Size()
    34  		},
    35  		GetNext: func() T {
    36  			ret := r[idx]
    37  			idx++
    38  			return ret
    39  		},
    40  	}
    41  }
    42  
    43  type Iterator[T any] struct {
    44  	IsHasNext func() bool
    45  	GetNext   func() T
    46  }
    47  
    48  func (r Iterator[T]) ToSeq() Seq[T] {
    49  	ret := Seq[T]{}
    50  	for r.HasNext() {
    51  		ret = append(ret, r.Next())
    52  	}
    53  	return ret
    54  }
    55  
    56  func (r Iterator[T]) Map(f func(T) any) Iterator[any] {
    57  	return MakeIterator(r.HasNext, func() any {
    58  		return f(r.Next())
    59  	})
    60  }
    61  
    62  func (r Iterator[T]) HasNext() bool {
    63  	return r.IsHasNext()
    64  }
    65  
    66  func (r Iterator[T]) Next() T {
    67  	return r.GetNext()
    68  }
    69  
    70  func MakeIterator[T any](has func() bool, next func() T) Iterator[T] {
    71  	return Iterator[T]{
    72  		IsHasNext: has,
    73  		GetNext:   next,
    74  	}
    75  }
    76  

View as plain text