-
Notifications
You must be signed in to change notification settings - Fork 31
Add all
, any
and find
methods on vectors
#15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I've thought about this too; they are basically nice shortcuts for : let v1 = Vec3::new(1, 2, 3);
let v2 = Vec3::new(3, 2, 1);
// Returns true if all of the elements satisfy the 'e > 5' predicate
let x = v1.map2(v2, |a, b| a + b * 3).map(|e| e > 5).reduce_and(); // true
// Returns true if any of the elements satisfy the 'e == 8' predicate
let y = v1.map2(v2, |a, b| a + b * 3).map(|e| e == 8).reduce_or(); // true
// Returns the index of the first element that satisfies the 'e == 8' predicate
let z = v1.map2(v2, |a, b| a + b * 3).iter().position(|e| e == 8).unwrap(); // 1 I'm a bit bothered by the fact that the primitive In fact, all of the methods you describe deserve to be built-in methods of These are convenient and easy to implement, so I'm all for adding them, but it's regrettable that they're not already part of the standard |
Thanks! You are right, |
In fact most of C++'s STL algorithm header has nice-looking operations that can operate on most collections, including the special case of slices of numbers (which To me the only question is "how far do we go?" and there's no end-all answer; we have to be opinionated and there are trade-offs. Also, in a vector math library I like to follow the rule "If GLSL has it, then it probably belongs in my library as well", which is the case here! |
For me, as a user rather than a maintainer, I often hit situations in which I tell myself "damn, this is a wordy piece of code". For me, that's the point at which implementing these methods becomes useful. Another big aspect is of course combinatorial functions. There's no point having a (Does |
Also I can't seem to decide if |
I guess there's an argument that |
Why not? :) They're glorified fixed-size arrays after all.
Then that would be (Sorry for the long post; it looked much less intimidating in my text editor.) To me the question is (as you suggested), at which point plugging together existing functionality becomes unacceptable for expressing a given intent. There are multiple factors that influence my decisions, which I've always somewhat "felt" but never took the time to "formalize", if that makes sense. I'll sum there up here (and keep as notes for myself):
With this in mind, here's my take on your proposal:
Note how in all of these instances, we would just be sparing one call to Why was Why was Why were My concern is that it feels like we're trying to implement our own interface to the Conclusion: it boils down to whether or not we mind typing I don't mind adding So, perhaps surpringly, I would rather take a conservative stance here, in order to prevent a snowball effect, and to encourage people to use the standard functions where it's reasonable enough. Thoughts? :) |
As a suggestion... is it not possible to implement the methods of |
Do you mean I do agree that What we're really looking for is a way to get rid of the call to In the meantime, there could be a middle ground. Let's pretend I add |
Now that
vek
has methods such asmap2
andapply2
, methods such asall
,any
andfind
would have many potential uses. For example:In addition, it might be useful to add a few utility methods specifically for boolean vectors:
.and()
and.or()
. These would provide a nice shortcut to the above code forVec3<bool>
-like types.The text was updated successfully, but these errors were encountered: