Skip to content

proposal: slices: add CollectKeys and CollectValues to collect from an iter.Seq2 #69706

Closed as not planned
@jmillikin

Description

@jmillikin

Proposal Details

The slices package currently has Collect() to perform iter.Seq[T] -> []T, but it doesn't have similar helper functions for collecting the keys or values from an iter.Seq2[K, V].

I propose to add the following two functions to slices:

func CollectKeys[K, V any](seq iter.Seq2[K, V]) []K {
	var s []K
	for k := range seq {
		s = append(s, k)
	}
	return s
}

func CollectValues[K, V any](seq iter.Seq2[K, V]) []V {
	var s []V
	for _, v := range seq {
		s = append(s, v)
	}
	return s
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    Incoming

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions