Skip to main content
Corrected typos; clarified justification for the approach used
Source Link
m_goldberg
  • 108.1k
  • 16
  • 104
  • 259

Belisarius' answer works well if you are willing to work with indexes. If you prefer to work within terms of the list elements themselvesrather than their positions, here is a rule-based solution.

innrJoin[datainnerJoin[data : {{_} ..}, a_, b_] := 
  data /. {x___, y : PatternSequence[{a}, ___, {b}], z___} :> {x, Flatten[{y}], z}

innrJoin[innerJoin[{{1}, {2}, {3}, {4}}, 2, 3]

{{1}, {2, 3}, {4}}

innrJoin[innerJoin[{{a}, {b}, {c}, {d}}, a, c]

{{a, b, c}, {d}}

Edit -- new and improved algorithm featuring the under appreciated PatternSequence :-)

Belisarius' answer works well if you are willing to work with indexes. If you prefer to work with the elements themselves, here is a rule-based solution.

innrJoin[data : {{_} ..}, a_, b_] := 
  data /. {x___, y : PatternSequence[{a}, ___, {b}], z___} :> {x, Flatten[{y}], z}

innrJoin[{{1}, {2}, {3}, {4}}, 2, 3]

{{1}, {2, 3}, {4}}

innrJoin[{{a}, {b}, {c}, {d}}, a, c]

{{a, b, c}, {d}}

Edit -- new and improved algorithm :-)

Belisarius' answer works well if you are willing to work with indexes. If you prefer to work in terms of the list elements rather than their positions, here is a rule-based solution.

innerJoin[data : {{_} ..}, a_, b_] := 
  data /. {x___, y : PatternSequence[{a}, ___, {b}], z___} :> {x, Flatten[{y}], z}

innerJoin[{{1}, {2}, {3}, {4}}, 2, 3]

{{1}, {2, 3}, {4}}

innerJoin[{{a}, {b}, {c}, {d}}, a, c]

{{a, b, c}, {d}}

Edit -- new and improved algorithm featuring the under appreciated PatternSequence :-)

Improved the algorithm
Source Link
m_goldberg
  • 108.1k
  • 16
  • 104
  • 259

Belisarius' answer works well if you are willing to work with indexes. If you prefer to work with the elements themselves, here is a rule-based solution.

innrJoin[data : {{_} ..}, a_, b_] := 
   data //. {x___, y : PatternSequence[{a}, y______, {b}], z___} :> 
     {x, {a, Sequence @@ Flatten[{y}], b}, z}

innrJoin[{{1}, {2}, {3}, {4}}, 2, 3]

{{1}, {2, 3}, {4}}

innrJoin[{{a}, {b}, {c}, {d}}, a, c]

{{a, b, c}, {d}}

Edit -- new and improved algorithm :-)

Belisarius' answer works well if you are willing to work with indexes. If you prefer to work with the elements themselves, here is a rule-based solution.

innrJoin[data : {{_} ..}, a_, b_] := 
   data //. {x___, {a}, y___, {b}, z___} :> 
     {x, {a, Sequence @@ Flatten[{y}], b}, z}

innrJoin[{{1}, {2}, {3}, {4}}, 2, 3]

{{1}, {2, 3}, {4}}

innrJoin[{{a}, {b}, {c}, {d}}, a, c]

{{a, b, c}, {d}}

Belisarius' answer works well if you are willing to work with indexes. If you prefer to work with the elements themselves, here is a rule-based solution.

innrJoin[data : {{_} ..}, a_, b_] := 
  data /. {x___, y : PatternSequence[{a}, ___, {b}], z___} :> {x, Flatten[{y}], z}

innrJoin[{{1}, {2}, {3}, {4}}, 2, 3]

{{1}, {2, 3}, {4}}

innrJoin[{{a}, {b}, {c}, {d}}, a, c]

{{a, b, c}, {d}}

Edit -- new and improved algorithm :-)

Source Link
m_goldberg
  • 108.1k
  • 16
  • 104
  • 259

Belisarius' answer works well if you are willing to work with indexes. If you prefer to work with the elements themselves, here is a rule-based solution.

innrJoin[data : {{_} ..}, a_, b_] := 
   data //. {x___, {a}, y___, {b}, z___} :> 
     {x, {a, Sequence @@ Flatten[{y}], b}, z}

innrJoin[{{1}, {2}, {3}, {4}}, 2, 3]

{{1}, {2, 3}, {4}}

innrJoin[{{a}, {b}, {c}, {d}}, a, c]

{{a, b, c}, {d}}