The lodash _.rearg method and rearranging function arguments
I have got into the habit of starting each month with one new lodash post while the library is still on my radar. I can not say that I find myself actually using lodash that much and more, when I do it is often just one or two methods that I bother with in the framework.
1 - basic lodash _.rearg example
So the basic idea of this method is that I take a function that has a few arguments, and then I pass that function as the first argument to the lodash _.rearg method. After that the second argument that is passed is an array of index values for the arguments that can eb used to change the order of the arguments for a resulting function that will be returned.
So in other words if i have a function called func that takes three arguments, and I pass that as the first argument, and the array [0,1,2] the resulting function that is returned will call the function func in the same order, I can then change the index values of the array to change the order in which they are called.
2 - Math.atan2 use case example of lodash _.rearg
So you might be scratching your head thinking to yourself why would I ever need to use a function like this when it comes to doing something real? Well one method that comes to mind is the Math.atan2 method. This method is something that I do find myself using fro time to time when it comes to working out some logic for a game project of one kind or another. One funny thing about the method is that it accepts the y value as the first argument rather than x. I have to admit that bothers be a little because I am of the mind set that the x value is what should come first when it comes to anything that has to do with points in a 2d grid. So in this case the lodash rearg method can be sued to quickly create a new atan2 method that is just a wrapper for the native method, but with the arguments in the oder that I think they should be.
So maybe there are a few instances here in there when I am in a situation in which I would want to change the order of arguments of a function. However is it really all that hrd to just create an abstraction like this without lodash? Of course not. With that said I think I will spend the rest of the post writing about a few quick examples of doing this sort of thing without the need of lodash.
3 - Vjs example of a atan2 wrapper of Math.atan2 without lodash
So yeah why not just have a function that will call the function and return the result, with the arguments rearrange the way that I want it?
5 - Conclusion
So that is about of for this lodash method at least. I guess I could expand this post a bit more on this topic, and maybe I will at some point in the future when it comes to editing this content now and then. I could maybe see about fining some more examples where making an alternative abstraction for a method is justified.