Enlistment for corsairing the map function (in REPL/Main only)

Hi,

I am toying with a module that end users are supposed to mainly be using via the Julia REPL, to leverage its piping capabilities.

In this specific tool’s REPL context, I would like to find a (somewhat safe, somewhat sanctionable) way to buccaneer map(f) to make it currying (thus avoiding to introduce a new function+name for that purpose), i.e.,

[1,2,3] |> map(x->x^2)

should work. (This is unlikely to be done in Julia, as, unlike the upcoming filter, corner cases of map’s current behavior exist.)

Any ideas would be much appreciated! Any other packages should be unaffected, as we want to avoid outright piracy. (Again, just the REPL use, the end user typing/memorizing experience, would be the goal here.)

My main two ideas would have been:

  1. Somehow “un-export” Base.map from Main:
    …but I found no way of removing the Main.map (alias?) from the (I suppose) “REPL module scope” Main.

  2. Check for input argument count
    I thought of overwriting the current map(f::Any)=f(), and testing for number of arguments – then either follow the original map behavior for a function that has a method with 0 args (it would fail otherwise anyway), or simple curry. Still fiddling with that… maybe via the sig field of all methods? (In this tool’s context, end users are highly unlikely to ever need or define zero-arg functions.)

(I am aware that even if a safe, REPL-only map-loot exists, that it might be considered dubious to “teach” non-standard map bahavior to users. In this specific case, end users are from a different profession, and unlikely to be exposed to this corner case map behavior in later/other Julia contexts.)

Thanks!
Martin

If you define map before you use it, then Base.map will never be imported. You could put this in startup.jl.

$ julia
               _
   _       _ _(_)_     |  Documentation: https://docs.julialang.org
  (_)     | (_) (_)    |
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 1.9.0-beta3 (2023-01-18)
 _/ |\__'_|_|_|\__'_|  |  Official https://julialang.org/ release
|__/                   |

julia> map(f) = x->Base.map(f, x)
map (generic function with 1 method)

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

julia> Base.map == map
false

julia> Base.map == Main.map
false

You could even create a package as follows.

$ julia
               _
   _       _ _(_)_     |  Documentation: https://docs.julialang.org
  (_)     | (_) (_)    |
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 1.9.0-beta3 (2023-01-18)
 _/ |\__'_|_|_|\__'_|  |  Official https://julialang.org/ release
|__/                   |

julia> baremodule MyPkg
           import Base
           export map
           map(f) = x->Base.map(f, x)
       end
Main.MyPkg

julia> import .MyPkg: map

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

julia> @which map
Main.MyPkg

Thar she blows!! Simply brilliant; thank you very much!