github.com/egonelbre/exp@v0.0.0-20240430123955-ed1d3aa93911/dataflow/partial/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 Out2[N Node2[A, B], A, B any](v N) *Pick2[N, A, B] {
    26  	return &Pick2[N, A, B]{Value: v}
    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[A float64] struct {
    40  	Value Node1[A]
    41  
    42  	Sin, Cos A
    43  }
    44  
    45  func (v *SinCos[A]) Update() {
    46  	sn, cs := math.Sincos(float64(v.Value.Out1()))
    47  	v.Sin, v.Cos = A(sn), A(cs)
    48  }
    49  
    50  func (v *SinCos[A]) Out1() A { return v.Sin }
    51  func (v *SinCos[A]) Out2() A { return v.Cos }
    52  
    53  type Probe[A any] struct {
    54  	Value Node1[A]
    55  }
    56  
    57  func (p *Probe[A]) Out1() A {
    58  	v := p.Value.Out1()
    59  	fmt.Println(v)
    60  	return v
    61  }
    62  
    63  func main() {
    64  	sinCos := &SinCos[float64]{
    65  		Value: &Parameter[float64]{
    66  			Name:         "Hello",
    67  			Value:        10,
    68  			DefaultValue: 5,
    69  		},
    70  	}
    71  
    72  	probeSin := &Probe[float64]{Value: sinCos}
    73  	probeCos := &Probe[float64]{Value: Out2(sinCos)}
    74  
    75  	sinCos.Update()
    76  	probeSin.Out1()
    77  	probeCos.Out1()
    78  }