Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 13 additions & 16 deletions agent/nanobot/hoist.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,37 +20,34 @@ import (
// Hoist lifts an Arr[T] into Arr[S] by applying an isomorphism between S and T.
type Hoister[S, T any] interface{ Hoist(Arr[T]) Arr[S] }

// BiMap creates a partial morphism between S and T.
// Codec creates a partial morphism between S and T.
// It allows us to treat S and T as interchangeable for the purpose of applying Arr[T] to S.
func BiMap[S, A, T, B any]() Hoister[S, T] {
return iso[S, A, T, B]{
sa: optics.ForProduct1[S, A](),
sb: optics.ForProduct1[S, B](),
ta: optics.ForProduct1[T, A](),
tb: optics.ForProduct1[T, B](),
}
func Codec[S, T, A, B any]() Hoister[S, T] {
return iso[S, T]{iso: optics.CodecS1T1[S, T, A, B]()}
}

// Morph creates a partial morphism between S and T using a custom isomorphism.
func Morph[S, T any](m optics.Isomorphism[S, T]) Hoister[S, T] {
return iso[S, T]{iso: m}
}

type iso[S, A, T, B any] struct {
sa optics.Lens[S, A]
sb optics.Lens[S, B]
ta optics.Lens[T, A]
tb optics.Lens[T, B]
type iso[S, T any] struct {
iso optics.Isomorphism[S, T]
}

// Hoist lifts an Arr[T] into Arr[S] using the isomorphism defined by the iso struct.
func (iso iso[S, A, T, B]) Hoist(arrT Arr[T]) Arr[S] {
func (iso iso[S, T]) Hoist(arrT Arr[T]) Arr[S] {
return func(ctx context.Context, s S, opt ...chatter.Opt) (S, error) {
t := alloc[T]()

iso.ta.Put(&t, iso.sa.Get(&s))
iso.iso.Forward(&s, &t)

t, err := arrT(ctx, t, opt...)
if err != nil {
return s, err
}

iso.sb.Put(&s, iso.tb.Get(&t))
iso.iso.Inverse(&t, &s)
return s, nil
}
}
Expand Down
6 changes: 3 additions & 3 deletions agent/nanobot/hoist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestHoist(t *testing.T) {
}

// Create a hoister using BiMap
hoister := nanobot.BiMap[Source, int, Target, string]()
hoister := nanobot.Codec[Source, Target, int, string]()

// Define an Arr[Target] that modifies the target
arrTarget := func(ctx context.Context, target Target, opt ...chatter.Opt) (Target, error) {
Expand Down Expand Up @@ -68,7 +68,7 @@ func TestHoist(t *testing.T) {
FieldB string
}

hoister := nanobot.BiMap[Source, int, Target, string]()
hoister := nanobot.Codec[Source, Target, int, string]()

// Arr[Target] that returns an error
testErr := errors.New("test error")
Expand Down Expand Up @@ -99,7 +99,7 @@ func TestHoist(t *testing.T) {
B bool
}

hoister := nanobot.BiMap[Source, float64, Target, bool]()
hoister := nanobot.Codec[Source, Target, float64, bool]()

arrTarget := func(ctx context.Context, target Target, opt ...chatter.Opt) (Target, error) {
target.A *= 2
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/aws/aws-sdk-go-v2/credentials v1.19.14
github.com/aws/aws-sdk-go-v2/service/sts v1.41.10
github.com/fogfish/faults v0.3.2
github.com/fogfish/golem/optics v0.14.2
github.com/fogfish/golem/optics v0.14.3
github.com/fogfish/guid/v2 v2.1.0
github.com/fogfish/it/v2 v2.2.4
github.com/goccy/go-yaml v1.19.2
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ github.com/fogfish/faults v0.3.2 h1:kQai2/VyXJxfd6SD/jYLHiqu0qDl/KXT48q1ppLMAnY=
github.com/fogfish/faults v0.3.2/go.mod h1:y8zvZN2pQUe9vDS7rzz0mAnbdfYMorPOeqxpy83YOCk=
github.com/fogfish/golem/hseq v1.3.1 h1:UyCmp44JcXvs7OVXJxnJldur/UpL5skJGTKdDFxYUsU=
github.com/fogfish/golem/hseq v1.3.1/go.mod h1:17XORt8nNKl6KOhF43MHSmjK8NksbkBsohAoJGiinUs=
github.com/fogfish/golem/optics v0.14.2 h1:bKu+8kGeieRIgpEyqlGaMPXapNGqDrklc7k4HbSLh5M=
github.com/fogfish/golem/optics v0.14.2/go.mod h1:qbtka94yn+x5U1KDD+UX69C07M6I2J9xgwOciAYYeI8=
github.com/fogfish/golem/optics v0.14.3 h1:QOx9ABahDlcpM44HmSjPJkJ/Q50nd8yohhDthKdgo+s=
github.com/fogfish/golem/optics v0.14.3/go.mod h1:qbtka94yn+x5U1KDD+UX69C07M6I2J9xgwOciAYYeI8=
github.com/fogfish/guid/v2 v2.1.0 h1:oEJHKM4yFOOCmKZdh0oH7eD3mL32n2+1YCc27lXB5rE=
github.com/fogfish/guid/v2 v2.1.0/go.mod h1:KkZ5T4EE3BqWQJFZBPLSHV/tBe23Xq4KvuPfwtNtepU=
github.com/fogfish/it/v2 v2.2.4 h1:hkBePGW7X/wDc1QCLG/j+/j47TG4obnozYsGMX51yMQ=
Expand Down
Loading