mirror of
https://github.com/RussellChamp/advent-of-code-2021.git
synced 2026-10-27 21:52:32 +00:00
50 lines
971 B
Go
50 lines
971 B
Go
package arrays
|
|
|
|
func FindFirst(list []string, filter func(string) bool) string {
|
|
for _, item := range list {
|
|
if filter(item) {
|
|
return item
|
|
}
|
|
}
|
|
panic("No item found that matches criteria")
|
|
}
|
|
|
|
func FindFirstInt(list []int, filter func(int) bool) int {
|
|
for _, item := range list {
|
|
if filter(item) {
|
|
return item
|
|
}
|
|
}
|
|
panic("No item found that matches criteria")
|
|
}
|
|
|
|
func FindAll(list []string, filter func(string) bool) []string {
|
|
var filteredList []string
|
|
for _, item := range list {
|
|
if filter(item) {
|
|
filteredList = append(filteredList, item)
|
|
}
|
|
}
|
|
|
|
return filteredList
|
|
}
|
|
|
|
func FindAllInt(list []int, filter func(int) bool) []int {
|
|
var filteredList []int
|
|
for _, item := range list {
|
|
if filter(item) {
|
|
filteredList = append(filteredList, item)
|
|
}
|
|
}
|
|
|
|
return filteredList
|
|
}
|
|
|
|
func MapStrToInt(input []string, fn func(string) int) []int {
|
|
var retVals []int
|
|
for _, s := range input {
|
|
retVals = append(retVals, fn(s))
|
|
}
|
|
return retVals
|
|
}
|