Dropping or taking from lists on the right #
THIS FILE IS SYNCHRONIZED WITH MATHLIB4. Any changes to this file require a corresponding PR to mathlib4.
Taking or removing element from the tail end of a list
Main defintions #
rdrop n
: dropn : ℕ
elements from the tailrtake n
: taken : ℕ
elements from the taildrop_while p
: remove all the elements that satisfy a decidablep : α → Prop
from the tail of a list until hitting the first non-satisfying elementtake_while p
: take all the elements that satisfy a decidablep : α → Prop
from the tail of a list until hitting the first non-satisfying element
Implementation detail #
The two predicate-based methods operate by performing the regular "from-left" operation on
list.reverse
, followed by another list.reverse
, so they are not the most performant.
The other two rely on list.length l
so they still traverse the list twice. One could construct
another function that takes a L : ℕ
and use L - n
. Under a proof condition that
L = l.length
, the function would do the right thing.
Drop elements from the tail end of a list that satisfy p : α → Prop
.
Implemented naively via list.reverse
Equations
- list.rdrop_while p l = (list.drop_while p l.reverse).reverse
Take elements from the tail end of a list that satisfy p : α → Prop
.
Implemented naively via list.reverse
Equations
- list.rtake_while p l = (list.take_while p l.reverse).reverse