November 2017
Intermediate to advanced
670 pages
17h 35m
English
Let's create a Map function to transform the contents of a Collection.
First, let's define Object to be the empty interface type and create a Collection type to be a slice of objects:
package mainimport "fmt"type Object interface{}type Collection []Objectfunc NewCollection(size int) Collection { return make(Collection, size)}
The NewCollection function creates a new instance of the collection with the given size:
type Callback func(current, currentKey, src Object) Object
The Callback type is a first-class function type that returns the calculated result:
func Map(c Collection, cb Callback) Collection { if c == nil { return Collection{} } else if cb == nil { return c } result := NewCollection(len(c)) for index, val := range c ...