Extract Local Maxima Given an array of positive integers, output an array of all the elements that are greater than or equal to the adjacent ones. Most elements will have two adjacent elements; the first and last element are special cases, as they only have one adjacent element.

You may assume that the array contains at least two elements.

Test cases:

Input               | Output
[4,2,6,12,4,5,4,3]  | [4,12,5]
[1,2]               | [2]
[1,2,3,2,1]         | [3]
[3,2,1,2,3]         | [3,3]
[4,4]               | [4,4]
[2,4,4,4,1]         | [4,4,4]
[2,3,3,4]           | [3,4]
[4,3,3,4]           | [4,4]


This is code-golf, shortest code wins!
Python, 54 bytes



f=lambda l,*p:l and l[:p&lt;=l[:1]&gt;=l[1:2]]+f(l[1:],l[0])


Try it online!

I/O is with tuples rather than lists.



Python, 57 bytes



f=lambda l,p=0:l and l[:[p]&lt;=l[:1]&gt;=l[1:2]]+f(l[1:],l[0])


Try it online!

Alt 57:

f=lambda l,p=0:l and l[l&lt;[max(p,*l[:2])]:1]+f(l[1:],l[0])