Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Data.List.Base.mapMaybe (#2359; deprecate use of with #2123) #2361

Merged
merged 11 commits into from
Apr 19, 2024
Prev Previous commit
Next Next commit
use foldr on @JacquesCarette 's solution
jamesmckinna committed Apr 13, 2024
commit 738d893bdec1bfd2bb0b4be82e24e1f824b3bf9a
11 changes: 8 additions & 3 deletions src/Data/List/Base.agda
Original file line number Diff line number Diff line change
@@ -50,9 +50,8 @@ map f [] = []
map f (x ∷ xs) = f x ∷ map f xs

catMaybes′ : List (Maybe A) List A
catMaybes′ [] = []
catMaybes′ (nothing ∷ xs) = catMaybes′ xs
catMaybes′ (just x ∷ xs) = x ∷ catMaybes′ xs
catMaybes′ [] = []
catMaybes′ (x ∷ xs) = maybe′ _∷_ id x $ catMaybes′ xs

mapMaybe′ : (A Maybe B) List A List B
mapMaybe′ p = catMaybes′ ∘ map p
@@ -182,6 +181,12 @@ product = foldr _*_ 1
length : List A
length = foldr (const suc) 0

catMaybes″ : List (Maybe A) List A
catMaybes″ = foldr (maybe′ _∷_ id) []

mapMaybe″ : (A Maybe B) List A List B
mapMaybe″ p = catMaybes″ ∘ map p

------------------------------------------------------------------------
-- Operations for constructing lists

2 changes: 1 addition & 1 deletion src/Data/List/Properties.agda
Original file line number Diff line number Diff line change
@@ -132,7 +132,7 @@ catMaybesTest [] = refl
catMaybesTest (just x ∷ xs) = cong (x ∷_) (catMaybesTest xs)
catMaybesTest (nothing ∷ xs) = catMaybesTest xs

mapMaybeTest : (p : A Maybe B) mapMaybe p ≗ mapMaybe p
mapMaybeTest : (p : A Maybe B) mapMaybe p ≗ mapMaybe p
mapMaybeTest p [] = refl
mapMaybeTest p (x ∷ xs) with ih mapMaybeTest p xs | p x
... | nothing = ih