Created
February 28, 2015 04:30
-
-
Save KAMEDAkyosuke/f567feb7c628821874b6 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
package main | |
import "log" | |
import "reflect" | |
func Apply(f interface{}, args []interface{}) interface{}{ | |
fv := reflect.ValueOf(f) | |
if fv.Kind() != reflect.Func { | |
log.Fatal("error") | |
} | |
l := make([]reflect.Value, len(args)) | |
for i, v := range args { | |
l[i] = reflect.ValueOf(v) | |
} | |
fv.Call(l) | |
} | |
func Eval(form interface{}) interface{} { | |
if list, t := form.([]interface{}); t { | |
log.Printf("form.([]interface{}) %v", list) | |
Apply(list[0], list[1:]) | |
} else if str, t := form.(string); t { | |
log.Printf("form.(string) %v", str) | |
} else if i, t := form.(int); t { | |
log.Printf("form.(int) %v", i) | |
} | |
} | |
func main() { | |
Eval("foo") | |
Eval(123) | |
Eval([]interface{}{ | |
func(s string){ | |
log.Printf("%v", s) | |
}, | |
"world"}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment