There has already been a discussion about potential currying, and I agree with the outcome. However, it would be quite convenient to have some form of partial functions implemented.
Prelude> let f x y z = x + y + z
Prelude> map (f 1 2) [1..3]
[4,5,6]
Prelude> map (^2) [1..5]
[1,4,9,16,25]
I came up with the following implementation, which I’ll admit is somewhat strange… but interesting, nonetheless.
julia> import Base: getindex
julia> getindex(f::Function, args...) = (xs...) -> f(args..., xs...)
getindex (generic function with 170 methods)
julia> f(x, y, z) = x + y + z
f (generic function with 1 method)
julia> map(f[1, 2], 1:3)
3-element Array{Int64,1}:
4
5
6
The following is slightly limited by the fact that the parser currently considers the use of the ^
operator invalid in this context (not a unary operator
), but this can be temporarily overcome by “aliasing” it.
julia> getindex(::typeof(^), n) = x -> x^n
getindex (generic function with 170 methods)
julia> pow = ^
^ (generic function with 47 methods)
julia> map(pow[2], 1:5)
5-element Array{Int64,1}:
1
4
9
16
25
Of course, this would just be syntactic sugar for using inline anonymous functions.
julia> map(x -> x^2, 1:5)
5-element Array{Int64,1}:
1
4
9
16
25
I have a hunch that this may never be implemented, potentially simply due to the fact that it could seem “un-Julian”. I’m curious to hear what others think about this concept, though. Is it something you would find useful? Is there a cleaner alternative? What side effects could this feature have - would it break anything? Why should/shouldn’t this (or something similar) be implemented? Any feedback would be much appreciated.