Skip to content

Instantly share code, notes, and snippets.

@nikolaydubina
Created March 23, 2022 11:42
Show Gist options
  • Save nikolaydubina/81130adecfc41ead871e96128b556db5 to your computer and use it in GitHub Desktop.
Save nikolaydubina/81130adecfc41ead871e96128b556db5 to your computer and use it in GitHub Desktop.
// I am applying function on two things and they tell me how 🐁
// https://go.dev/play/p/8iGGh8Tebgw
package main
import "fmt"
type Summer interface {
Sum(other Summer) Summer
}
type AddSummer struct {
X int
Y int
}
func (s AddSummer) Sum(other Summer) Summer {
v, ok := other.(AddSummer)
if !ok {
return s
}
return AddSummer{
X: s.X + v.X,
Y: s.Y + v.Y,
}
}
func sum(s []Summer) Summer {
t := s[0]
for i, q := range s {
if i == 0 {
continue
}
t = t.Sum(q)
}
return t
}
func main() {
s := AddSummer{X: 1, Y: 1}
v := sum([]Summer{s, s, s, s})
fmt.Printf("%#v", v)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment