// Copyright (c) 2025 Alexey Mayshev and contributors. All rights reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package xiter import "iter" // Concat returns an iterator over the concatenation of the sequences. func Concat[V any](seqs ...iter.Seq[V]) iter.Seq[V] { return func(yield func(V) bool) { for _, seq := range seqs { for e := range seq { if !yield(e) { return } } } } } // MergeFunc merges two sequences of values ordered by the function f. // Values appear in the output once for each time they appear in x // and once for each time they appear in y. // When equal values appear in both sequences, // the output contains the values from x before the values from y. // If the two input sequences are not ordered by f, // the output sequence will not be ordered by f, // but it will still contain every value from x and y exactly once. func MergeFunc[V any](x, y iter.Seq[V], f func(V, V) int) iter.Seq[V] { return func(yield func(V) bool) { next, stop := iter.Pull(y) defer stop() v2, ok2 := next() for v1 := range x { for ok2 && f(v1, v2) > 0 { if !yield(v2) { return } v2, ok2 = next() } if !yield(v1) { return } } for ok2 { if !yield(v2) { return } v2, ok2 = next() } } }