@@ -525,6 +525,15 @@ def normalize(numbers):
525525 total = float (sum (numbers ))
526526 return [n / total for n in numbers ]
527527
528+ def clip (x , lowest , highest ):
529+ """Return x clipped to the range [lowest..highest].
530+ >>> clip(-1, 0, 1)
531+ 0
532+ >>> clip(10, 0, 1)
533+ 1
534+ """
535+ return max (lowest , min (x , highest ))
536+
528537#______________________________________________________________________________
529538## OK, the following are not as widely useful utilities as some of the other
530539## functions here, but they do show up wherever we have 2D grids: Wumpus and
@@ -549,14 +558,15 @@ def distance2((ax, ay), (bx, by)):
549558 "The square of the distance between two (x, y) points."
550559 return (ax - bx )** 2 + (ay - by )** 2
551560
552- def clip (vector , lowest , highest ):
561+ def vector_clip (vector , lowest , highest ):
553562 """Return vector, except if any element is less than the corresponding
554563 value of lowest or more than the corresponding value of highest, clip to
555564 those values.
556- >>> clip ((-1, 10), (0, 0), (9, 9))
565+ >>> vector_clip ((-1, 10), (0, 0), (9, 9))
557566 (0, 9)
558567 """
559- return type (vector )(map (min , map (max , vector , lowest ), highest ))
568+ return type (vector )(map (clip , vector , lowest , highest ))
569+
560570#______________________________________________________________________________
561571# Misc Functions
562572
0 commit comments