November 2017
Intermediate to advanced
670 pages
17h 35m
English
We'll manipulate strings and some JSON, as well as a car functor:
package mainimport ( "encoding/json" "fmt" "functor" "strings")
Create a cars variable to hold a Car type and initialize it with two cars. Since we annotated our Make and Model fields with 'json', we can easily unmarshal a Toyota Highlander into a car:
func main() { cars := []functor.Car{ {"Honda", "Accord"}, {"Lexus", "IS250"}} str := `{"make": "Toyota", "model": "Highlander"}` highlander := functor.Car{} json.Unmarshal([]byte(str), &highlander) cars = append(cars, highlander)
Now, let's exercise our car functor and verify that it works properly:
fmt.Println("initial state :", functor.Wrap(cars))fmt.Println("unit application:", functor.Wrap(cars).Map(functor.Unit)) ...