Skip to main content
Help improve this documentation

This documentation is still new and evolving. If you spot any mistakes, unclear explanations, or missing details, please open an issue.

Your feedback helps us improve!

Core - Tuple helpersโ€‹

This page lists all tuple operations available in the core package of lo.

  • Constructors for tuple values from 2 up to 9 elements.

    Variants: T2..T9

    t := lo.T3(1, "a", true)
    // lo.Tuple3[int, string, bool]{A:1, B:"a", C:true}
    Variant:
    Prototypes:
    func T2[A, B any](a A, b B) lo.Tuple2[A, B]
    func T3[A, B, C any](a A, b B, c C) lo.Tuple3[A, B, C]
    func T4[A, B, C, D any](a A, b B, c C, d D) lo.Tuple4[A, B, C, D]
    func T5[A, B, C, D, E any](a A, b B, c C, d D, e E) lo.Tuple5[A, B, C, D, E]
    func T6[A, B, C, D, E, F any](a A, b B, c C, d D, e E, f F) lo.Tuple6[A, B, C, D, E, F]
    func T7[A, B, C, D, E, F, G any](a A, b B, c C, d D, e E, f F, g G) lo.Tuple7[A, B, C, D, E, F, G]
    func T8[A, B, C, D, E, F, G, H any](a A, b B, c C, d D, e E, f F, g G, h H) lo.Tuple8[A, B, C, D, E, F, G, H]
    func T9[A, B, C, D, E, F, G, H, I any](a A, b B, c C, d D, e E, f F, g G, h H, i I) lo.Tuple9[A, B, C, D, E, F, G, H, I]
  • Extracts values from tuples. Variants support tuple sizes from 2 to 9.

    Variants: Unpack2..Unpack9

    a, b, c := lo.Unpack3(lo.T3(1, "a", true))
    Prototype:
    func Unpack2[A, B any](tuple Tuple2[A, B]) (A, B)
  • Zips multiple slices into a slice of tuples. Variants support 2 up to 9 input slices.

    Variants: Zip2..Zip9

    xs := []int{1,2}
    ys := []string{"a","b"}
    pairs := lo.Zip2(xs, ys)
    Prototype:
    func Zip2[A, B any](a []A, b []B) []Tuple2[A, B]
  • Splits a slice of tuples back into multiple parallel slices. Variants support tuple sizes from 2 to 9.

    Variants: Unzip2..Unzip9

    pairs := []lo.Tuple2[int, string]{
    lo.T2(1, "a"),
    lo.T2(2, "b"),
    }
    xs, ys := lo.Unzip2(pairs)
    Prototypes:
    func Unzip2[A, B any](tuples []Tuple2[A, B]) ([]A, []B)
    func Unzip3[A, B, C any](tuples []Tuple3[A, B, C]) ([]A, []B, []C)
    func Unzip4[A, B, C, D any](tuples []Tuple4[A, B, C, D]) ([]A, []B, []C, []D)
    func Unzip5[A, B, C, D, E any](tuples []Tuple5[A, B, C, D, E]) ([]A, []B, []C, []D, []E)
    func Unzip6[A, B, C, D, E, F any](tuples []Tuple6[A, B, C, D, E, F]) ([]A, []B, []C, []D, []E, []F)
    func Unzip7[A, B, C, D, E, F, G any](tuples []Tuple7[A, B, C, D, E, F, G]) ([]A, []B, []C, []D, []E, []F, []G)
    func Unzip8[A, B, C, D, E, F, G, H any](tuples []Tuple8[A, B, C, D, E, F, G, H]) ([]A, []B, []C, []D, []E, []F, []G, []H)
    func Unzip9[A, B, C, D, E, F, G, H, I any](tuples []Tuple9[A, B, C, D, E, F, G, H, I]) ([]A, []B, []C, []D, []E, []F, []G, []H, []I)
  • Zips multiple slices and projects each grouped set through a function. Variants support 2 up to 9 input slices.

    Variants: ZipBy2..ZipBy9

    xs := []int{1,2}
    ys := []string{"a","b"}
    pairs := lo.ZipBy2(xs, ys, func(x int, y string) string {
    return fmt.Sprintf("%d-%s", x, y)
    })
    Prototypes:
    func ZipBy2[A any, B any, Out any](a []A, b []B, predicate func(a A, b B) Out) []Out
    func ZipBy3[A any, B any, C any, Out any](a []A, b []B, c []C, predicate func(a A, b B, c C) Out) []Out
    func ZipBy4[A any, B any, C any, D any, Out any](a []A, b []B, c []C, d []D, predicate func(a A, b B, c C, d D) Out) []Out
    func ZipBy5[A any, B any, C any, D any, E any, Out any](a []A, b []B, c []C, d []D, e []E, predicate func(a A, b B, c C, d D, e E) Out) []Out
    func ZipBy6[A any, B any, C any, D any, E any, F any, Out any](a []A, b []B, c []C, d []D, e []E, f []F, predicate func(a A, b B, c C, d D, e E, f F) Out) []Out
    func ZipBy7[A any, B any, C any, D any, E any, F any, G any, Out any](a []A, b []B, c []C, d []D, e []E, f []F, g []G, predicate func(a A, b B, c C, d D, e E, f F, g G) Out) []Out
    func ZipBy8[A any, B any, C any, D any, E any, F any, G any, H any, Out any](a []A, b []B, c []C, d []D, e []E, f []F, g []G, h []H, predicate func(a A, b B, c C, d D, e E, f F, g G, h H) Out) []Out
    func ZipBy9[A any, B any, C any, D any, E any, F any, G any, H any, I any, Out any](a []A, b []B, c []C, d []D, e []E, f []F, g []G, h []H, i []I, predicate func(a A, b B, c C, d D, e E, f F, g G, h H, i I) Out) []Out
  • Transforms each input element into a tuple and splits results into parallel slices. Variants support arities from 2 to 9.

    Variants: UnzipBy2..UnzipBy9

    type User struct{ ID int; Name string }
    ids, names := lo.UnzipBy2([]User{{1,"a"},{2,"b"}}, func(u User) (int, string) {
    return u.ID, u.Name
    })
    Prototypes:
    func UnzipBy2[In any, A any, B any](items []In, predicate func(In) (a A, b B)) ([]A, []B)
    func UnzipBy3[In any, A any, B any, C any](items []In, predicate func(In) (a A, b B, c C)) ([]A, []B, []C)
    func UnzipBy4[In any, A any, B any, C any, D any](items []In, predicate func(In) (a A, b B, c C, d D)) ([]A, []B, []C, []D)
    func UnzipBy5[In any, A any, B any, C any, D any, E any](items []In, predicate func(In) (a A, b B, c C, d D, e E)) ([]A, []B, []C, []D, []E)
    func UnzipBy6[In any, A any, B any, C any, D any, E any, F any](items []In, predicate func(In) (a A, b B, c C, d D, e E, f F)) ([]A, []B, []C, []D, []E, []F)
    func UnzipBy7[In any, A any, B any, C any, D any, E any, F any, G any](items []In, predicate func(In) (a A, b B, c C, d D, e E, f F, g G)) ([]A, []B, []C, []D, []E, []F, []G)
    func UnzipBy8[In any, A any, B any, C any, D any, E any, F any, G any, H any](items []In, predicate func(In) (a A, b B, c C, d D, e E, f F, g G, h H)) ([]A, []B, []C, []D, []E, []F, []G, []H)
    func UnzipBy9[In any, A any, B any, C any, D any, E any, F any, G any, H any, I any](items []In, predicate func(In) (a A, b B, c C, d D, e E, f F, g G, h H, i I)) ([]A, []B, []C, []D, []E, []F, []G, []H, []I)
  • Computes the cartesian product of input slices, returning tuples of all combinations. Variants support 2 up to 9 input slices.

    Variants: CrossJoin2..CrossJoin9

    a := []int{1,2}
    b := []string{"x","y"}
    pairs := lo.CrossJoin2(a, b)
    Prototypes:
    func CrossJoin2[A, B any](listA []A, listB []B) []Tuple2[A, B]
    func CrossJoin3[A, B, C any](listA []A, listB []B, listC []C) []Tuple3[A, B, C]
    func CrossJoin4[A, B, C, D any](listA []A, listB []B, listC []C, listD []D) []Tuple4[A, B, C, D]
    func CrossJoin5[A, B, C, D, E any](listA []A, listB []B, listC []C, listD []D, listE []E) []Tuple5[A, B, C, D, E]
    func CrossJoin6[A, B, C, D, E, F any](listA []A, listB []B, listC []C, listD []D, listE []E, listF []F) []Tuple6[A, B, C, D, E, F]
    func CrossJoin7[A, B, C, D, E, F, G any](listA []A, listB []B, listC []C, listD []D, listE []E, listF []F, listG []G) []Tuple7[A, B, C, D, E, F, G]
    func CrossJoin8[A, B, C, D, E, F, G, H any](listA []A, listB []B, listC []C, listD []D, listE []E, listF []F, listG []G, listH []H) []Tuple8[A, B, C, D, E, F, G, H]
    func CrossJoin9[A, B, C, D, E, F, G, H, I any](listA []A, listB []B, listC []C, listD []D, listE []E, listF []F, listG []G, listH []H, listI []I) []Tuple9[A, B, C, D, E, F, G, H, I]
  • Computes a cartesian product and projects each combination through a function. Variants support 2 up to 9 input slices.

    Variants: CrossJoinBy2..CrossJoinBy9

    a := []int{1,2}
    b := []string{"x","y"}
    out := lo.CrossJoinBy2(a, b, func(x int, y string) string {
    return fmt.Sprintf("%d-%s", x, y)
    })
    Prototypes:
    func CrossJoinBy2[A, B, Out any](listA []A, listB []B, project func(a A, b B) Out) []Out
    func CrossJoinBy3[A, B, C, Out any](listA []A, listB []B, listC []C, project func(a A, b B, c C) Out) []Out
    func CrossJoinBy4[A, B, C, D, Out any](listA []A, listB []B, listC []C, listD []D, project func(a A, b B, c C, d D) Out) []Out
    func CrossJoinBy5[A, B, C, D, E, Out any](listA []A, listB []B, listC []C, listD []D, listE []E, project func(a A, b B, c C, d D, e E) Out) []Out
    func CrossJoinBy6[A, B, C, D, E, F, Out any](listA []A, listB []B, listC []C, listD []D, listE []E, listF []F, project func(a A, b B, c C, d D, e E, f F) Out) []Out
    func CrossJoinBy7[A, B, C, D, E, F, G, Out any](listA []A, listB []B, listC []C, listD []D, listE []E, listF []F, listG []G, project func(a A, b B, c C, d D, e E, f F, g G) Out) []Out
    func CrossJoinBy8[A, B, C, D, E, F, G, H, Out any](listA []A, listB []B, listC []C, listD []D, listE []E, listF []F, listG []G, listH []H, project func(a A, b B, c C, d D, e E, f F, g G, h H) Out) []Out
    func CrossJoinBy9[A, B, C, D, E, F, G, H, I, Out any](listA []A, listB []B, listC []C, listD []D, listE []E, listF []F, listG []G, listH []H, listI []I, project func(a A, b B, c C, d D, e E, f F, g G, h H, i I) Out) []Out