Map as a partial function

Currently in Julia v0.7.0 we have

julia> [1,2,3] |> map(x->x*x)
ERROR: MethodError: no method matching (::getfield(Main, Symbol("##3#4")))()
Closest candidates are:
  #3(::Any) at REPL[0]:1
Stacktrace:
 [1] map(::getfield(Main, Symbol("##3#4"))) at .\abstractarray.jl:2033
 [2] top-level scope at none:0

But if I define

julia> Base.map(f::Function) = Base.Fix1(Base.map,f)

the results are

julia> [1,2,3] |> map(x->x*x)
3-element Array{Int64,1}:
 1
 4
 9

The latter seems useful.
Any change map(f::Function) = Fix1(map,f) could be added to Base.operators.jl ?

I think the long-term plan is to have syntax for partial application, see

https://github.com/JuliaLang/julia/pull/24990

for a recent discussion.

1 Like

Why not

[1,2,3] .|> x->x^2
9 Likes

That works. Clever and more straight forward. Didn’t know you could do that.

2 Likes

I’ve been noticing more and more that broadcast has been replacing many traditional uses of map. Obviously, they’re not completely equivalent, but for the large number of cases where they are, broadcast seems to be the emerging consensus of the Julia community (and, as far as I know, one of the first examples of a uniquely “Julian” style).

5 Likes