From 09274b41d4f867c2be907454e99e89ff7dff5ef9 Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Mon, 29 Nov 2021 23:58:29 +0000 Subject: [PATCH] ehh i dunno --- merge/box.go | 8 +++++--- merge/merge.go | 1 + merge/merge_test.go | 14 +++++--------- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/merge/box.go b/merge/box.go index 674bd1d..3428d00 100644 --- a/merge/box.go +++ b/merge/box.go @@ -44,14 +44,16 @@ func strip[X any](f func(X) error) func(interface{}) error { } } +func stripReturn[X any](f func() X) func() interface{} { + return func() interface {} { return f() } +} + // Box takes a mergeable value and creates a new mergeable value of type Boxed. // Any two Boxed values can attempt to merge at runtime. func Box[X Merges[X]](x X) Boxed { return Boxed{ val: x, - ident: func() interface{} { - return x.MergeIdentity() - }, + ident: stripReturn(x.MergeIdentity), merge: strip(x.Merge), } } diff --git a/merge/merge.go b/merge/merge.go index b3ecd18..7e3dae2 100644 --- a/merge/merge.go +++ b/merge/merge.go @@ -18,6 +18,7 @@ package merge // Although the type B satisfies the interface Merges[*C], it does not satisfy // the constraint [X Merges[X]], which is what is used throughout this package. type Merges[X any] interface { + // MergeIdentity defines the identity for the merge function MergeIdentity() X Merge(X) error } diff --git a/merge/merge_test.go b/merge/merge_test.go index 04be542..0b932dd 100644 --- a/merge/merge_test.go +++ b/merge/merge_test.go @@ -10,15 +10,13 @@ type additive struct { total int } -func (*additive) MergeIdentity() *additive { - return new(additive) -} - func (a *additive) Merge(b *additive) error { a.total += b.total return nil } +func (*additive) MergeIdentity() *additive { return new(additive) } + func add(n int) *additive { return &additive{total: n} } @@ -27,9 +25,7 @@ type multiplicative struct { scale int } -func (m *multiplicative) MergeIdentity() *multiplicative { - return &multiplicative{scale: 1} -} +func (*multiplicative) MergeIdentity() *multiplicative { return &multiplicative{scale: 1} } func (m *multiplicative) Merge(v *multiplicative) error { m.scale *= v.scale @@ -46,14 +42,14 @@ type exclusive struct { stock int } -func (*exclusive) MergeIdentity() *exclusive { return new(exclusive) } - func (e *exclusive) Merge(source *exclusive) error { e.stock += source.stock source.stock = 0 return nil } +func (*exclusive) MergeIdentity() *exclusive { return new(exclusive) } + func ex(n int) *exclusive { return &exclusive{stock: n} }