November 2017
Intermediate to advanced
670 pages
17h 35m
English
It takes one line of FP-style code to apply an upgrade and downgrade to a car. Granted, the Upgrade and Downgrade mapper functions were defined in the functor package, but that's a great benefit. We can keep the boilerplate implementation of looping through the slice of cars separate from our business use case logic.
With the imperative implementation style, we first implement the for...range iteration block into which we insert our Upgrade/Downgrade logic:
// FUNCTIONAL STYLEfmt.Println("up and downgrade:", functor.Wrap(cars).Map(functor.Upgrade).Map(functor.Downgrade))// IMPERATIVE STYLEcars2 := []functor.Car{}for _, car := range cars { // upgrade if !strings.Contains(car.Model, " LX" ...