What are some examples of Idempotent functions?
A projection is a linear map satisfying $P^2=P$. These are always idempotent, by definition.
- If $f:\mathbb R\to\mathbb R$ is continuous and idempotent then $I=f(\mathbb R)$ is a closed interval and $f(x)=x$ for all $x\in I$.
- If $f$ is also differentiable and nonconstant, then $I=\mathbb R$, i.e., $f(x)=x$ for all $x\in\mathbb R$.
Proof of 1.: If $f$ is continuous and idempotent, then $I=f(\mathbb R)$ is an interval by continuity alone along with the IVT. If $A=\{x\in \mathbb R: f(x)=x\}$, then $A$ is a closed set by continuity, $A\subseteq I$ because each $x\in A$ equals $f(x)\in I$, and $I\subseteq A$ by idempotency. Thus $I=A$, confirming that $I$ is a closed interval on which $f$ is the identity function.
Proof of 2.: Suppose that $f$ is continuous and idempotent, but not constant and not the identity function. Then $I$ is not $\mathbb R$, not a singleton, so by 1. $I$ is a nontrivial closed interval that is either bounded above or below (or both). Suppose $I$ is bounded above, and let $b=\sup(I)=\max(I)$, the last equality holding by closedness of $I$. Because $I$ is a nontrivial interval, $I$ contains $(a,b]$ for some $a<b$. It follows that $f$ is not differentiable at $x=b$, because $\lim\limits_{h\to 0-}\dfrac{f(b+h)-f(b)}{h}=1$, but for all $h>0$, $\dfrac{f(b+h)-f(b)}{h}\leq 0$. If $f$ is bounded below a similar argument applies to show that $f$ is not differentiable at $\inf(I)=\min(I)$. By contraposition, this confirms that if $f$ is idempotent, differentiable and nonconstant, then $I=\mathbb R$, i.e., $f(x)=x$ for all $x\in\mathbb R$.
In the case where $f$ is continuous and not constant or the identity function, the graph of $f$ consists of a closed line segment or ray on the line $y=x$, having the form $\{(x,x):x\in f(\mathbb R)\}$, then extends continuously in a way that is arbitrary as long as the $y$ values stay in $I=f(\mathbb R)=f(I)$. This is a special case of Jair Taylor's more general description, where $S$ must be a interval and the pieced together map must be continuous.
For a given bounded interval $[a,b]$, $a<b$, a formula for a continuous idempotent function $f$ having $[a,b]=f(\mathbb R)$ is $$f(x)=\frac{b-a}{\pi}\arcsin\left(\sin\left(\frac{\pi(x-\frac12(a+b))}{b-a}\right)\right)+\frac{a+b}{2},$$
a triangle wave function obtained by dilating and shifting the example $\arcsin(\sin(x))$ given in Jair Taylor's answer. To get arbitrary closed rays instead, you can shift and reflect $y=|x|$ to get $y=\pm|x-h|+h$.
Here are several:
$f(x)=x$
$f(x)=\vert x\vert$
$f(x)=\lfloor x\rfloor$
$f(x)=\lceil x\rceil$