github.com/packtpublishing/learning-functional-programming-in-go@v0.0.0-20230130084745-8b849f6d58c4/Chapter09/wip/fpingo/simple-functor.go (about)

     1  package main
     2  
     3  import (
     4  	"log"
     5  
     6  	"github.com/go-functional/core/functor"
     7  	"fmt"
     8  )
     9  
    10  func main() {
    11  	intSlice := []int{1, 2, 3, 4}
    12  	log.Printf("lifing this int slice into a functor: %#v", intSlice)
    13  	// here's where we turn an int slice into a functor. in FP terms, this is called "lifting"
    14  	functor := functor.LiftIntSlice(intSlice)
    15  	log.Printf("original functor: %s", functor)
    16  
    17  	// now we map over the functor to get a new functor with a new int slice in it.
    18  
    19  	// this is the mapper function that we will pass to the Map method
    20  	mapperFunc := func(i int) int {
    21  		// note that this func needs to be pure!
    22  		return i + 10
    23  	}
    24  
    25  	// The Map func isn't a Go map! Map is a method on all functors that executes mapperFunc
    26  	// on every item in the slice it contains
    27  	mapped := functor.Map(mapperFunc)
    28  	log.Printf("mapped functor: %s", mapped)
    29  
    30  }