You can use SlotSequence and ConstantArray (or Table):
fun[x_List, n_Integer] := Flatten[Outer[List, ##] & @@ ConstantArray[x, n], 2]
fun[{a, b}, 3] == Tuples[{a, b}, 3]
(* True *)
Answer from rm -rf on Stack ExchangeYou can use SlotSequence and ConstantArray (or Table):
fun[x_List, n_Integer] := Flatten[Outer[List, ##] & @@ ConstantArray[x, n], 2]
fun[{a, b}, 3] == Tuples[{a, b}, 3]
(* True *)
Here's an approach with Table.
fun[x_List, n_Integer] := Flatten[Outer[List, Sequence @@ Table[x, {n}]], n-1]
examples
fun[{a, b}, 3]
{{a, a, a}, {a, a, b}, {a, b, a}, {a, b, b}, {b, a, a}, {b, a, b}, {b, b, a}, {b, b, b}}
fun[{a, b}, 4]
{{a, a, a, a}, {a, a, a, b}, {a, a, b, a}, {a, a, b, b}, {a, b, a, a}, {a, b, a, b}, {a, b, b, a}, {a, b, b, b}, {b, a, a, a}, {b, a, a, b}, {b, a, b, a}, {b, a, b, b}, {b, b, a, a}, {b, b, a, b}, {b, b, b, a}, {b, b, b, b}}
l = {1, {5, 3}, 6}
Then use:
f[Sequence @@ l, 3]
f[1, {5, 3}, 6, 3]
Sequence @@ is probably the "right" way to do this for normal functions, but you should know that you can also use SlotSequence or BlankSequence in Function or replacement respectively:
l = {1, {5, 3}, 6}
f[##, 3] & @@ l
f[1, {5, 3}, 6, 3]
l /. _[x__] :> f[x, 3]
f[1, {5, 3}, 6, 3]
These become important in the case where f holds its arguments:
SetAttributes[f, HoldAll]
f[Sequence @@ l, 3]
f[##, 3] & @@ l
l /. _[x__] :> f[x, 3]
f[Sequence @@ l, 3] f[1, {5, 3}, 6, 3] f[1, {5, 3}, 6, 3]
Note that Sequence @@ l does not evaluate.
Further distinction is apparent between the second two methods when the input is to be held:
l = Hold[1 + 1, {5!, 3/0}, 6 - 1];
This causes evaluation because the Function was not also given the HoldAll attribute:
f[##, 3] & @@ l
During evaluation of In[22]:= Power::infy: Infinite expression 1/0 encountered. >>
f[2, {120, ComplexInfinity}, 5, 3]
This does not:
l /. _[x__] :> f[x, 3]
f[1 + 1, {5!, 3/0}, 6 - 1, 3]
Reference: Injecting a sequence of expressions into a held expression
Try this:
Table[Range[i], {i, 1, 10}]
(* {{1}, {1, 2}, {1, 2, 3}, {1, 2, 3, 4}, {1, 2, 3, 4, 5}, {1, 2, 3, 4,
5, 6}, {1, 2, 3, 4, 5, 6, 7}, {1, 2, 3, 4, 5, 6, 7, 8}, {1, 2, 3, 4,
5, 6, 7, 8, 9}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}} *)
or this:
Table[Table[k, {k, 1, i}], {i, 1, 10}]
with the same result. Have fun!
My entry into the obfuscated Mathematica competition for April, 2016.
rangeList[n_Integer?Positive] := NestList[Join[#, {Last[#] + 1}] &, {1}, n - 1]
rangeList[5]
{{1}, {1, 2}, {1, 2, 3}, {1, 2, 3, 4}, {1, 2, 3, 4, 5}}
Too bad I'm posting this on April 15 rather than April 1.
It seems I have found the answer: Apply.
Apply[f, {a, b, c, d}]
gives the output:
f[a, b, c, d]
The short infix syntax for Apply (at levelspec 0) is @@:
f @@ {a, b, c, d}
f[a, b, c, d]
Of course Apply, but also:
f[{a, b, c, d} /. List -> Sequence]
f[a, b, c, d]
(my finger is hovering above the 'close' link, though)
MapIndexed[Join, data]
MapIndexed[Join]@data (*in versions 10+ *)
{{81, 15, 1}, {15, 1, 2}, {1, 68, 3}, {68, 4, 4}, ... , {63, 58, 98}, {58, 54, 99}}
I'm sure there are lots of ways. Here's one:
data = Partition[RandomSample[Range[100]],2,1];
Flatten[{data[[#]], #}] & /@ Range[Length[data]]
{{60, 77, 1}, {77, 62, 2}, {62, 37, 3}, {37, 22, 4}, ....
