Re: Get Parts of Expression by Position list
- To: mathgroup at christensen.cybernetics.net
- Subject: [mg391] Re: Get Parts of Expression by Position list
- From: Xah Y Lee <xyl10060 at fhda.edu>
- Date: Mon, 9 Jan 1995 00:19:41 -0800 (PST)
>Problem: How to write a function that work like Part[] but
>uses the syntax returned by Position[] to specify parts of expression.
Dearly Beloved MathGroupers,
I made a blunder in my previous message.
The reason for my Part function not to work is because myPart should be
{{3,2,3},{3,1,2},{5}} not {{3,2,3},{3,1,2},5}.
It will work now:
--
myTree = Insert[Range[20],{{a,b,c}, Range[100,105]}, 3];
myParts = {{3,2,3},{3,1,2},{5}};
Fold[ Part[#1,#2]&, myTree, #]& /@ myParts
--
or, we could write a function. The function will emulate Part[] but uses
the syntax returned by Position[] to specify parts of expression.
--
Clear[myPartFunction]
myPartFunction[ tree_List, pos_List] :=
Fold[ Part[#1,#2]&, tree, #]& /@ pos
--
I'd be interested to know from WRI why they decided
for Delete[] and Part[] to use different syntax as their argument.
It seems to me functions like Part[] or Take[] would be better off using
the syntax returned by Position[] like in Delete[].
There once was a brainy baboon | Xah Lee
Who always breathed down a bassoon,| xyl10060 at tiptoe.fhda.edu
For he said, "It appears |
That in billion of years |
I shall certainly hit on a tune." <-- Author not known to me.