Changeset 17870
- Timestamp:
- 09/16/07 15:05:07 (14 months ago)
- Files:
-
- 2 modified
-
src/Pugs/Prim.hs (modified) (4 diffs)
-
t/operators/range.t (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/Pugs/Prim.hs
r17865 r17870 1456 1456 op1Range (VInt int) 1457 1457 1458 {- In the four op2Range* functions below, rationals 1459 have to be handled separately because Haskell ranges 1460 are different from Perl 6 ranges. For example, 1461 in Haskell, [1.1 .. 2] will return [1.1,2.1]. So, we 1462 run the elements through a filter to ensure that the 1463 upper bound is satisfied 1464 -} 1458 1465 op2Range :: Val -> Val -> Eval Val 1459 1466 op2Range (VStr s) y = do … … 1468 1475 op2Range (VRat n) y = do 1469 1476 y' <- fromVal y 1470 return . VList $ map VRat [n .. y']1477 return . VList $ map VRat (filter (<= y') [n .. y']) 1471 1478 op2Range x (VRat n) = do 1472 1479 x' <- fromVal x 1473 return . VList $ map VRat [x' .. n]1480 return . VList $ map VRat (filter (<= n) [x' .. n]) 1474 1481 op2Range x y = do 1475 1482 x' <- fromVal x … … 1477 1484 return . VList $ map VInt [x' .. y'] 1478 1485 1486 -- because the right-exclusivity of a range can leave it 1487 -- with no remaining elements, we need to check before 1488 -- removing an element when enforcing left-exclusivity 1489 removeRangeFirst :: [Val] -> [Val] 1490 removeRangeFirst vals = if null vals then vals else init vals 1491 1479 1492 op2RangeExclRight :: Val -> Val -> Eval Val 1493 op2RangeExclRight (VRat n) y = do 1494 y' <- fromVal y 1495 return . VList $ map VRat (filter (< y') [n .. y']) 1496 op2RangeExclRight x (VRat n) = do 1497 x' <- fromVal x 1498 return . VList $ map VRat (filter (< n) [x' .. n]) 1480 1499 op2RangeExclRight x y = do 1481 1500 VList vals <- op2Range x y 1482 return . VList $ init vals1501 return . VList $ removeRangeFirst vals 1483 1502 1484 1503 op2RangeExclLeft :: Val -> Val -> Eval Val 1504 op2RangeExclLeft (VRat n) y = do 1505 y' <- fromVal y 1506 return . VList $ map VRat (filter (\v -> n < v && v <= y') [n .. y']) 1507 op2RangeExclLeft x (VRat n) = do 1508 x' <- fromVal x 1509 return . VList $ map VRat (filter (\v -> x' < v && v <= n) [x' .. n]) 1485 1510 op2RangeExclLeft x y = do 1486 1511 VList vals <- op2Range x y … … 1488 1513 1489 1514 op2RangeExclBoth :: Val -> Val -> Eval Val 1515 op2RangeExclBoth (VRat n) y = do 1516 y' <- fromVal y 1517 return . VList $ map VRat (filter (\v -> n < v && v < y') [n .. y']) 1518 op2RangeExclBoth x (VRat n) = do 1519 x' <- fromVal x 1520 return . VList $ map VRat (filter (\v -> x' < v && v < n) [x' .. n]) 1490 1521 op2RangeExclBoth x y = do 1491 1522 VList vals <- op2Range x y 1492 return . VList $ init (tail vals)1523 return . VList $ removeRangeFirst (tail vals) 1493 1524 1494 1525 op2ChainedList :: Val -> Val -> Val -
t/operators/range.t
r15818 r17870 3 3 use Test; 4 4 5 plan 47;5 plan 50; 6 6 7 7 # 3..2 must *not* produce "3 2". Use reverse to get a reversed range. -lwall … … 34 34 is [1^..^2], [], "double-exclusive range (^..^) can produce null range"; 35 35 36 # tests of (x ^..^ x) here and below ensure that our implementation 37 # of double-exclusive range does not blindly remove an element 38 # from the head and tail of a list 39 is [1^..^1], [], "double-exclusive range (x ^..^ x) where x is an int"; 40 36 41 is ["a"^.."z"], ["b".."z"], "bottom-exclusive string range (^..) works"; 37 42 is ["a"..^"z"], ["a".."y"], "top-exclusive string range (..^) works"; 38 43 is ["a"^..^"z"], ["b".."y"], "double-exclusive string range (^..^) works"; 39 44 is ['a'^..^'b'], [], "double-exclusive string range (^..^) can produce null range"; 45 is ['a' ^..^ 'a'], [], "double-exclusive range (x ^..^ x) where x is a char"; 40 46 41 47 is 1.5 ~~ 1^..^2, Bool::True, "lazy evaluation of the range operator", :todo<bug>; … … 70 76 is ~(1.1 ^..^ 4.9), "2.1 3.1 4.1", "both exclusive float range"; 71 77 is ~(1.9 ^..^ 4.9), "2.9 3.9" , "both exclusive float range"; 72 78 is [1.1 ^..^ 1.1], [], "double-exclusive range (x ^..^ x) where x is a float"; 73 79 74 80 # Test that the operands are forced to scalar context
