5 ms·
The "whatever star" seems like a less-well-implemented version of Wolfram Language's pure anonymous functions: http://www.wolfram.com/language/elementary-intro
by bloaf 9y ago
The "whatever star" seems like a less-well-implemented version of Wolfram Language's pure anonymous functions:
http://www.wolfram.com/language/elementary-introduction/2nd-ed/26-pure-anonymous-functions.html http://www.wolfram.com/language/elementary-introduction/2nd-...
Essentially, WL uses # instead of * and requires a "&" to indicate when you're done defining your function. So the WL equivalent to Perl's
(1, 2, 3).map: * * 2
is
Map[ #*2&, {1,2,3}]
(idiomatically you would use /@ as shorthand for Map[], so it would be)
#*2&/@{1,2,3}
Except WL is far more general with what you can do with #, and you can do things like this:
Outer[ #1 * #2 &, {1,2,3}, {4,5,6}]
which returns
{{4, 5, 6}, {8, 10, 12}, {12, 15, 18}}
because #1 * #2 & defines a function of two arguments, and "Outer" feeds the lists into that function. You can even use ## to refer to all the arguments passed into that function (regardless of how many arguments there are), so
Outer[## &, {1, 2, 3}, {4, 5, 6}]
returns
{{1, 4, 1, 5, 1, 6}, {2, 4, 2, 5, 2, 6}, {3, 4, 3, 5, 3, 6}}
And you can even do stuff like:
f[x_] := x^2
Outer[ #1[#2] &, {e, f, g}, {4, 5, 6}]
to get
{{e[4], e[5], e[6]}, {16, 25, 36}, {g[4], g[5], g[6]}}
- perlgeek 9y agoif you want a more general way to create anonymous functions in Perl 6, it's not that many more keystrokes away: sub ($a, $b) { $a + $b, [1, 2, 3], [4, 5, 6] } Or even sub { $^a + $^b, [1, 2, 3], [4, 5, 6] } So yes, the whatever-star isn't the most powerful thing imaginable, but it's good enough for very simple cases, and more powerful mechanism are available too.
- bloaf 9y agoBut that's my point. Mathematica's # syntax is both. (i.e. it is good enough for very simple cases, and it is the more powerful mechanism.)
- lmm 9y agoScala had it before WL, with "_" which can be used with two or more arguments where necessary. I suspect other languages had it earlier than that. It's a very useful shorthand, although it becomes less necessary with properly first class functions and/or currying by default. E.g. I don't know if Haskell has it at all, but it doesn't need it.
- sbmassey 9y agoMathematica has had that syntax since the 1990's at least. Plus there's good old q's implicit argument naming of x, y, z when you didn't give the args explicit names: each[{2*x}] 1 2 3 1 2 3 {x*y}/: 4 5 6
- bloaf 9y agohttp://reference.wolfram.com/language/ref/Slot.html?q=Slot http://reference.wolfram.com/language/ref/Slot.html?q=Slot At the bottom of the page, it says: >Introduced in 1988 (1.0) So WL had this syntax several years before the JVM was even invented.
- lmm 9y agoHmm, I thought I remembered WL being announced a few years ago, long after I was using this stuff on the JVM.