Skip to main content
replaced http://mathematica.stackexchange.com/ with https://mathematica.stackexchange.com/
Source Link

The best I have is manual RHS holding and Join, after which an arbitrary head could be Applied:

Join @@ Cases[expr, x : _Times :> Hold[x], 3] 
Hold[2/2, 8/4, 1/0] 

This could be done automatically as follows:

makeHeld[(L_ -> R_) | (L_ :> R_)] := L :> HoldComplete[R]; makeHeld[pat_] := x : pat :> HoldComplete[x]; heldCases[expr_, rule_, args___] := Join @@ Cases[Unevaluated @ expr, makeHeld @ rule, args] 

I am now reasonably happy with this but I still wonder if there is a more elegant or efficient method.


For fun here is a one-line definition of makeHeld using "vanishing patterns""vanishing patterns" and Part propertiesPart properties:

makeHeld[(L_ -> R_) | (L_ :> R_) | L_] := x : L -> HoldComplete[R, x][[{1}]]; 

It's not quite as efficient and clarity suffers so I won't replace the version above with it.

The best I have is manual RHS holding and Join, after which an arbitrary head could be Applied:

Join @@ Cases[expr, x : _Times :> Hold[x], 3] 
Hold[2/2, 8/4, 1/0] 

This could be done automatically as follows:

makeHeld[(L_ -> R_) | (L_ :> R_)] := L :> HoldComplete[R]; makeHeld[pat_] := x : pat :> HoldComplete[x]; heldCases[expr_, rule_, args___] := Join @@ Cases[Unevaluated @ expr, makeHeld @ rule, args] 

I am now reasonably happy with this but I still wonder if there is a more elegant or efficient method.


For fun here is a one-line definition of makeHeld using "vanishing patterns" and Part properties:

makeHeld[(L_ -> R_) | (L_ :> R_) | L_] := x : L -> HoldComplete[R, x][[{1}]]; 

It's not quite as efficient and clarity suffers so I won't replace the version above with it.

The best I have is manual RHS holding and Join, after which an arbitrary head could be Applied:

Join @@ Cases[expr, x : _Times :> Hold[x], 3] 
Hold[2/2, 8/4, 1/0] 

This could be done automatically as follows:

makeHeld[(L_ -> R_) | (L_ :> R_)] := L :> HoldComplete[R]; makeHeld[pat_] := x : pat :> HoldComplete[x]; heldCases[expr_, rule_, args___] := Join @@ Cases[Unevaluated @ expr, makeHeld @ rule, args] 

I am now reasonably happy with this but I still wonder if there is a more elegant or efficient method.


For fun here is a one-line definition of makeHeld using "vanishing patterns" and Part properties:

makeHeld[(L_ -> R_) | (L_ :> R_) | L_] := x : L -> HoldComplete[R, x][[{1}]]; 

It's not quite as efficient and clarity suffers so I won't replace the version above with it.

added 18 characters in body
Source Link
Mr.Wizard
  • 275.2k
  • 34
  • 606
  • 1.5k

The best I have is manual RHS holding and Join, after which an arbitrary head could be Applied:

Join @@ Cases[expr, x : _Times :> Hold[x], 3] 
Hold[2/2, 8/4, 1/0] 

This could be done automatically as follows:

makeHeld[(L_ -> R_) | (L_ :> R_)] := L :> HoldComplete[R]; makeHeld[pat_] := x : pat :> HoldComplete[x]; heldCases[expr_, rule_, args___] :=   Join @@ Cases[exprCases[Unevaluated @ expr, makeHeld @ rule, args] 

I am now reasonably happy with this but I still wonder if there is a more elegant or efficient method.


For fun here is a one-line definition of makeHeld using "vanishing patterns" and Part properties:

makeHeld[(L_ -> R_) | (L_ :> R_) | L_] := x : L -> HoldComplete[R, x][[{1}]]; 

It's not quite as efficient and clarity suffers so I won't replace the version above with it.

The best I have is manual RHS holding and Join, after which an arbitrary head could be Applied:

Join @@ Cases[expr, x : _Times :> Hold[x], 3] 
Hold[2/2, 8/4, 1/0] 

This could be done automatically as follows:

makeHeld[(L_ -> R_) | (L_ :> R_)] := L :> HoldComplete[R]; makeHeld[pat_] := x : pat :> HoldComplete[x]; heldCases[expr_, rule_, args___] := Join @@ Cases[expr, makeHeld @ rule, args] 

I am now reasonably happy with this but I still wonder if there is a more elegant or efficient method.


For fun here is a one-line definition of makeHeld using "vanishing patterns" and Part properties:

makeHeld[(L_ -> R_) | (L_ :> R_) | L_] := x : L -> HoldComplete[R, x][[{1}]]; 

It's not quite as efficient and clarity suffers so I won't replace the version above with it.

The best I have is manual RHS holding and Join, after which an arbitrary head could be Applied:

Join @@ Cases[expr, x : _Times :> Hold[x], 3] 
Hold[2/2, 8/4, 1/0] 

This could be done automatically as follows:

makeHeld[(L_ -> R_) | (L_ :> R_)] := L :> HoldComplete[R]; makeHeld[pat_] := x : pat :> HoldComplete[x]; heldCases[expr_, rule_, args___] :=   Join @@ Cases[Unevaluated @ expr, makeHeld @ rule, args] 

I am now reasonably happy with this but I still wonder if there is a more elegant or efficient method.


For fun here is a one-line definition of makeHeld using "vanishing patterns" and Part properties:

makeHeld[(L_ -> R_) | (L_ :> R_) | L_] := x : L -> HoldComplete[R, x][[{1}]]; 

It's not quite as efficient and clarity suffers so I won't replace the version above with it.

added 428 characters in body
Source Link
Mr.Wizard
  • 275.2k
  • 34
  • 606
  • 1.5k

The best I have is manual RHS holding and Join, after which an arbitrary head could be Applied:

Join @@ Cases[expr, x : _Times :> Hold[x], 3] 
Hold[2/2, 8/4, 1/0] 

This could be done automatically as follows:

makeHeld[(L_ -> R_) | (L_ :> R_)] := L :> HoldComplete[R]; makeHeld[pat_] := x : pat :> HoldComplete[x]; heldCases[expr_, rule_, args___] := Join @@ Cases[expr, makeHeld @ rule, args] 

I am now reasonably happy with this but I still wonder if there is a more elegant or efficient method.

 

(Edit For fun here is a one-line definition of makeHeld using "vanishing patterns" and Part properties:

makeHeld[(L_ -> R_) | (L_ :> R_) | L_] := x : L -> HoldComplete[R, x][[{1}]]; 

It's not quite as efficient and clarity suffers so I madewon't replace the inner replacement a separate function becauseversion above with it is slightly more efficient and easier to read.)

The best I have is manual RHS holding and Join, after which an arbitrary head could be Applied:

Join @@ Cases[expr, x : _Times :> Hold[x], 3] 
Hold[2/2, 8/4, 1/0] 

This could be done automatically as follows:

makeHeld[(L_ -> R_) | (L_ :> R_)] := L :> HoldComplete[R]; makeHeld[pat_] := x : pat :> HoldComplete[x]; heldCases[expr_, rule_, args___] := Join @@ Cases[expr, makeHeld @ rule, args] 

I wonder if there is a more elegant or efficient method.

(Edit: I made the inner replacement a separate function because it is slightly more efficient and easier to read.)

The best I have is manual RHS holding and Join, after which an arbitrary head could be Applied:

Join @@ Cases[expr, x : _Times :> Hold[x], 3] 
Hold[2/2, 8/4, 1/0] 

This could be done automatically as follows:

makeHeld[(L_ -> R_) | (L_ :> R_)] := L :> HoldComplete[R]; makeHeld[pat_] := x : pat :> HoldComplete[x]; heldCases[expr_, rule_, args___] := Join @@ Cases[expr, makeHeld @ rule, args] 

I am now reasonably happy with this but I still wonder if there is a more elegant or efficient method.

 

For fun here is a one-line definition of makeHeld using "vanishing patterns" and Part properties:

makeHeld[(L_ -> R_) | (L_ :> R_) | L_] := x : L -> HoldComplete[R, x][[{1}]]; 

It's not quite as efficient and clarity suffers so I won't replace the version above with it.

deleted 50 characters in body
Source Link
Mr.Wizard
  • 275.2k
  • 34
  • 606
  • 1.5k
Loading
added 63 characters in body
Source Link
Mr.Wizard
  • 275.2k
  • 34
  • 606
  • 1.5k
Loading
Source Link
Mr.Wizard
  • 275.2k
  • 34
  • 606
  • 1.5k
Loading