Comment

Comment by jrockway

I think people are going to go off the rails with functions that abstract over for loops. I see it all the time in Javascript code, where people iterate over an Array multiple times because that's what the API makes easy.

For example, say you want to separate a slice into two slices, one that contains matches and one that contains everything else. Right now, you'd just write:

   var matches, mismatches []whatever
   for _, x := range xs {
       if x == condition {
           matches = append(matches, x)
       } else {
           mismatches = append(mismatches, x)
       }
   }
But soon, people will be golfing that down to:

   matches    := slices.Search(xs, func(x whatever) bool { return x == condition })
   mismatches := slices.Search(xs, func(x whatever) bool { return x != condition })
This is twice as slow, but it's less lines so I have a feeling that people will have an uncontrollable desire to use it as often as possible. I hope that my feeling is wrong.

(Do note that slices.Search is not a real function yet.)

parent jrockway 4 years ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton