Skip to main content
Defined an operator and added support for non-pure functions.
Source Link
WReach
  • 69.8k
  • 4
  • 167
  • 275

If the goal is to apply a symbolic code transformationWe could define an operator combine that generates a composite pure function fromtransforms a list of individualfunctions into another function that tests if all the originals return true when applied to an argument.

If we know for certain that all of the initial functions are pure slot-based functions (as in the question), thenwe can define the operator using Distribute can be used:

conscombine[constraints:{Function[_]..}] := Distribute[And @@ constraints, Function, And] 

So then:

Select[{con1[#]&10, con2[#]&21, con3[#]&30, 41}, combine[{# > 15 &, # < 35 &}]] Distribute[And(* @@{21, cons30} *) combine[{con1[#] &, Functioncon2[#] &, And]con3[#] &}] (* con1[#1] && con2[#1] && con3[#1] & *) 

But ifNote that the goaloperator is explicitly defined to simply evaluate theoperate only upon lists of pure slot-based functions and determine if they all return true. If it is desirable to support any kinds of functions, then we could use this more elaborate form:

combine[constraints_List] := AllTrue[constraints, OperatorApplied[Construct][#]]& 

So then:

Select[{10, 21, 30, 41}, combine[{GreaterThan[15], OddQ, #<35&}]] (* {21} *) combine[{f, g, h}] (* AllTrue[{f, g, h}, OperatorApplied[Construct][#1]] & *) 

Both of these definitions have the other answers involving AllTrue, Through, ComapApply, etcfeature that they will stop evaluating functions as soon as the first returns false. That is, unneeded function evaluations are more appropriateavoided.

If the goal is to apply a symbolic code transformation that generates a composite pure function from a list of individual pure functions, then Distribute can be used:

cons = {con1[#]&, con2[#]&, con3[#]&} Distribute[And @@ cons, Function, And] (* con1[#1] && con2[#1] && con3[#1] & *) 

But if the goal is to simply evaluate the functions and determine if they all return true, then the other answers involving AllTrue, Through, ComapApply, etc. are more appropriate.

We could define an operator combine that transforms a list of functions into another function that tests if all the originals return true when applied to an argument.

If we know for certain that all of the initial functions are pure slot-based functions (as in the question), we can define the operator using Distribute:

combine[constraints:{Function[_]..}] := Distribute[And @@ constraints, Function, And] 

So then:

Select[{10, 21, 30, 41}, combine[{# > 15 &, # < 35 &}]] (* {21, 30} *) combine[{con1[#] &, con2[#] &, con3[#] &}] (* con1[#1] && con2[#1] && con3[#1] & *) 

Note that the operator is explicitly defined to operate only upon lists of pure slot-based functions. If it is desirable to support any kinds of functions, then we could use this more elaborate form:

combine[constraints_List] := AllTrue[constraints, OperatorApplied[Construct][#]]& 

So then:

Select[{10, 21, 30, 41}, combine[{GreaterThan[15], OddQ, #<35&}]] (* {21} *) combine[{f, g, h}] (* AllTrue[{f, g, h}, OperatorApplied[Construct][#1]] & *) 

Both of these definitions have the feature that they will stop evaluating functions as soon as the first returns false. That is, unneeded function evaluations are avoided.

Added some context for this answer.
Source Link
WReach
  • 69.8k
  • 4
  • 167
  • 275

If the goal is to apply a symbolic code transformation that generates a composite pure function from a list of individual pure functions, then Distribute can be used:

cons = {con1[#]&, con2[#]&, con3[#]&} Distribute[And @@ cons, Function, And] (* con1[#1] && con2[#1] && con3[#1] & *) ``` 

But if the goal is to simply evaluate the functions and determine if they all return true, then the other answers involving AllTrue, Through, ComapApply, etc. are more appropriate.

If the goal is to apply a symbolic code transformation that generates a composite pure function from a list of individual pure functions, then Distribute can be used:

cons = {con1[#]&, con2[#]&, con3[#]&} Distribute[And @@ cons, Function, And] (* con1[#1] && con2[#1] && con3[#1] & *) ``` 

If the goal is to apply a symbolic code transformation that generates a composite pure function from a list of individual pure functions, then Distribute can be used:

cons = {con1[#]&, con2[#]&, con3[#]&} Distribute[And @@ cons, Function, And] (* con1[#1] && con2[#1] && con3[#1] & *) 

But if the goal is to simply evaluate the functions and determine if they all return true, then the other answers involving AllTrue, Through, ComapApply, etc. are more appropriate.

deleted 5 characters in body
Source Link
WReach
  • 69.8k
  • 4
  • 167
  • 275

If the goal is to apply a symbolic code transformation that generates a composite pure function from a list of individual pure functions, then then Distribute can be used:

cons = {con1[#]&, con2[#]&, con3[#]&} Distribute[And @@ cons, Function, And] (* con1[#1] && con2[#1] && con3[#1] & *) ``` 

If the goal is to apply a symbolic code transformation that generates a composite pure function from a list of individual pure functions, then then Distribute can be used:

cons = {con1[#]&, con2[#]&, con3[#]&} Distribute[And @@ cons, Function, And] (* con1[#1] && con2[#1] && con3[#1] & *) ``` 

If the goal is to apply a symbolic code transformation that generates a composite pure function from a list of individual pure functions, then Distribute can be used:

cons = {con1[#]&, con2[#]&, con3[#]&} Distribute[And @@ cons, Function, And] (* con1[#1] && con2[#1] && con3[#1] & *) ``` 
Source Link
WReach
  • 69.8k
  • 4
  • 167
  • 275
Loading