Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Go

Idiom #314 Fill array with value

Set all the elements in the array x to the same value v

func fill[T any](x []T, v T) {
	for i := range x {
		x[i] = v
	}
}

fill is generic, it works for any type parameter T
for i := range x {
	x[i] = v
}
#include <array>
x.fill(v);

New implementation...