Created
March 23, 2022 11:42
-
-
Save nikolaydubina/81130adecfc41ead871e96128b556db5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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