Skip to content

Instantly share code, notes, and snippets.

@takoeight0821 takoeight0821/hello_cpp.go
Last active Aug 29, 2015

Embed
What would you like to do?
Cのプリプロセッサ on Go言語
package main
import (
"fmt"
"strconv"
)
#define map(TYPE_S, TYPE_F, SOURCE, FUNC)\
func (f func(TYPE_S) TYPE_F) []TYPE_F {\
r := make ([]TYPE_F, len(SOURCE));\
for i, e := range SOURCE {\
r[i] = f(e)\
};\
return r;\
}(FUNC)
func square(x int) int {
return x * x
}
func main() {
numbers := []int{1,2,3,4,5}
result := map(int, int, numbers, square)
fmt.Println(result)
fizzbuzz := map(int, string, numbers, func (x int) string {
switch x % 15 {
case 0: return "FizzBuzz"
case 3,6,9,12: return "Fizz"
case 5,10: return "Buzz"
default: return strconv.Itoa(x)
}
})
fmt.Println(fizzbuzz)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.