feat: add some useful helper functions I didn't actually use in the end
This commit is contained in:
parent
3e7bc07f19
commit
e999fa678d
2 changed files with 18 additions and 0 deletions
|
|
@ -201,6 +201,16 @@ eraseDupsAux as []
|
|||
@[inline] def span (p : α → Bool) (as : List α) : List α × List α :=
|
||||
spanAux p as []
|
||||
|
||||
@[specialize] def groupByAux (eq : α → α → Bool) : List α → List (List α) → List (List α)
|
||||
| a::as, (ag::g)::gs => match eq a ag with
|
||||
| true => groupByAux as ((a::ag::g)::gs)
|
||||
| false => groupByAux as ([a]::(ag::g).reverse::gs)
|
||||
| _, gs => gs.reverse
|
||||
|
||||
@[specialize] def groupBy (p : α → α → Bool) : List α → List (List α)
|
||||
| [] => []
|
||||
| a::as => groupByAux p as [[a]]
|
||||
|
||||
def lookup [HasBeq α] : α → List (α × β) → Option β
|
||||
| _, [] => none
|
||||
| a, (k,b)::es => match a == k with
|
||||
|
|
|
|||
|
|
@ -51,6 +51,14 @@ instance : Monad Option :=
|
|||
| some a => if p a then some a else none
|
||||
| none => none
|
||||
|
||||
@[inline] protected def all {α} (p : α → Bool) : Option α → Bool
|
||||
| some a => p a
|
||||
| none => true
|
||||
|
||||
@[inline] protected def any {α} (p : α → Bool) : Option α → Bool
|
||||
| some a => p a
|
||||
| none => false
|
||||
|
||||
@[macroInline] protected def orelse {α : Type u} : Option α → Option α → Option α
|
||||
| some a, _ => some a
|
||||
| none, b => b
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue