Quantcast
Channel: bind specific 'Slots' to specific 'Map' - Mathematica Stack Exchange
Viewing all articles
Browse latest Browse all 3

Answer by WReach for bind specific 'Slots' to specific 'Map'

$
0
0

There is no direct notation for anonymous slot references to outer pure function arguments (named arguments to Function having been ruled out). But we can use the higher-order function OperatorApplied as a way to avoid explicit argument naming:

OperatorApplied[Take, 2][#] /@ Range@Length[#] & /@ test(* {{{4}, {4,2}, {4,2,2}},    {{9}, {9,1}, {9,1,5}},    {{5}, {5,2}, {5,2,9}, {5,2,9,3}},    {{5}, {5,2}, {5,2,7}, {5,2,7,1}, {5,2,7,1,1}}} *)

or, equivalently:

OperatorApplied[Take[#2, #]&][#] /@ Range@Length[#] & /@ test

For this particular case, CurryApplied could be substituted for OperatorApplied. We could also use Curry (my old favourite) but it has been deprecated and slated for removal some day.

See (197168) for more discussion of this currying technique within nested pure functions.


Just For Fun

If Wolfram Language had some kind of forking operator, we could dispense with slot notation for this example and write in point-free form:

fork[f_][g_, h_][x_] = f[g[x], h[x]]test // Map@fork[Map][Curry[Take,2], Range@*Length](* {{{4}, {4,2}, {4,2,2}},    {{9}, {9,1}, {9,1,5}},    {{5}, {5,2}, {5,2,9}, {5,2,9,3}},    {{5}, {5,2}, {5,2,7}, {5,2,7,1}, {5,2,7,1,1}}} *)

The Query sublanguage has something resembling a fork operator, so we can write:

test // Query[All, Apply[Map]@*{Curry[Take, 2], Range@*Length}]

Viewing all articles
Browse latest Browse all 3

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>