Changeset 3501
- Timestamp:
- 05/20/05 13:59:49 (4 years ago)
- svk:copy_cache_prev:
- 5078
- Files:
-
- 3 modified
-
src/Pugs/Prim.hs (modified) (2 diffs)
-
src/Pugs/Prim/List.hs (modified) (2 diffs)
-
t/builtins/lists/uniq.t (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/Pugs/Prim.hs
r3495 r3501 462 462 op1 "min" = op1Min 463 463 op1 "max" = op1Max 464 op1 "uniq" = op1Uniq 464 465 op1 "chr" = op1Cast (VStr . (:[]) . chr) 465 466 op1 "ord" = op1Cast $ \str -> if null str then undef else (castV . ord . head) str … … 1084 1085 \\n Scalar pre min (List)\ 1085 1086 \\n Scalar pre max (List)\ 1087 \\n List pre uniq (List)\ 1086 1088 \\n Str pre join (Array: Str)\ 1087 1089 \\n Str pre join (Str, List)\ -
src/Pugs/Prim/List.hs
r3495 r3501 1 1 module Pugs.Prim.List ( 2 2 op0Zip, op1Pick, op1Sum, 3 op1Min, op1Max, 3 op1Min, op1Max, op1Uniq, 4 4 op2FoldL, op2Fold, op2Grep, op2Map, op2Join, 5 5 sortByM, … … 102 102 return $ if min_or_max (cmp > (0::VInt)) then a else b 103 103 104 op1Uniq v = do 105 -- We want to have a real Haskell list 106 args <- fromVal v 107 -- Extract our comparator sub, or Nothing if none was specified 108 (valList, cmp) <- case args of 109 (v:vs) -> do 110 ifValTypeIsa v "Code" 111 (return (vs, Just v)) 112 (ifValTypeIsa (last args) "Code" 113 (return (init args, Just $ last args)) 114 (return (args, Nothing))) 115 _ -> return (args, Nothing) 116 -- After this parameter unpacking, we begin doing the real work. 117 op1Uniq' cmp valList 118 where 119 op1Uniq' :: (Maybe Val) -> [Val] -> Eval Val 120 -- If the user didn't specify an own comparasion sub, we can simply use 121 -- Haskell's nub. 122 op1Uniq' Nothing valList = return . VList $ nub valList 123 -- Else, we have to write our own nubByM and use that. 124 op1Uniq' (Just subVal) valList = do 125 sub <- fromVal subVal 126 evl <- asks envEval 127 -- Here we execute the user's sub 128 result <- nubByM (\a b -> do 129 rv <- local (\e -> e{ envContext = cxtItem "Bool" }) $ do 130 evl (App (Val sub) [Val a, Val b] []) 131 -- The sub returns either true or false. 132 bool <- fromVal rv 133 return . VBool $ bool) valList 134 return . VList $ result 135 -- This is the same as nubBy, only lifted into the Eval monad 136 nubByM :: (Val -> Val -> Eval Val) -> [Val] -> Eval [Val] 137 nubByM eq l = nubByM' l [] 138 where 139 nubByM' [] _ = return [] 140 nubByM' (y:ys) xs = do 141 -- elemByM returns a Val, but we need a VBool, so we have to use fromVal. 142 cond <- fromVal =<< elemByM eq y xs 143 if cond then nubByM' ys xs else do 144 result <- nubByM' ys (y:xs) 145 return (y:result) 146 elemByM :: (Val -> Val -> Eval Val) -> Val -> [Val] -> Eval Val 147 elemByM _ _ [] = return . VBool $ False 148 elemByM eq y (x:xs) = do 149 cond <- fromVal =<< eq x y 150 -- Same here (we need a VBool, not a Var). 151 if cond then return . VBool $ cond else elemByM eq y xs 152 104 153 op2FoldL :: Val -> Val -> Eval Val 105 154 op2FoldL sub@(VCode _) list = op2FoldL list sub -
t/builtins/lists/uniq.t
r3500 r3501 20 20 my @array = <a b b c d e b b b b f b>; 21 21 is ~@array, "a b b c d e b b b b f b", "basic sanity"; 22 is eval('~@array.uniq'), "a b c d e f", "method form of uniq works" , :todo;23 is eval('~uniq @array'), "a b c d e f", "subroutine form of uniq works" , :todo;24 ok eval('@array.=uniq'), "inplace form of uniq works (1)" , :todo;25 is ~@array, "a b c d e f", "inplace form of uniq works (2)" , :todo;22 is eval('~@array.uniq'), "a b c d e f", "method form of uniq works"; 23 is eval('~uniq @array'), "a b c d e f", "subroutine form of uniq works"; 24 ok eval('@array.=uniq'), "inplace form of uniq works (1)"; 25 is ~@array, "a b c d e f", "inplace form of uniq works (2)"; 26 26 } 27 27 … … 30 30 my @array = <a b A c b d>; 31 31 # Semantics w/o junctions 32 is eval('~@array.uniq:{ lc $^a eq lc $^b }'), "a b c d", "method form of uniq with own comparator works" , :todo;33 is eval('~uniq { lc $^a eq lc $^b } @array'), "a b c d", "subroutine form of uniq with own comparator works" , :todo;32 is eval('~@array.uniq:{ lc $^a eq lc $^b }'), "a b c d", "method form of uniq with own comparator works"; 33 is eval('~uniq { lc $^a eq lc $^b } @array'), "a b c d", "subroutine form of uniq with own comparator works"; 34 34 35 35 # Semantics w/ junctions
