On Tue, Mar 16, 2010 at 10:53 AM, Dan Bron <[email protected]> wrote:
> When using dyad I, how does one control how to handle the various edge
> conditions gracefully (specifically out-of-bounds values and
> values exactly between intervals)?
>
> (1) y is greater or lesser than the head or tail of x
y I. x (with x in ascending order) gives the index of
the largest value in x which is not smaller than y.
So, properly speaking, y < {. x is not an edge condition
since the smallest value in x is not smaller than y
and 0 is an appropriate result.
when y > {: x we get an index which is the length
of x. For this case, we can add an element to the
list we are selecting from.
> For example, how would you solve http://rosettacode.org/wiki/Price_Fraction
> cleanly?
That problem specifies that the floating point value be in
the range 0..1.0. It is not clear to me that any
explicit treatment of invalid values would be clean.
Anyways, for a quick solution, I would
'bound replace'=:|:(3 5 { 0 ". ]);._2]0 :0
>= 0.00 < 0.06 := 0.10
>= 0.06 < 0.11 := 0.18
>= 0.11 < 0.16 := 0.26
>= 0.16 < 0.21 := 0.32
>= 0.21 < 0.26 := 0.38
>= 0.26 < 0.31 := 0.44
>= 0.31 < 0.36 := 0.50
>= 0.36 < 0.41 := 0.54
>= 0.41 < 0.46 := 0.58
>= 0.46 < 0.51 := 0.62
>= 0.51 < 0.56 := 0.66
>= 0.56 < 0.61 := 0.70
>= 0.61 < 0.66 := 0.74
>= 0.66 < 0.71 := 0.78
>= 0.71 < 0.76 := 0.82
>= 0.76 < 0.81 := 0.86
>= 0.81 < 0.86 := 0.90
>= 0.86 < 0.91 := 0.94
>= 0.91 < 0.96 := 0.98
>= 0.96 < 1.01 := 1.00
)
rescale=: replace {~ bound I. ]
If I wanted to explicitly treat error cases,
I would add guard values at the beginning
and end of that table.
For a more compact definition, I might make use of:
(-: /:~) 2-/\ replace
1
(~.,:#/.~)2-/\ replace
_0.08 _0.06 _0.04 _0.02
2 4 12 1
But if I had any expectation that the table would
receive arbitrary updates in the future, I would
not bother trying for a more compact definition.
FYI,
--
Raul
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm