Skip to content

Commit

Permalink
add reverseFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
Olivia Brode-Roger committed Jul 11, 2023
1 parent 7542f94 commit d36c55c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
25 changes: 23 additions & 2 deletions src/List/Extra.elm
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module List.Extra exposing
( last, init, getAt, uncons, unconsLast, maximumBy, maximumWith, minimumBy, minimumWith, andMap, andThen, reverseMap, takeWhile, dropWhile, unique, uniqueBy, allDifferent, allDifferentBy, setIf, setAt, remove, updateIf, updateAt, updateIfIndex, removeAt, removeIfIndex, filterNot, swapAt, stableSortWith
( last, init, getAt, uncons, unconsLast, maximumBy, maximumWith, minimumBy, minimumWith, andMap, andThen, reverseMap, reverseFilter, takeWhile, dropWhile, unique, uniqueBy, allDifferent, allDifferentBy, setIf, setAt, remove, updateIf, updateAt, updateIfIndex, removeAt, removeIfIndex, filterNot, swapAt, stableSortWith
, intercalate, transpose, subsequences, permutations, interweave, cartesianProduct, uniquePairs
, foldl1, foldr1, indexedFoldl, indexedFoldr, Step(..), stoppableFoldl
, scanl, scanl1, scanr, scanr1, mapAccuml, mapAccumr, unfoldr, iterate, initialize, cycle, reverseRange
Expand All @@ -17,7 +17,7 @@ module List.Extra exposing
# Basics
@docs last, init, getAt, uncons, unconsLast, maximumBy, maximumWith, minimumBy, minimumWith, andMap, andThen, reverseMap, takeWhile, dropWhile, unique, uniqueBy, allDifferent, allDifferentBy, setIf, setAt, remove, updateIf, updateAt, updateIfIndex, removeAt, removeIfIndex, filterNot, swapAt, stableSortWith
@docs last, init, getAt, uncons, unconsLast, maximumBy, maximumWith, minimumBy, minimumWith, andMap, andThen, reverseMap, reverseFilter, takeWhile, dropWhile, unique, uniqueBy, allDifferent, allDifferentBy, setIf, setAt, remove, updateIf, updateAt, updateIfIndex, removeAt, removeIfIndex, filterNot, swapAt, stableSortWith
# List transformations
Expand Down Expand Up @@ -569,6 +569,27 @@ reverseMap f xs =
foldl (\x acc -> f x :: acc) [] xs


{-| `reverseMap f xs` gives the same result as `List.reverse (List.map f xs)`,
but is tail-recursive and slightly more efficient.
reverseFilter (\x -> x > 5) [ 1, 4, 9, 16]
--> [ 16, 9 ]
-}
reverseFilter : (a -> Bool) -> List a -> List a
reverseFilter isGood xs =
foldl
(\x acc ->
if isGood x then
x :: acc

else
acc
)
[]
xs


{-| Negation of `member`.
notMember 1 [ 1, 2, 3 ]
Expand Down
5 changes: 5 additions & 0 deletions tests/Tests.elm
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ all =
\() ->
Expect.equal (reverseMap sqrt [ 1, 4, 9 ]) [ 3, 2, 1 ]
]
, describe "reverseFilter" <|
[ test "filters and reverses" <|
\() ->
Expect.equal (reverseFilter (\x -> x > 5) [ 1, 4, 9, 16 ]) [ 16, 9 ]
]
, describe "notMember" <|
[ test "disconfirms member" <|
\() ->
Expand Down

0 comments on commit d36c55c

Please sign in to comment.