Skip to content

Commit 45ec837

Browse files
Saransh-cppjamesmckinna
authored andcommitted
Add find, findIndex, and findIndices for Data.List (#2042)
* `find*` functions for `Data.List` both decidable predicate and boolean function variants * Back to `Maybe.map` * New type for findIndices * Add comments * Respect style guide --------- Co-authored-by: jamesmckinna <[email protected]>
1 parent b3a5a68 commit 45ec837

File tree

6 files changed

+42
-5
lines changed

6 files changed

+42
-5
lines changed

CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -2173,6 +2173,13 @@ Other minor changes
21732173
wordsByᵇ : (A → Bool) → List A → List (List A)
21742174
derunᵇ : (A → A → Bool) → List A → List A
21752175
deduplicateᵇ : (A → A → Bool) → List A → List A
2176+
2177+
findᵇ : (A → Bool) → List A -> Maybe A
2178+
findIndexᵇ : (A → Bool) → (xs : List A) → Maybe $ Fin (length xs)
2179+
findIndicesᵇ : (A → Bool) → (xs : List A) → List $ Fin (length xs)
2180+
find : Decidable P → List A → Maybe A
2181+
findIndex : Decidable P → (xs : List A) → Maybe $ Fin (length xs)
2182+
findIndices : Decidable P → (xs : List A) → List $ Fin (length xs)
21762183
```
21772184

21782185
* Added new functions and definitions to `Data.List.Base`:

src/Data/List/Base.agda

+31-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ open import Data.Nat.Base as ℕ using (ℕ; zero; suc; _+_; _*_ ; _≤_ ; s≤s
2020
open import Data.Product.Base as Prod using (_×_; _,_; map₁; map₂′)
2121
open import Data.Sum.Base as Sum using (_⊎_; inj₁; inj₂)
2222
open import Data.These.Base as These using (These; this; that; these)
23-
open import Function.Base using (id; _∘_ ; _∘′_; _∘₂_; const; flip)
23+
open import Function.Base
24+
using (id; _∘_ ; _∘′_; _∘₂_; _$_; const; flip)
2425
open import Level using (Level)
2526
open import Relation.Nullary.Decidable.Core using (does; ¬?)
2627
open import Relation.Unary using (Pred; Decidable)
@@ -395,6 +396,26 @@ deduplicateᵇ : (A → A → Bool) → List A → List A
395396
deduplicateᵇ r [] = []
396397
deduplicateᵇ r (x ∷ xs) = x ∷ filterᵇ (not ∘ r x) (deduplicateᵇ r xs)
397398

399+
-- Finds the first element satisfying the boolean predicate
400+
findᵇ : (A Bool) List A Maybe A
401+
findᵇ p [] = nothing
402+
findᵇ p (x ∷ xs) = if p x then just x else findᵇ p xs
403+
404+
-- Finds the index of the first element satisfying the boolean predicate
405+
findIndexᵇ : (A Bool) (xs : List A) Maybe $ Fin (length xs)
406+
findIndexᵇ p [] = nothing
407+
findIndexᵇ p (x ∷ xs) = if p x
408+
then just zero
409+
else Maybe.map suc (findIndexᵇ p xs)
410+
411+
-- Finds indices of all the elements satisfying the boolean predicate
412+
findIndicesᵇ : (A Bool) (xs : List A) List $ Fin (length xs)
413+
findIndicesᵇ p [] = []
414+
findIndicesᵇ p (x ∷ xs) = if p x
415+
then zero ∷ indices
416+
else indices
417+
where indices = map suc (findIndicesᵇ p xs)
418+
398419
-- Equivalent functions that use a decidable predicate instead of a
399420
-- boolean function.
400421

@@ -436,6 +457,15 @@ derun R? = derunᵇ (does ∘₂ R?)
436457
deduplicate : {R : Rel A p} B.Decidable R List A List A
437458
deduplicate R? = deduplicateᵇ (does ∘₂ R?)
438459

460+
find : {P : Pred A p} Decidable P List A Maybe A
461+
find P? = findᵇ (does ∘ P?)
462+
463+
findIndex : {P : Pred A p} Decidable P (xs : List A) Maybe $ Fin (length xs)
464+
findIndex P? = findIndexᵇ (does ∘ P?)
465+
466+
findIndices : {P : Pred A p} Decidable P (xs : List A) List $ Fin (length xs)
467+
findIndices P? = findIndicesᵇ (does ∘ P?)
468+
439469
------------------------------------------------------------------------
440470
-- Actions on single elements
441471

src/Data/List/Membership/Setoid/Properties.agda

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ open import Algebra using (Op₂; Selective)
1212
open import Data.Bool.Base using (true; false)
1313
open import Data.Fin.Base using (Fin; zero; suc)
1414
open import Data.Fin.Properties using (suc-injective)
15-
open import Data.List.Base
15+
open import Data.List.Base hiding (find)
1616
open import Data.List.Relation.Unary.Any as Any using (Any; here; there)
1717
open import Data.List.Relation.Unary.All as All using (All)
1818
import Data.List.Relation.Unary.Any.Properties as Any

src/Data/List/Relation/Binary/Subset/Setoid/Properties.agda

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
module Data.List.Relation.Binary.Subset.Setoid.Properties where
1010

1111
open import Data.Bool.Base using (Bool; true; false)
12-
open import Data.List.Base hiding (_∷ʳ_)
12+
open import Data.List.Base hiding (_∷ʳ_; find)
1313
open import Data.List.Relation.Unary.Any as Any using (Any; here; there)
1414
open import Data.List.Relation.Unary.All as All using (All)
1515
import Data.List.Membership.Setoid as Membership

src/Data/List/Relation/Unary/Any/Properties.agda

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ open import Data.Bool.Base using (Bool; false; true; T)
1212
open import Data.Bool.Properties using (T-∨; T-≡)
1313
open import Data.Empty using (⊥)
1414
open import Data.Fin.Base using (Fin; zero; suc)
15-
open import Data.List.Base as List
15+
open import Data.List.Base as List hiding (find)
1616
open import Data.List.Properties using (ʳ++-defn)
1717
open import Data.List.Effectful as Listₑ using (monad)
1818
open import Data.List.Relation.Unary.Any as Any using (Any; here; there)

src/Function/Base.agda

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ flip f = λ y x → f x y
7272

7373
-- Application - note that _$_ is right associative, as in Haskell.
7474
-- If you want a left associative infix application operator, use
75-
-- Category.Functor._<$>_ from Category.Monad.Identity.IdentityMonad.
75+
-- RawFunctor._<$>_ from Effect.Functor.
7676

7777
_$_ : {A : Set a} {B : A Set b}
7878
((x : A) B x) ((x : A) B x)

0 commit comments

Comments
 (0)