@@ -20,7 +20,8 @@ open import Data.Nat.Base as ℕ using (ℕ; zero; suc; _+_; _*_ ; _≤_ ; s≤s
20
20
open import Data.Product.Base as Prod using (_×_; _,_; map₁; map₂′)
21
21
open import Data.Sum.Base as Sum using (_⊎_; inj₁; inj₂)
22
22
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)
24
25
open import Level using (Level)
25
26
open import Relation.Nullary.Decidable.Core using (does; ¬?)
26
27
open import Relation.Unary using (Pred; Decidable)
@@ -395,6 +396,24 @@ deduplicateᵇ : (A → A → Bool) → List A → List A
395
396
deduplicateᵇ r [] = []
396
397
deduplicateᵇ r (x ∷ xs) = x ∷ filterᵇ (not ∘ r x) (deduplicateᵇ r xs)
397
398
399
+ findᵇ : (A → Bool) → List A → Maybe A
400
+ findᵇ p [] = nothing
401
+ findᵇ p (x ∷ xs) = if p x then just x else findᵇ p xs
402
+
403
+ findIndexᵇ : (A → Bool) → (x : List A) → Maybe $ Fin (length x)
404
+ findIndexᵇ p [] = nothing
405
+ findIndexᵇ p (x ∷ xs) = if p x
406
+ then just zero
407
+ else mapMaybe suc (findIndexᵇ p xs)
408
+
409
+ findIndicesᵇ : (A → Bool) → List A → List ℕ
410
+ findIndicesᵇ {A = A} p = h 0 where
411
+ h : ℕ → List A → List ℕ
412
+ h n [] = []
413
+ h n (x ∷ xs) = if p x
414
+ then n ∷ h (suc n) xs
415
+ else h (suc n) xs
416
+
398
417
-- Equivalent functions that use a decidable predicate instead of a
399
418
-- boolean function.
400
419
@@ -436,6 +455,15 @@ derun R? = derunᵇ (does ∘₂ R?)
436
455
deduplicate : ∀ {R : Rel A p} → B.Decidable R → List A → List A
437
456
deduplicate R? = deduplicateᵇ (does ∘₂ R?)
438
457
458
+ find : ∀ {P : Pred A p} → Decidable P → List A → Maybe A
459
+ find P? = findᵇ (does ∘ P?)
460
+
461
+ findIndex : ∀ {P : Pred A p} → Decidable P → (x : List A) → Maybe $ Fin (length x)
462
+ findIndex P? = findIndexᵇ (does ∘ P?)
463
+
464
+ findIndices : ∀ {P : Pred A p} → Decidable P → List A → List ℕ
465
+ findIndices P? = findIndicesᵇ (does ∘ P?)
466
+
439
467
------------------------------------------------------------------------
440
468
-- Actions on single elements
441
469
0 commit comments