blob: 4a57a5335d3150914b9aea008320bf6efc8f3a4e [file] [log] [blame]
// run
// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
type Gen[A any] func() (A, bool)
func combine[T1, T2, T any](g1 Gen[T1], g2 Gen[T2], join func(T1, T2) T) Gen[T] {
return func() (T, bool) {
var t T
t1, ok := g1()
if !ok {
return t, false
}
t2, ok := g2()
if !ok {
return t, false
}
return join(t1, t2), true
}
}
type Pair[A, B any] struct {
A A
B B
}
func NewPair[A, B any](a A, b B) Pair[A, B] { return Pair[A, B]{a, b} }
func Combine2[A, B any](ga Gen[A], gb Gen[B]) Gen[Pair[A, B]] {
return combine(ga, gb, NewPair(A, B))
}
func main() {
var g1 Gen[int] = func() (int, bool) { return 3, true }
var g2 Gen[string] = func() (string, bool) { return "x", false }
gc := combine(g1, g2, NewPair[int, string])
gc2 := Combine2(g1, g2)
if got, ok := gc(); ok {
panic(got)
}
if got2, ok := gc2(); ok {
panic(got2)
}
}