Last active
January 27, 2018 13:29
-
-
Save hamakn/13b4ef805af039e28cbf518c28e6b932 to your computer and use it in GitHub Desktop.
golangどう書く
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
// rubyで言う | |
// (0..42).select(&:even?).each_slice(3).to_a | |
// # => [[0, 2, 4], [6, 8, 10], [12, 14, 16], [18, 20, 22], [24, 26, 28], [30, 32, 34], [36, 38, 40], [42]] | |
// がしたい!! | |
package main | |
import "fmt" | |
func main() { | |
fmt.Println( | |
createArrays(42, 3, func(i int) bool { | |
return i%2 == 0 | |
}), | |
) | |
// => [[0 2 4] [6 8 10] [12 14 16] [18 20 22] [24 26 28] [30 32 34] [36 38 40] [42]] | |
} | |
func createArrays(max, each_arr_size int, selector func(i int) bool) [][]int { | |
result := make([][]int, 0, max/each_arr_size) | |
index := -1 | |
current_arr_size := 0 | |
for i := 0; i <= max; i++ { | |
if !selector(i) { | |
continue | |
} | |
if current_arr_size == 0 { | |
index++ | |
result = append(result, make([]int, 0, each_arr_size)) | |
current_arr_size = each_arr_size | |
} | |
result[index] = append(result[index], i) | |
current_arr_size-- | |
} | |
return result | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment