Alex,

clamp-to-range is implemented as you suggested in git. However, looking at how it's used, we could remove a bunch of code from math.ranges and simplify things.

Using clamp instead of clamp-to-range:

IN: jamshred.tunnel

: clamp-length ( n seq -- n' )
    0 swap length clamp ;

: sub-tunnel ( from to segments -- segments )
    [ '[ _ clamp-length ] bi@ ] keep <slice> ;

: get-segment ( segments n -- segment )
    over clamp-length swap nth ;

IN: jamshred.player

: change-player-speed ( inc player -- )
    [ + 0 max-speed clamp ] change-speed drop ;

: multiply-player-speed ( n player -- )
    [ * 0 max-speed clamp ] change-speed drop ;


We could remove speed-range from jamshred.player and this code from math.ranges:

-: range-increasing? ( range -- ? )
-    step>> 0 > ;
-
-: range-decreasing? ( range -- ? )
-    step>> 0 < ;
-
-: first-or-last ( seq head? -- elt )
-    [ first ] [ last ] if ;
-
-: range-min ( range -- min )
-    dup range-increasing? first-or-last ;
-
-: range-max ( range -- max )
-    dup range-decreasing? first-or-last ;
-
-: clamp-to-range ( n range -- n )
-    [ range-min ] [ range-max ] bi clamp ;
-
-: sequence-index-range  ( seq -- range )
-    length [0,b) ;

Nothing else uses these words.  How does that look?

Doug


On May 25, 2009, at 7:49 PM, Alex Chapman wrote:

Hi Doug,

This clamp functionality is already present in math.ranges as clamp- to-range:

( scratchpad ) 100 -20 0 [a,b] clamp-to-range .
0
( scratchpad ) 100 20 0 [a,b] clamp-to-range .
20
( scratchpad ) 100 120 0 [a,b] clamp-to-range .
100

Not as clean obviously, but more general. Maybe clamp-to-range can be simplified from:

: clamp-to-range ( n range -- n )
    [ range-min max ] [ range-max min ] bi ;

to:

: clamp-to-range ( n range -- n )
    [ range-min ] [ range-max ] bi clamp ;

Alex

2009/5/26 Doug Coleman <[email protected]>
Hi everyone,

I added a ``clamp'' word to math.order for clamping a value to an
interval.  Writing min/max by hand is error-prone and the pattern
comes up fairly often.

( scratchpad ) -20 0 100 clamp .
0
( scratchpad ) 20 0 100 clamp .
20
( scratchpad ) 120 0 100 clamp .
100

The word 'at-default' has been removed because it was only used in a
few places, it's just ``?at drop'' and because it had no docs.

Someday soon ``peek'' will be renamed to ``last'' since not all
sequences should be thought of as stacks and because first/last is
more congruent than first/peek.

As always, if anyone notices inconsistencies or patterns for which a
word should exist, let us know.

Happy factoring,
Doug

------------------------------------------------------------------------------
Register Now for Creativity and Technology (CaT), June 3rd, NYC. CaT
is a gathering of tech-side developers & brand creativity professionals. Meet
the minds behind Google Creative Lab, Visual Complexity, Processing, &
iPhoneDevCamp asthey present alongside digital heavyweights like Barbarian
Group, R/GA, & Big Spaceship. http://www.creativitycat.com
_______________________________________________
Factor-talk mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/factor-talk

------------------------------------------------------------------------------
Register Now for Creativity and Technology (CaT), June 3rd, NYC. CaT
is a gathering of tech-side developers & brand creativity professionals. Meet
the minds behind Google Creative Lab, Visual Complexity, Processing, &
iPhoneDevCamp asthey present alongside digital heavyweights like Barbarian Group, R/GA, & Big Spaceship. http://www.creativitycat.com _______________________________________________
Factor-talk mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/factor-talk

------------------------------------------------------------------------------
Register Now for Creativity and Technology (CaT), June 3rd, NYC. CaT
is a gathering of tech-side developers & brand creativity professionals. Meet
the minds behind Google Creative Lab, Visual Complexity, Processing, & 
iPhoneDevCamp asthey present alongside digital heavyweights like Barbarian
Group, R/GA, & Big Spaceship. http://www.creativitycat.com 
_______________________________________________
Factor-talk mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/factor-talk

Reply via email to