Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2012

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Non-sequential composition of pure functions

  • To: mathgroup at smc.vnet.net
  • Subject: [mg127803] Re: Non-sequential composition of pure functions
  • From: Sseziwa Mukasa <mukasa at gmail.com>
  • Date: Thu, 23 Aug 2012 02:55:23 -0400 (EDT)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • Delivered-to: l-mathgroup@wolfram.com
  • Delivered-to: mathgroup-newout@smc.vnet.net
  • Delivered-to: mathgroup-newsend@smc.vnet.net
  • References: <20120822062720.4DD8A6850@smc.vnet.net>

On approach:

Extract the list of conditions from the functions:

(Debug) In[49]:=
list = {(If[cond1[#[[1]]], action]) &, (If[cond2[#[[2]]],
      action]) &, (If[cond3[#[[3]]], action]) &};

(Debug) In[50]:= list /. (If[cond_, action_] &) -> (cond &)

(Debug) Out[50]= {cond1[#1[[1]]] &, cond2[#1[[2]]] &,
 cond3[#1[[3]]] &}

Then you can use Through and And to evaluate all the conditions:

(Debug) In[53]:= And @@
 Through[(list /. (If[cond_, action_] &) -> (cond &))[{arg1, arg2,
    arg3}]]

(Debug) Out[53]= cond1[arg1] && cond2[arg2] && cond3[arg3]

At this point all you need to do is extract the action from one of the
functions, or all of them if they are different, and perform based on the
result of the conditional.  Putting it all together:

(Debug) In[55]:=
combineConditionalFunctions[functions_ {If[_, _] & ..},
  arguments_ {__}] :=
 Module[{performAction =
    And @@ Through[(functions /. (If[cond_, action_] &) -> (cond &))[
       arguments]],
   action = functions[[1]] /. (If[_, action_]) & -> action},
  If[performAction, action]]

(Debug) In[56]:= combineConditionalFunctions[list, {arg1, arg2, arg3}]

(Debug) Out[56]= If[
 cond1[arg1] && cond2[arg2] && cond3[arg3], action$900]

On Wed, Aug 22, 2012 at 2:27 AM, Earl Mitchell <earl.j.mitchell at gmail.com>wrote:

>
> Hi All,
>
> I have a list of pure functions (If[Conditional[#[[1]]],Action]&
> statements) and I want to compose them into one large pure function
>
> If[FirstConditional[#[[1]]]&&SecondConditional[#[[6]]]&&ThirdConditional[#[[foo]]],Action]&.
>  They are to be applied to a list of values, and the conditionals checked
> for multiple columns for a given element of a list.
>
> I'm having problems joining these things together - to the point that I've
> considered converting them all to strings and doing the tedious
> (hackie) string manipulations to get the final function in the right form.
>  Any recommendations on how to do this?  I found Composition[] but it nests
> the functions - and I want to combine them.
>
> Thanks!
>
> Mitch
>


  • Prev by Date: Re: Defining a total derivative
  • Next by Date: Re: Non-sequential composition of pure functions
  • Previous by thread: Non-sequential composition of pure functions
  • Next by thread: Re: Non-sequential composition of pure functions