3 ms·
This example func isEven(iter iterator.ConstIterator) bool { return iter.Value().(int)%2 == 0 } ... algorithm.CountIf(a.Begin(), a.End(
by tgv 5y ago
This example
func isEven(iter iterator.ConstIterator) bool {
return iter.Value().(int)%2 == 0
}
...
algorithm.CountIf(a.Begin(), a.End(), isEven)
is certainly off-putting. It starts out with a "deque", which is basically []any (in modern Go), needs Begin and End pointers, and then even doesn't pass the actual value, but the iterator (which can be modified in the call). I'd rather see something more idiomatic like this
CountIf(len(a), func(i int) bool {return a[i] % 2 == 0 })
As you say, it might be helpful if you're porting something that relies heavily on STL, but only if you don't understand the code, and are willing to take the risk of subtle differences between the C++ and Go implementations.