Does Julia provide something similar to std::bind in C++? I wish to do something along the lines of:
function add(x, y)
return x + y
end
add45 = bind(add, 4, 5)
add2 = bind(add, _1, 2)
add3 = bind(add, 3, _2)
And if this is possible does it incur any performance overhead?
As answered here you can obtain this behavior using higher order functions in Julia.
Regarding the performance. There should be no overhead. Actually the compiler should inline everything in such a situation and even perform constant propagation (so that the code could actually be faster). The use of
constin the other answer here is needed only because we are working in global scope. If all this would be used within a function thenconstis not required (as the function that takes this argument will be properly compiled), so in the example below I do not useconst.Let me give an example with
Base.Fix1and youraddfunction:Note that I did not define
add2asconstand since we are in global scope I need to prefix it with$to interpolate its value into the benchmarking suite.If I did not do it you would get:
Which is essentially the same timing and memory use, but does 6 not 2 allocations since in this case
add2is a type-unstable global variable.I work on DataFrames.jl, and there using the patterns which we discuss here is very useful. Let me give just one example:
What the operation does is picking rows where values from column
:xthat are less than2.5. The key thing to understand here is what<(2.5)does. It is:so as you can see it is similar to what we would have obtained if we defined the
x -> x < 2.5function (essentially fixing the second argument of<function, as in Julia<is just a two argument function). Such shortcuts like<(2.5)above are defined in Julia by default for several common comparison operators.