func (cache *IssueCacheFile) WriteIssueCache() error { cache.File.Truncate(0) cache.File.Seek(0, 0) cache.Issues = functional.Filter(func(i *Issue) bool { return i != nil }, cache.Issues).([]*Issue) err := json.NewEncoder(cache.File).Encode(cache.Issues) logOnError(err) return cache.File.Close() }
func main() { people := []Person{Person{Name: "Mary", Height: 160}, Person{Name: "Isla", Height: 80}, Person{Name: "Sam"}} fmt.Println("People", people) knowingHeight := f.Filter(func(person Person) bool { return (person.Height > 0) }, people).([]Person) fmt.Println("Knowing height people", knowingHeight) average := f.Reduce(func(a float32, person Person) float32 { return a + (float32(person.Height) / float32(len(knowingHeight))) }, people, float32(0)).(float32) fmt.Println("Average height among people we know their height: ", average) }
func SetupHook(path string, script string) { bash := "#!/bin/bash" lns, err := ReadFileLines(path) logOnError(err) if len(lns) == 0 { lns = append(lns, bash) } //Filter existing script line lns = functional.Filter(func(a string) bool { return a != script }, lns).([]string) lns = append(lns, script) logOnError(WriteFileLines(path, lns, true)) }
func main() { words := []string{"doing", "go", "functional", "programming"} numbers := []int{1, 2, 3} fmt.Println("MAP") fmt.Println("Square", numbers, f.Map(func(x int) int { return x * x }, numbers)) fmt.Println("Length", words, f.Map(func(x string) int { return len(x) }, words)) fmt.Println("Hash", words, f.Map(hash, words)) fmt.Println("REDUCE") fmt.Println("Sum", numbers, f.Reduce(func(a int, x int) int { return a + x }, numbers, 0)) fmt.Println("Total length", words, f.Reduce(func(a int, x string) int { return a + len(x) }, words, 0)) fmt.Println("FILTER") fmt.Println("Long words (>5)", words, f.Filter(func(x string) bool { return (len(x) > 5) }, words).([]string)) }