Changeset 17865

Show
Ignore:
Timestamp:
09/16/07 01:53:07 (14 months ago)
Author:
ryporter
Message:

adding support for first() to pugs

Files:
3 modified

Legend:

Unmodified
Added
Removed
  • src/Pugs/Prim.hs

    r17701 r17865  
    10401040op2 "pick" = op2Pick 
    10411041op2 "grep" = op2Grep 
     1042op2 "first" = op2First 
    10421043op2 "map"  = op2Map 
    10431044op2 "join" = op2Join 
     
    19021903\\n   List      pre     map     safe   (Code, List)\ 
    19031904\\n   List      pre     grep    safe   (Code, List)\ 
     1905\\n   Scalar    pre     first   safe   (Code, List)\ 
    19041906\\n   List      pre     sort    safe   (Code, List)\ 
    19051907\\n   List      pre     reduce  safe   (Code, List)\ 
     
    19081910\\n   List      pre     map     safe   (Array: Code)\ 
    19091911\\n   List      pre     grep    safe   (Array: Code)\ 
     1912\\n   Scalar    pre     first   safe   (Array: Code)\ 
    19101913\\n   List      pre     sort    safe   (Array: Code)\ 
    19111914\\n   List      pre     reduce  safe   (Array: Code)\ 
  • src/Pugs/Prim/List.hs

    r16573 r17865  
    55    op1Min, op1Max, op1Uniq, 
    66    op2Pick, 
    7     op2ReduceL, op2Reduce, op2Grep, op2Map, op2Join, 
     7    op2ReduceL, op2Reduce, op2Grep, op2First, op2Map, op2Join, 
    88    sortByM, 
    99    op1HyperPrefix, op1HyperPostfix, op2Hyper, 
     
    331331    return $ VList vals 
    332332 
     333op2First :: Val -> Val -> Eval Val 
     334op2First sub@(VCode _) list = op2First list sub 
     335op2First list sub = do 
     336  (VList vals) <- (op2Grep list sub) 
     337  if (length vals) > 0  
     338    then return $ (vals !! 0) 
     339    else fail $ "Cannot call first() with a filter that removes all elements from the input list" 
     340 
    333341op2Map :: Val -> Val -> Eval Val 
    334342op2Map sub@(VCode _) list = op2Map list sub 
  • t/builtins/lists/first.t

    r16466 r17865  
    1010=cut 
    1111 
    12 plan 6; 
     12plan 7; 
    1313 
    1414my @list = (1 .. 10); 
     
    1616{ 
    1717    my $result = first { ($_ % 2) }, @list; 
    18     ok ($result ~~ Item, "first() returns an Item"); 
    19     is ($result, 1, "returned value by first() is correct"); 
     18    ok($result ~~ Item, "first() returns an Item"); 
     19    is($result, 1, "returned value by first() is correct"); 
    2020} 
    2121 
    2222{ 
    2323    my $result = @list.first( { ($_ == 4)}); 
    24     ok ($result ~~ Item, "method form of first returns an item"); 
    25     is ($result, 4, "method form of first returns the expected item"); 
     24    ok($result ~~ Item, "method form of first returns an item"); 
     25    is($result, 4, "method form of first returns the expected item"); 
    2626} 
    2727 
    2828{ 
    29     my $result = @list.grep():{ ($_ == 4) }; 
    30     ok($result ~~ Item, "first():{block} returns an Item"); 
     29    my $result = @list.first():{ ($_ == 4) }; 
     30    ok($result ~~ Item, "first():<block> returns an Item"); 
    3131    is($result, 4, "first() returned the expected value"); 
    3232} 
    3333 
     34{ 
     35    dies_ok { @list.first( { ($_ == 11) } ) }, "first should fail if there is no first element"; 
     36}