Skip to content

Commit 11c0295

Browse files
committed
Add lookupAll utility
1 parent 8bc7cab commit 11c0295

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/Libraries/Base1/List.bs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,19 @@ lookup k xs = case (find (\x -> x.fst == k) xs) of
119119
Nothing -> Nothing
120120
Just (_,v) -> Just v
121121

122+
--@ Lookup all values for a key in an association list.
123+
--@ \index{lookup@\te{lookup} (\te{List} function)}
124+
--@ \begin{libverbatim}
125+
--@ function List#(b) lookup (a k, List#(Tuple2#(a,b)) xs)
126+
--@ provisos(Eq#(a));
127+
--@ \end{libverbatim}
128+
lookupAll :: (Eq a) => a -> List (a, b) -> List b
129+
lookupAll k (Cons x xs) =
130+
if x.fst == k
131+
then x.snd :> lookupAll k xs
132+
else lookupAll k xs
133+
lookupAll _ Nil = Nil
134+
122135
--@ Append two lists, return appended list.
123136
--@ \index{append@\te{append} (\te{List} function)}
124137
--@ \begin{libverbatim}

0 commit comments

Comments
 (0)