MCPcopy
hub / github.com/duke-git/lancet / Flatten

Function Flatten

slice/slice.go:475–500  ·  view source on GitHub ↗

Flatten flattens slice with one level. Play: https://go.dev/play/p/hYa3cBEevtm

(slice any)

Source from the content-addressed store, hash-verified

473// Flatten flattens slice with one level.
474// Play: https://go.dev/play/p/hYa3cBEevtm
475func Flatten(slice any) any {
476 sv := reflect.ValueOf(slice)
477 if sv.Kind() != reflect.Slice {
478 panic("Flatten: input must be a slice")
479 }
480
481 elemType := sv.Type().Elem()
482 if elemType.Kind() == reflect.Slice {
483 elemType = elemType.Elem()
484 }
485
486 result := reflect.MakeSlice(reflect.SliceOf(elemType), 0, sv.Len())
487
488 for i := 0; i < sv.Len(); i++ {
489 item := sv.Index(i)
490 if item.Kind() == reflect.Slice {
491 for j := 0; j < item.Len(); j++ {
492 result = reflect.Append(result, item.Index(j))
493 }
494 } else {
495 result = reflect.Append(result, item)
496 }
497 }
498
499 return result.Interface()
500}
501
502// FlattenDeep flattens slice recursive.
503// Play: https://go.dev/play/p/yjYNHPyCFaF

Callers 2

ExampleFlattenFunction · 0.85
TestFlattenFunction · 0.85

Calls 3

KindMethod · 0.80
ValueOfMethod · 0.45
LenMethod · 0.45

Tested by 2

ExampleFlattenFunction · 0.68
TestFlattenFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…