3 simplify code
source | link

There is a rule/replacement based method using ReplaceList and BlankNullSequence that I think is elegant and easy to understand.

ReplaceList[{1, 2, 3}, {a___, b___} :> {a, x, b}]
{{x, 1, 2, 3}, {1, x, 2, 3}, {1, 2, x, 3}, {1, 2, 3, x}}
ReplaceList[{1, 2, 3}, {a___, b___, c___} :> {a, x, b, x, c}]
{{x, x, 1, 2, 3}, {x, 1, x, 2, 3}, {x, 1, 2, x, 3}, {x, 1, 2, 3, x}, {1, x, x, 2, 3},
 {1, x, 2, x, 3}, {1, x, 2, 3, x}, {1, 2, x, x, 3}, {1, 2, x, 3, x}, {1, 2, 3, x, x}}

The only difficulty is extending this to an arbitrary number of insertions. For that we need meta-programming, that is code that generates code:

sprinkle[v_List, x_, n_Integer?Positive] :=
  Table[Unique["a", Temporary], {n + 1}] /. syms_ :>
    ReplaceList[v, RuleDelayed @@ {Pattern[#, ___] & /@ syms, -> Riffle[syms, x]}]x]]

Now:

sprinkle[{1, 2, 3}, x, 4]
{{x, x, x, x, 1, 2, 3}, {x, x, x, 1, x, 2, 3}, {x, x, x, 1, 2, x, 3}, ... }

Your original example may be had with:

A = {{1, 2, 3, 1, 2, 3}, {1, 1, 2, 2, 3, 3}};
Join @@ (sprinkle[#, a, 2] & /@ A)

You can leave off the Join @@ if you want two sets of sublists; I could not tell form your question if that was desired.

Note: using a capital letter to start a user Symbol name is often a bad idea as these may conflict with internal functions – I preserved your original name for clarity alone.

There is a rule/replacement based method using ReplaceList and BlankNullSequence that I think is elegant and easy to understand.

ReplaceList[{1, 2, 3}, {a___, b___} :> {a, x, b}]
{{x, 1, 2, 3}, {1, x, 2, 3}, {1, 2, x, 3}, {1, 2, 3, x}}
ReplaceList[{1, 2, 3}, {a___, b___, c___} :> {a, x, b, x, c}]
{{x, x, 1, 2, 3}, {x, 1, x, 2, 3}, {x, 1, 2, x, 3}, {x, 1, 2, 3, x}, {1, x, x, 2, 3},
 {1, x, 2, x, 3}, {1, x, 2, 3, x}, {1, 2, x, x, 3}, {1, 2, x, 3, x}, {1, 2, 3, x, x}}

The only difficulty is extending this to an arbitrary number of insertions. For that we need meta-programming, that is code that generates code:

sprinkle[v_List, x_, n_Integer?Positive] :=
  Table[Unique["a", Temporary], {n + 1}] /. syms_ :>
    ReplaceList[v, RuleDelayed @@ {Pattern[#, ___] & /@ syms, Riffle[syms, x]}]

Now:

sprinkle[{1, 2, 3}, x, 4]
{{x, x, x, x, 1, 2, 3}, {x, x, x, 1, x, 2, 3}, {x, x, x, 1, 2, x, 3}, ... }

Your original example may be had with:

A = {{1, 2, 3, 1, 2, 3}, {1, 1, 2, 2, 3, 3}};
Join @@ (sprinkle[#, a, 2] & /@ A)

You can leave off the Join @@ if you want two sets of sublists; I could not tell form your question if that was desired.

Note: using a capital letter to start a user Symbol name is often a bad idea as these may conflict with internal functions – I preserved your original name for clarity alone.

There is a rule/replacement based method using ReplaceList and BlankNullSequence that I think is elegant and easy to understand.

ReplaceList[{1, 2, 3}, {a___, b___} :> {a, x, b}]
{{x, 1, 2, 3}, {1, x, 2, 3}, {1, 2, x, 3}, {1, 2, 3, x}}
ReplaceList[{1, 2, 3}, {a___, b___, c___} :> {a, x, b, x, c}]
{{x, x, 1, 2, 3}, {x, 1, x, 2, 3}, {x, 1, 2, x, 3}, {x, 1, 2, 3, x}, {1, x, x, 2, 3},
 {1, x, 2, x, 3}, {1, x, 2, 3, x}, {1, 2, x, x, 3}, {1, 2, x, 3, x}, {1, 2, 3, x, x}}

The only difficulty is extending this to an arbitrary number of insertions. For that we need meta-programming, that is code that generates code:

sprinkle[v_List, x_, n_Integer?Positive] :=
  Table[Unique["a", Temporary], {n + 1}] /. syms_ :>
    ReplaceList[v, Pattern[#, ___] & /@ syms -> Riffle[syms, x]]

Now:

sprinkle[{1, 2, 3}, x, 4]
{{x, x, x, x, 1, 2, 3}, {x, x, x, 1, x, 2, 3}, {x, x, x, 1, 2, x, 3}, ... }

Your original example may be had with:

A = {{1, 2, 3, 1, 2, 3}, {1, 1, 2, 2, 3, 3}};
Join @@ (sprinkle[#, a, 2] & /@ A)

You can leave off the Join @@ if you want two sets of sublists; I could not tell form your question if that was desired.

Note: using a capital letter to start a user Symbol name is often a bad idea as these may conflict with internal functions – I preserved your original name for clarity alone.

2 added 95 characters in body
source | link

There is a rule/replacement based method using ReplaceList and BlankNullSequence that I think is elegant and easy to understand.

ReplaceList[{1, 2, 3}, {a___, b___} :> {a, x, b}]
{{x, 1, 2, 3}, {1, x, 2, 3}, {1, 2, x, 3}, {1, 2, 3, x}}
ReplaceList[{1, 2, 3}, {a___, b___, c___} :> {a, x, b, x, c}]
{{x, x, 1, 2, 3}, {x, 1, x, 2, 3}, {x, 1, 2, x, 3}, {x, 1, 2, 3, x}, {1, x, x, 2, 3},
 {1, x, 2, x, 3}, {1, x, 2, 3, x}, {1, 2, x, x, 3}, {1, 2, x, 3, x}, {1, 2, 3, x, x}}

The only difficulty is extending this to an arbitrary number of insertions. For that we need meta-programming, that is code that generates code:

sprinkle[v_List, x_, n_Integer?Positive] :=
  Table[Unique["a", Temporary], {n + 1}] /. syms_ :>
    ReplaceList[v, RuleDelayed @@ {Pattern[#, ___] & /@ syms, Riffle[syms, x]}]

Now:

sprinkle[{1, 2, 3}, "a"x, 4]
{{"a"x, "a"x, "a"x, "a"x, 1, 2, 3}, {"a"x, "a"x, "a"x, 1, "a"x, 2, 3}, {x, x, x, 1, 2, x, 3}, ... }

Your original example may be had with:

A = {{1, 2, 3, 1, 2, 3}, {1, 1, 2, 2, 3, 3}};
Join @@ (sprinkle[#, "a"a, 2] & /@ A)

You can leave off the Join @@ if you want two sets of sublists; I could not tell form your question if that was desired.

Note: using a capital letter to start a user Symbol name is often a bad idea as these may conflict with internal functions – I preserved your original name for clarity alone.

There is a rule/replacement based method using ReplaceList that I think is elegant and easy to understand.

ReplaceList[{1, 2, 3}, {a___, b___} :> {a, x, b}]
{{x, 1, 2, 3}, {1, x, 2, 3}, {1, 2, x, 3}, {1, 2, 3, x}}
ReplaceList[{1, 2, 3}, {a___, b___, c___} :> {a, x, b, x, c}]
{{x, x, 1, 2, 3}, {x, 1, x, 2, 3}, {x, 1, 2, x, 3}, {x, 1, 2, 3, x}, {1, x, x, 2, 3},
 {1, x, 2, x, 3}, {1, x, 2, 3, x}, {1, 2, x, x, 3}, {1, 2, x, 3, x}, {1, 2, 3, x, x}}

The only difficulty is extending this to an arbitrary number of insertions. For that we need meta-programming, that is code that generates code:

sprinkle[v_List, x_, n_Integer?Positive] :=
  Table[Unique["a", Temporary], {n + 1}] /. syms_ :>
    ReplaceList[v, RuleDelayed @@ {Pattern[#, ___] & /@ syms, Riffle[syms, x]}]

Now:

sprinkle[{1, 2, 3}, "a", 4]
{{"a", "a", "a", "a", 1, 2, 3}, {"a", "a", "a", 1, "a", 2, 3}, ... }

Your original example may be had with:

A = {{1, 2, 3, 1, 2, 3}, {1, 1, 2, 2, 3, 3}};
Join @@ (sprinkle[#, "a", 2] & /@ A)

You can leave off the Join @@ if you want two sets of sublists; I could not tell form your question if that was desired.

There is a rule/replacement based method using ReplaceList and BlankNullSequence that I think is elegant and easy to understand.

ReplaceList[{1, 2, 3}, {a___, b___} :> {a, x, b}]
{{x, 1, 2, 3}, {1, x, 2, 3}, {1, 2, x, 3}, {1, 2, 3, x}}
ReplaceList[{1, 2, 3}, {a___, b___, c___} :> {a, x, b, x, c}]
{{x, x, 1, 2, 3}, {x, 1, x, 2, 3}, {x, 1, 2, x, 3}, {x, 1, 2, 3, x}, {1, x, x, 2, 3},
 {1, x, 2, x, 3}, {1, x, 2, 3, x}, {1, 2, x, x, 3}, {1, 2, x, 3, x}, {1, 2, 3, x, x}}

The only difficulty is extending this to an arbitrary number of insertions. For that we need meta-programming, that is code that generates code:

sprinkle[v_List, x_, n_Integer?Positive] :=
  Table[Unique["a", Temporary], {n + 1}] /. syms_ :>
    ReplaceList[v, RuleDelayed @@ {Pattern[#, ___] & /@ syms, Riffle[syms, x]}]

Now:

sprinkle[{1, 2, 3}, x, 4]
{{x, x, x, x, 1, 2, 3}, {x, x, x, 1, x, 2, 3}, {x, x, x, 1, 2, x, 3}, ... }

Your original example may be had with:

A = {{1, 2, 3, 1, 2, 3}, {1, 1, 2, 2, 3, 3}};
Join @@ (sprinkle[#, a, 2] & /@ A)

You can leave off the Join @@ if you want two sets of sublists; I could not tell form your question if that was desired.

Note: using a capital letter to start a user Symbol name is often a bad idea as these may conflict with internal functions – I preserved your original name for clarity alone.

1
source | link

There is a rule/replacement based method using ReplaceList that I think is elegant and easy to understand.

ReplaceList[{1, 2, 3}, {a___, b___} :> {a, x, b}]
{{x, 1, 2, 3}, {1, x, 2, 3}, {1, 2, x, 3}, {1, 2, 3, x}}
ReplaceList[{1, 2, 3}, {a___, b___, c___} :> {a, x, b, x, c}]
{{x, x, 1, 2, 3}, {x, 1, x, 2, 3}, {x, 1, 2, x, 3}, {x, 1, 2, 3, x}, {1, x, x, 2, 3},
 {1, x, 2, x, 3}, {1, x, 2, 3, x}, {1, 2, x, x, 3}, {1, 2, x, 3, x}, {1, 2, 3, x, x}}

The only difficulty is extending this to an arbitrary number of insertions. For that we need meta-programming, that is code that generates code:

sprinkle[v_List, x_, n_Integer?Positive] :=
  Table[Unique["a", Temporary], {n + 1}] /. syms_ :>
    ReplaceList[v, RuleDelayed @@ {Pattern[#, ___] & /@ syms, Riffle[syms, x]}]

Now:

sprinkle[{1, 2, 3}, "a", 4]
{{"a", "a", "a", "a", 1, 2, 3}, {"a", "a", "a", 1, "a", 2, 3}, ... }

Your original example may be had with:

A = {{1, 2, 3, 1, 2, 3}, {1, 1, 2, 2, 3, 3}};
Join @@ (sprinkle[#, "a", 2] & /@ A)

You can leave off the Join @@ if you want two sets of sublists; I could not tell form your question if that was desired.