underscore
GitHubToggle Dark/Light/Auto modeToggle Dark/Light/Auto modeToggle Dark/Light/Auto modeBack to homepage

Flatmap

Flatmap flattens the input slice element into the new slice. FlatMap maps every element with the help of a mapper function, then flattens the input slice element into the new slice.

package main

import (
	"fmt"
	u "github.com/rjNemo/underscore"
)

func main() {
  nums := []int{1, 2, 3, 4}
  mapper := func(n int) []int { return []int{(n - 1) * n, (n) * n} }
  res := u.Flatmap(nums, mapper)
  fmt.Println(res) // {0, 1, 2, 4, 6, 9, 12, 16}
}