Or with variable arguments to the function, although this is probably
overkill:
module Enumerable
def smap(f,scope=nil,pos=0,*args)
if scope
m = scope.method(f)
else
m = Object.method(f)
end
a1 = args[0,pos]
a2 = args[pos…-1] || []
map{|a|m[*a1,a,*a2]}
end
end
def sqr(x)
x*x
end
def third(a,b,c,d,e)
c
end
p [1,2,3].smap(:sqr)
=> [1,4,9]
p [2,4].smap(:*,“xy”)
=> [“xyxy”,"xyxyxyxy]
p [1,2,3].smap(:log,Math)
p [10,100,1000].smap(:log,Math,0,10)
p [2,5].smap(:third,nil,2,0,0,0,0)
p [2,5].smap(:third,nil,1,0,0,0,0)