ul=RandomInteger[{1,20},10]

g[ul]

Map[fun,ul]

Apply[g,ul]

Clear[g]

g[args1___,l_,r_,args2___]/;l>r:=g[args1,r,l,args2]

g[l_List]:=Apply[g,l]   (* this works *)

g[{l__}]:=g[l]  (* this also works *)

g[ul]

Clear[g]

g[{args1___,l_,r_,args2___}]/;l>r

g[List[args1___,l_,r_,args2___]]/;l>r:=
g[List[args1,r,l,args2]]

g[ul]

g[x_]:=x

g[x_List]:=x

g[{x_}]:={x}  (* nope *)

g[{x__}]:={x}

g[{__}]:=

g[name:{__}]:=name

g[ul]

h[{a_,{_,_},-1}]:=fun[a]

h[{_,{b_,c_},-1}]:=fun[b,c]

h[{_,p:{_,_},-1}]:=fun[p]

RandomInteger[{1,2},20]

seq={"H","T"}[[
RandomInteger[{1,2},1000]
]];

Map[{#,1}&,seq]

Clear[init]

init[done_,{f_,r___}]:=
init[
Join[{{f,1}},done],
{r}
]

init[{done___,{}}]:={done}   (* nope *)

init[done_,{}]:=done

i1=init[{},seq];

{{H,7},{T,1},{H,1},{T,4},...}

comp[{l___,{ht_,x_},{ht_,y_},r___}]:=
comp[{l,{ht,x+y},r}]

comp[x_List]:=x

comp[init[{},seq]]//Length

$IterationLimit=100000

n=100;
comp[init[{},{"H","T"}[[RandomInteger[{1,2},n]]]]]

n=2000;
comp[init[{},{"H","T"}[[RandomInteger[{1,2},n]]]]]//Length

rand=RandomSample[Range[10]]

randIdx=MapIndexed[{#2[[1]],#1}&,rand]

cycle[{l___,{prefix__,idx_},m___,{idx_,val_},r___}]:=
cycle[{l,{prefix,idx,val},m,r}]

cycle[{l___,{a_,sub___,a_},r___}]:=
cycle[{l,{sub,a},r}]

cycle[l_List]:=l

cycle[randIdx]

cyclesN[n_]:=
cycle[
MapIndexed[{#2[[1]],#1}&,RandomSample[Range[n]]]
]

cyclesN[150]//Length