Created
August 20, 2016 22:06
-
-
Save svanellewee/927a50f812a3b568bd909c49508217cc to your computer and use it in GitHub Desktop.
Go mappable interface
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
package main | |
import ( | |
"fmt" | |
"github.com/golang/protobuf/proto" | |
) | |
type Mapable interface{ | |
Map(interface{}) Mapable | |
} | |
type IntSlice []int | |
func (i IntSlice) Map(fn interface{}) Mapable { | |
data := make(IntSlice,0) | |
v,_ := fn.(func(int) int) | |
for _, value := range i{ | |
data = append(data, v(value)) | |
} | |
return data | |
} | |
type Ztring string | |
func (s Ztring) Map(fn interface{}) Mapable { | |
var data Ztring | |
v, _ := fn.(func(Ztring) Ztring) | |
for _, value := range s{ | |
data += v(Ztring(value)) | |
} | |
return data | |
} | |
func main() { | |
fmt.Println("Let's get dangerous") | |
fmt.Printf("SSS %s\n\n", proto.String("hello")) | |
// animals := []Animal{ Dog{}, Human{}, Bird{}} | |
// for _, animal := range animals { | |
// fmt.Println(animal.Walk()) | |
// } | |
i := IntSlice{10, 20, 30} | |
result := i.Map(func(value int) int{ | |
return value * 123 | |
}) | |
fmt.Println(result) | |
var s Ztring = "hello" | |
result2 := s.Map(func(i Ztring) Ztring{ | |
return "["+i+"]" | |
}) | |
fmt.Println("result=", result2) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment