Switch to a tail recursive version of uniqueBy
This commit is contained in:
parent
f6ce669fb4
commit
b23c80f463
|
@ -268,15 +268,17 @@ groupBy f xs =
|
||||||
in
|
in
|
||||||
List.foldl (\v acc -> Dict.update (f v) (update v) acc) Dict.empty xs
|
List.foldl (\v acc -> Dict.update (f v) (update v) acc) Dict.empty xs
|
||||||
|
|
||||||
uniqueByRecursive : (a -> comparable) -> List a -> Set comparable -> List a
|
uniqueByTailRecursive : (a -> comparable) -> List a -> Set comparable -> List a -> List a
|
||||||
uniqueByRecursive f l s = case l of
|
uniqueByTailRecursive f l s acc =
|
||||||
x::tail -> if Set.member (f x) s
|
case l of
|
||||||
then uniqueByRecursive f tail s
|
x::tail ->
|
||||||
else x::uniqueByRecursive f tail (Set.insert (f x) s)
|
if Set.member (f x) s
|
||||||
[] -> []
|
then uniqueByTailRecursive f tail s acc
|
||||||
|
else uniqueByTailRecursive f tail s (x::acc)
|
||||||
|
[] -> acc
|
||||||
|
|
||||||
uniqueBy : (a -> comparable) -> List a -> List a
|
uniqueBy : (a -> comparable) -> List a -> List a
|
||||||
uniqueBy f l = uniqueByRecursive f l Set.empty
|
uniqueBy f l = uniqueByTailRecursive f l Set.empty []
|
||||||
|
|
||||||
findFirst : (a -> Bool) -> List a -> Maybe a
|
findFirst : (a -> Bool) -> List a -> Maybe a
|
||||||
findFirst cond l = case l of
|
findFirst cond l = case l of
|
||||||
|
|
Loading…
Reference in New Issue
Block a user