github.com/egonelbre/exp@v0.0.0-20240430123955-ed1d3aa93911/dataflow/generic/main.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"math"
     6  )
     7  
     8  type Node1[A any] interface {
     9  	Out1() A
    10  }
    11  
    12  type Node2[A, B any] interface {
    13  	Out1() A
    14  	Out2() B
    15  }
    16  
    17  type Node3[A, B, C any] interface {
    18  	Out1() A
    19  	Out2() B
    20  	Out3() C
    21  }
    22  
    23  type Pick2[N Node2[A, B], A, B any] struct{ Value N }
    24  
    25  func MkPick2[N Node2[A, B], A, B any](in N) Pick2[N, A, B] {
    26  	return Pick2[N, A, B]{Value: in}
    27  }
    28  
    29  func (p Pick2[N, A, B]) Out1() B { return p.Value.Out2() }
    30  
    31  type Parameter[T any] struct {
    32  	Name         string
    33  	Value        T
    34  	DefaultValue T
    35  }
    36  
    37  func (p *Parameter[T]) Out1() T { return p.Value }
    38  
    39  type SinCos[In Node1[A], A float64] struct {
    40  	Value In
    41  
    42  	Sin, Cos A
    43  }
    44  
    45  func MkSinCos[In Node1[A], A float64](v In) *SinCos[In, A] {
    46  	return &SinCos[In, A]{Value: v}
    47  }
    48  
    49  func (v *SinCos[In, A]) Update() {
    50  	sn, cs := math.Sincos(float64(v.Value.Out1()))
    51  	v.Sin, v.Cos = A(sn), A(cs)
    52  }
    53  
    54  func (v *SinCos[In, A]) Out1() A { return v.Sin }
    55  func (v *SinCos[In, A]) Out2() A { return v.Cos }
    56  
    57  type Probe[In Node1[A], A any] struct {
    58  	Value In
    59  }
    60  
    61  func MkProbe[In Node1[A], A any](v In) Probe[In, A] {
    62  	return Probe[In, A]{Value: v}
    63  }
    64  
    65  func (p *Probe[In, A]) Out1() A {
    66  	v := p.Value.Out1()
    67  	fmt.Println(v)
    68  	return v
    69  }
    70  
    71  func main() {
    72  	sinCos := MkSinCos(
    73  		&Parameter[float64]{
    74  			Name:         "Hello",
    75  			Value:        10,
    76  			DefaultValue: 5,
    77  		},
    78  	)
    79  
    80  	probeSin := MkProbe(sinCos)
    81  	probeCos := MkProbe(MkPick2(sinCos))
    82  
    83  	sinCos.Update()
    84  	probeSin.Out1()
    85  	probeCos.Out1()
    86  }