RE: Test of a pure function
- To: mathgroup at smc.vnet.net
- Subject: [mg34038] RE: [mg34010] Test of a pure function
- From: "DrBob" <majort at cox-internet.com>
- Date: Sat, 27 Apr 2002 00:57:04 -0400 (EDT)
- Reply-to: <drbob at bigfoot.com>
- Sender: owner-wri-mathgroup at wolfram.com
A pure function isn't always the way to go --- for maintainability of
the code, if nothing else. Look at this approach:
xBins=Union[Sort[Random[Integer,{1,100}]&/@Range[10]]]
{1,39,46,54,55,66,75,85,96,100}
bracket[x_]:=
Flatten[Position[
IntervalMemberQ[#,x]&/@
Interval/@
Transpose[{Join[{-\[Infinity]},xBins],Join[xBins,{\[Infinity]}]}],
True]]
bracket/@Join[{-20},xBins,{200}]
{{1},{1,2},{2,3},{3,4},{4,5},{5,6},{6,7},{7,8},{8,9},{9,10},{10,11},{11}
}
This function gives the positions in xBins of elements that bracket x
or, if x < Min[xBins] it gives {1}, and if x > Max[xBins] it gives
{Length[xBins]+1}. The two special cases return singletons while usual
cases result in at least two elements returned.
It seems to me that a good solution might involve ListCorrelate, but I
haven't been able to think that through as yet.
Bobby Treat
-----Original Message-----
From: Shawn O'Connor [mailto:soconnor at ccs.nrl.navy.mil]
To: mathgroup at smc.vnet.net
Subject: [mg34038] [mg34010] Test of a pure function
Hello,
I hope someone can help me with this I know the solution should be
easy
but I am still learning how pure functions work.
I am trying to pick out a position in a list were a number falls i.e.
Position[xBins, _?(#1<=x <= #2 &)]. I keep getting errors Like these
Function::slotn: Slot number 2 in #1¾x¾#2& cannot be filled
from \
(#1¾x¾#2&)[List].
Function::slotn: Slot number 2 in
#1¾x¾#2& cannot be filled from \
(#1¾x¾#2&)[-1].
From
In[83]:=
Function::slotn: Slot number 2 in #1¾x¾#2& cannot be filled from
\
(#1¾x¾#2&)[-0.995].
General::stop: Further output of
Function::slotn will be suppressed during \
this calculation.
Can I make a comparison with the nth and nth+1 element in a pure
function.
Thank you,