Last active
August 29, 2015 14:16
-
-
Save takoeight0821/63318a92dbb93c83b500 to your computer and use it in GitHub Desktop.
Cのプリプロセッサ on Go言語
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" | |
"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