go-flat: github.com/spatialcurrent/go-flat/pkg/flat Index | Examples | Files

package flat

import "github.com/spatialcurrent/go-flat/pkg/flat"

Package flat provides functions to recursively flatten a slice of slices.

Index

Examples

Package Files

flat.go

func Flat Uses

func Flat(in interface{}) interface{}

Flat recursively flattens a slice of slices. Flat attempts to maintain the slice type if possible.

Code:

in := []interface{}{
    []interface{}{
        "a",
        "b",
        "c",
    },
    []int{1, 2, 3},
    []interface{}{},
    "d",
    "e",
    "f",
}
out := Flat(in)
fmt.Println(fmt.Sprintf("%#v", out))

Output:

[]interface {}{"a", "b", "c", 1, 2, 3, "d", "e", "f"}

func FlatDepth Uses

func FlatDepth(in interface{}, depth int, maxDepth int) interface{}

Flat recursively flattens a slice of slices to a maximum depth. Flat attempts to maintain the slice type if possible. If maxDepth is less than zero, then there is no maximum depth.

Code:

in := []interface{}{
    []interface{}{
        "a",
        []interface{}{
            "b",
            []interface{}{
                "c",
                "d",
                "e",
            },
        },
    },
}
maxDepth := 1
out := FlatDepth(in, 0, maxDepth)
fmt.Println(fmt.Sprintf("%#v", out))

Output:

[]interface {}{"a", "b", []interface {}{"c", "d", "e"}}

Package flat imports 2 packages (graph) and is imported by 1 packages. Updated 2019-06-14. Refresh now. Tools for package owners.