2021-02-21 11:54:38 -07:00
|
|
|
// run -gcflags=-G=3
|
|
|
|
|
|
|
|
// Copyright 2021 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
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
2021-05-09 12:38:34 -06:00
|
|
|
type Gen[A any] func() (A, bool)
|
2021-02-21 11:54:38 -07:00
|
|
|
|
2021-05-09 12:38:34 -06:00
|
|
|
func Combine[T1, T2, T any](g1 Gen[T1], g2 Gen[T2], join func(T1, T2) T) Gen[T] {
|
2021-07-28 14:39:30 -06:00
|
|
|
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
|
|
|
|
}
|
2021-02-21 11:54:38 -07:00
|
|
|
}
|
|
|
|
|
2021-05-09 12:38:34 -06:00
|
|
|
type Pair[A, B any] struct {
|
2021-02-21 11:54:38 -07:00
|
|
|
A A
|
|
|
|
B B
|
|
|
|
}
|
|
|
|
|
2021-05-09 12:38:34 -06:00
|
|
|
func _NewPair[A, B any](a A, b B) Pair[A, B] {
|
|
|
|
return Pair[A, B]{a, b}
|
2021-02-21 11:54:38 -07:00
|
|
|
}
|
|
|
|
|
2021-05-09 12:38:34 -06:00
|
|
|
func Combine2[A, B any](ga Gen[A], gb Gen[B]) Gen[Pair[A, B]] {
|
2021-07-28 14:39:30 -06:00
|
|
|
return Combine(ga, gb, _NewPair[A, B])
|
2021-02-21 11:54:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2021-05-09 12:38:34 -06:00
|
|
|
var g1 Gen[int] = func() (int, bool) { return 3, true }
|
|
|
|
var g2 Gen[string] = func() (string, bool) { return "x", false }
|
|
|
|
var g3 Gen[string] = func() (string, bool) { return "y", true }
|
2021-02-21 11:54:38 -07:00
|
|
|
|
2021-05-09 12:38:34 -06:00
|
|
|
gc := Combine(g1, g2, _NewPair[int, string])
|
2021-02-21 11:54:38 -07:00
|
|
|
if got, ok := gc(); ok {
|
|
|
|
panic(fmt.Sprintf("got %v, %v, wanted -/false", got, ok))
|
|
|
|
}
|
2021-05-09 12:38:34 -06:00
|
|
|
gc2 := Combine2(g1, g2)
|
2021-02-21 11:54:38 -07:00
|
|
|
if got, ok := gc2(); ok {
|
|
|
|
panic(fmt.Sprintf("got %v, %v, wanted -/false", got, ok))
|
|
|
|
}
|
|
|
|
|
2021-05-09 12:38:34 -06:00
|
|
|
gc3 := Combine(g1, g3, _NewPair[int, string])
|
2021-02-21 11:54:38 -07:00
|
|
|
if got, ok := gc3(); !ok || got.A != 3 || got.B != "y" {
|
|
|
|
panic(fmt.Sprintf("got %v, %v, wanted {3, y}, true", got, ok))
|
|
|
|
}
|
2021-05-09 12:38:34 -06:00
|
|
|
gc4 := Combine2(g1, g3)
|
2021-02-21 11:54:38 -07:00
|
|
|
if got, ok := gc4(); !ok || got.A != 3 || got.B != "y" {
|
2021-07-28 14:39:30 -06:00
|
|
|
panic(fmt.Sprintf("got %v, %v, wanted {3, y}, true", got, ok))
|
2021-02-21 11:54:38 -07:00
|
|
|
}
|
|
|
|
}
|