More patterns

t[f_[a_,b_]]:={f,a,b}

t[g[a,b]]

t[Sin[x,y]]

t[Sin[x+y]]

t[Sin[3+Pi]]

Sin[3+Pi]

t[Sin[3+Pi/2.]]

t[f_[_,b_]_]:=(Print["match!"];{f,b})

FullForm[f_[_,b_]_]

k=t[3 myfun[3,exp]]

Optional arguments

Clear[f,M]

f[a_, b_, c_,mode_:"Lower"]:=
Which[
 mode == "Lower", 
{{a, 0},
{b,c}},
mode=="Upper",
{{a,b},
{0,c}},
True,
Print["mode error in function f"];Abort[]
]

f[M[1],M[2],M[3],"Upper"]//MatrixForm

f[M[1],M[2],M[3],"Lower"]//MatrixForm

f[M[1],M[2],M[3]]//MatrixForm

f[TL,BL,BR,"Symmetric"]//MatrixForm

Clear[f]

f[a_^b_]:={a,b}

{f[1^2],f[2^1],f[Pi^(t+y)],f[(t+y)]}

ff[a_^(b_:1)]:={a,b}

{ff[1^2],ff[2^1],ff[Pi^(t+y)],ff[(t+y)]}

Recap

f[x_]:= (* general function definition *)

f[x_List]:= (* restricting the type of the arguments *)

f[x_,y_]:= (* multiple arguments *)

f[x_,"a"]:= (* fixing one of the arguments *)

f[x_/?NumberQ[x]]:= (* test on argument x *)

f[x_,y_,z_/;condition[z]]:= (* condition on argument z *)

f[x_,y_,z_]/;condition[x,y,z]:=(* condition on arguments *)

f[a_,b__,l___]:=
(* matching one element, a non-empty list of elements, any list  *)

f[a_,b__,l___]:={l}

f[a_+b_]:=(* specifying the form of the argument *)

f[{{a_,b_},___}]:=(* again, specifying the shape of the argument *)

f[m:{_^b_}]:=Power[m,-b]:=(* assigning names to parts of a pattern *)

f[a_,b_:Pi]:=Sin[a+b]:= (* default arguments *)

Pure Functions

"Function"  and  "# &"

Function[{x},x^2]

3^2

sq[x_]:={x^2,x}

Function[{x},x^2][3]
Function[{x},{x,x^2}]@3

x

Function[{a,b,c},a (b+c)][2,3,4]

Map

f1=Function[{x,y},Sin[x^2+y^2]/Sqrt[(x^2+y^2)]];

f1[3,4]

Plot3D[f1[x,y],
{x,0,Pi},
{y,0,Pi}
]

#^2&[3]

# 3&@a

#^3-#^2&[a]

#2&[2,3]

#2&[3]

2{##}&[2,3]

Plus@@{1,2,3}

{
f#&@f,
#@f&@f,
f@#&[f],
f@@{1,2,3},
##&@@{1,2,3}
}//ColumnForm

Plot3D[Cos[#1]+Sin[#1+#2]&[x,y],{x,0,Pi},{y,0,Pi}]

Plot3D[Cos[x]+Sin[x +y],{x,0,Pi},{y,0,Pi}]

h[f_,k_Integer]:=f[#]^k&

h[Sin,3]

h[Sin,3][2.]

{h[Sin,2][1.3],Sin[1.3]^2}

Plot[{
h[# Sin[#]&,1][x],
h[#Sin[#]&,2][x],
h[#Sin[#]&,3][x]
},
{x,-2,3},
AspectRatio->Automatic]

Plot[
Table[
h[# Sin[#]&,i][x],
{i,3}],
{x,-2,3}]

Functional Constructs: Map, Apply...

Clear[h,f,t,l]

l={"a","b",3,4,{-5,x,Exp[Pi]},f[6],t[5]}

l1=Map[h,l]

TreeForm[l]

TreeForm[Map[h,l]]

FullForm[l1]

l

h@l

h@@l

h/@l

TreeForm[l]

Level[l,{2}]

l2=Map[h,l,{1}]

Map[h,l,{1,2}]

l

Map[ {#,Sqrt[#]}&, l ]//MatrixForm

{
{1,2,3},
{4,5,6}
}//MatrixForm

Map[ v[#]&, 
{
{1,2,3},
{4,5,6}
}
]//MatrixForm

{{
{1,2,3},
{4,5,6}
}}//TreeForm

Map[ v, 
{{
{1,2,3},
{4,5,6}
}},{1,3}
]

Map[ v, 
{
{1,2,3},
{4,5,6}
},
{2}
]//MatrixForm

Map[ v, 
{
{1,2,3},
{4,5,6}
},
Infinity
]//MatrixForm

Map[ v[#]&, 
{
{1,2,3},
{4,5,6}
},
Infinity
]//TreeForm

MapAll[ v[#]&, 
{
{1,2,3},
{4,5,6}
}
]

Map[v, t[23,kk,63]]

t[23,kk,63]//TreeForm

v[t[23,kk,63]]//TreeForm

Map[v,1+Pi+c]

Map[v,1+Pi+c+3]

Map[v, t[23,kk,63], Heads->True]

Examples

l=RandomInteger[{10^1,10^2},{15}]

Map[{#,PrimeQ[#]}&,l]//ColumnForm

Is there any prime number in the list?

Map[PrimeQ,l]

I want to see the number!   -> look also at Select

Map[{#,PrimeQ[#]}&,l]

Map[Mod[#,17]==0&,l]

Is there any number divisible by 17?

Map[{#,Mod[#,17]==0}&,l]

Map[Exp[#]&,l]
Map[Exp,l]

e^n?

Map[{#,Exp[#]}&,l]

Map[N[Sin[#]/#]&,l]

Sin[n]/n?

Map[Sin[#]/#&,l]

Map[Sin[x]^#&,l]

Map[Sin[#]&^#&,l]
Map[(Sin[#]^#&)&,l]

Map[Function[{x},Sin[x]^#]&,l][[3]][4.]

Sin[4.]^95

Returning the n-th power of function Sin[x]?

lF=Map[
Function[{x},Sin[x]^#]&,
l
]

lF[[1]][3.]

lf={Sin,Cos,#^3-#&,Integrate[(1-x^2)/x^2,{x,1,#}]&}

Map[#[Pi]&,lf]

Evaluating each function in Pi?

Map[#[Pi]&,lf]

l1={a,b,c,d,e};
l2={1,2,3,4,5};

{l1,l2}//Transpose

Map[#[[1]]^#[[2]]&,Transpose[{l1,l2}],{2}]

Map[g,Transpose[{l1,l2}],{1}]

How to form {a,(b^2),(c^3),(d^4),(e^5)} ?

{l1,l2}//Transpose

Map[#[[1]]^#[[2]]&,Transpose[{l1,l2}]]

lf
lx={x,y,0.,Pi}

Map[#[[1]][#[[2]]]&,Transpose[{lf,lx}]]

Apply each function to the corresponding point

Transpose[{lf,lx}]

#[[1]][#[[2]]]&

Map[#[[1]][#[[2]]]&,Transpose[{lf,lx}]]

MapIndexed[]

Map[v,{a,b,c,d,e}]
MapIndexed[v,{a,b,c,d,e}]

MapIndexed[ #1^#2[[1]]&, {a,b,c,d,e}]

TreeForm[{{a,b},{c,d},e}]

MapIndexed[ v, {{a,b},{c,d},e},{2}]

MapThread[]

l1={1,2,3,4};
l2={a,b,c,d};
l3=Table[Random[],{4}];

MapThread[ t, {l1,l2,l3}]

Apply[] passes all the arguments to a function

Apply[ff,{1,2,3,4,5}]

Apply[Times,{a,2,b,4}]

Apply[Plus,{{1,a},{2,b},{4,d}}]

Apply[Join,{{1},{2,3},{4,5}}]

Syntax

Map[g,{arg0,arg1,arg2}]
g/@{arg0,arg1,arg2}

Apply[g,{arg0,arg1,arg2}]
g@@{arg0,arg1,arg2}

Map[t,l1]

t@l1

t/@l1

t@@l1

More:  Inner[], Outer[], Thread[]...

Inner[Times,{1,2,3,4},{a,b,c,d},Plus]  (* inner product *)

Inner[Plus,{1,2,3,4},{a,b,c,d},Times]

Inner[{#1,#2}& ,{1,2,3,4},{a,b,c,d},Plus]

Select[], Cases[], Position[], Count[]...

?Select

Select[
Table[Random[],{10}],
#>.5&]

Cases[{a, a^b, 3^Pi, Pi^3}, _^_]

Position[{a, a^b, 3^Pi, Pi^3}, _^_]