While reformatting Szabolcs's code from (42660) I noticed this interesting operation:
expr /. {{-∞, mid___, ∞} :> { mid }, {-∞, mid___ } :> { mid, ∞}, { mid___, ∞} :> {-∞, mid }, { mid___ } :> {-∞, mid, ∞}} Essentially if a given expression (-∞) at the beginning of a List is present, remove it, but if it is absent, add it. Likewise for ∞ at the end of the list. An empty list {} should become {-∞, ∞}, while {-∞, ∞} itself should become {}. Examples:
Replace[ {{1, 2, 3}, {-∞, 1, 2}, {1, 2, ∞}, {-∞, 1, 2, ∞}, {-∞, ∞}, {∞}, {-∞}, {}}, {{-∞, mid___, ∞} :> { mid }, {-∞, mid___ } :> { mid, ∞}, { mid___, ∞} :> {-∞, mid }, { mid___ } :> {-∞, mid, ∞}}, {1} ] {{-∞, 1, 2, 3, ∞}, {1, 2, ∞}, {-∞, 1, 2}, {1, 2}, {}, {-∞}, {∞}, {-∞, ∞}}
How might this operation be done with a single replacement rule, or cleanly with a different method?