Function Repository Resource:

CopyMachine

Source Notebook

Create an operator for copying expressions

Contributed by: Bob Sandheinrich

ResourceFunction["CopyMachine"][n][expr]

creates a list of n copies of expr.

ResourceFunction["CopyMachine"][n,h][expr]

wraps n copies of expr in h.

ResourceFunction["CopyMachine"][{n1,n2,},][expr]

creates an n1n2 array.

ResourceFunction["CopyMachine"][]

represents a copy machine.

Details

ResourceFunction["CopyMachine"] is an operator that is commonly used in Dataset and Query.

Examples

Basic Examples (3) 

Create an operator for making ten copies:

In[1]:=
copier = ResourceFunction["CopyMachine"][10]
Out[1]=

Make ten copies:

In[2]:=
copier["Yo"]
Out[2]=

Create an operator that creates copies within a string:

In[3]:=
catenater = ResourceFunction["CopyMachine"][10, StringJoin]
Out[3]=
In[4]:=
catenater["Yo"]
Out[4]=

Create a 3×2 array using an operator:

In[5]:=
arrayifier = ResourceFunction["CopyMachine"][{3, 2}]
Out[5]=
In[6]:=
arrayifier[x]
Out[6]=

Scope (2) 

Import a dataset:

In[7]:=
ds = ResourceData["Sample Data: Fisher's Irises"]
Out[7]=

Create a dataset that only contains five copies of the last row:

In[8]:=
ds[-1, ResourceFunction["CopyMachine"][5]]
Out[8]=

Copy the fourth row three times:

In[9]:=
ds[{4 -> ResourceFunction["CopyMachine"][3, Sequence]}]
Out[9]=

Create random data in a list:

In[10]:=
data = RandomInteger[10, 20]
Out[10]=

Splice five copies of the second element into the data:

In[11]:=
MapAt[ResourceFunction["CopyMachine"][5, Sequence], data, 2]
Out[11]=

Applications (1) 

Give Honest Abe a vocal quirk:

In[12]:=
StringRiffle[
 With[{n = Interpreter["SemanticInteger"][#]}, If[IntegerQ[n], ResourceFunction["CopyMachine"][n, Sequence], Identity]][#] & /@ TextWords@First@TextSentences@ResourceData["Gettysburg Address"]]
Out[12]=

Properties and Relations (3) 

For arrays, the head h is used at all levels:

In[13]:=
ResourceFunction["CopyMachine"][{4, 3, 2}, h][x]
Out[13]=

Using zero gives an empty result:

In[14]:=
ResourceFunction["CopyMachine"][0][x]
Out[14]=

Negative repetitions are not supported:

In[15]:=
ResourceFunction["CopyMachine"][-2][x]
Out[15]=

Publisher

Bob

Version History

  • 1.0.0 – 15 March 2022

Related Resources

Author Notes

The motivation for creating CopyMachine was to be able to splice into structures like datasets.

License Information