clipperhouse / gen

Type-driven code generation for Go
http://clipperhouse.com/gen/overview/
Other
1.43k stars 90 forks source link

Take first n elements #70

Open tp opened 10 years ago

tp commented 10 years ago

Today I encountered a case where I wanted to take the first n elements from a slice. With the slice[low:high] notation you have to first check, whether the slice has n elements, which makes this simple operation rather long.

thingsToTake := 7
if len(things) > thingsToTake {
  things = things[0:thingsToTake]
}

Do you think, such a feature would be appropriate for gen?

clipperhouse commented 10 years ago

It’s certainly a convenient method, I use it in LINQ a lot. Will think about it – on the one hand, it’s handy. On the other, it’s very nearly covered by normal slice operations, so it feels like sugar.