Programming
Bash
Arrays
Regex - grep
Strings
C
Arrays
Regex - POSIX
Strings
C++
Arrays
Bimaps
Functions
Fixed pos args
Var pos args
Anon functions
Callbacks
Closures
Higher-order
Maps
Regex
Strings
Go
Arrays
Maps
Strings
Java
Arrays
Regex
Strings
JavaScript
Arrays
Functions
Objects
Regex
Strings
Perl
Arrays
Regex
Strings
Python
Arrays
Functions
Regex
Strings
C++ Functions
Fixed positional args
No args string f()
{
  return "hello";
}
a=f();
hello
Single arg (int) int f(int n)
{
  return n+1;
}
n=f(1);
2
Single arg (string) string f(const string& a)
{
  return ("good"+a);
}
a=f("bye");
goodbye
Multiple args (int) int f(int a, int b)
{
  return (a+b);
}
n=f(1,2);
3
Variable positional args
Count template <class ... Args>
int f(Args&& ... args)
{
    return sizeof ... (Args);
}
n=f(1,"hello",5.5);
3
First arg template <class ... Args>
int f(Args&& ... args)
{
  auto t = std::make_tuple(
    std::forward<Args>(args)
    ...);
  return std::get<0>(t);
}
// Compilation fails if
// first arg not compatible
// with int
n=f(1,"hello",5.5);
1
Second arg template <class ... Args>
string f(Args&& ... args)
{
  auto t = std::make_tuple(
    std::forward<Args>(args)
    ...);
  return std::get<1>(t);
}
// Compilation fails if
// second arg not compatible
// with string
a=f(1,"hello",5.5);
hello
Third arg template <class ... Args>
double f(Args&& ... args)
{
  auto t = std::make_tuple(
    std::forward<Args>(args)
    ...);
  return std::get<2>(t);
}
// Compilation fails if
// third arg not compatible
// with double
d=f(1,"hello",5.5);
5.5
Anonymous functions (aka lambda functions)
Create in place n/a auto inc_by_3_fp = [] (int x)
{
  return x+3;
};
n=inc_by_3_fp(0);
3
Create by factory std::function<int (int)> mk_inc_f(int n)
{
  // Must capture by value [=]
  // else n will not be valid
  // after mk_inc_f returns
  return [=] (int x)
  {
    return x+n;
  };
}
auto inc_by_3_fp=mk_inc_f(3);
n=inc_by_3_fp(1);
4
Callbacks
Anonymous // Callback signature
using Callback =
  std::function<int (int a)>;
// Caller class
struct Caller
{
  const Callback* cb;
  void set(const Callback& cb)
  {
    this->cb=&cb;
  }
  int call(int a)
  {
    return (*cb)(a);
  }
};
// Define anonymous callback
auto f = [] (int a)
{
  return a*a;
};
// Create caller
Caller c;
// Register callback
c.set(f);
// Invoke caller
int n=c.call(2);
4
Member function // Callback signature
using Callback =
  std::function<int (int a)>;
// Caller class
struct Caller
{
  const Callback* cb;
  void set(const Callback& cb)
  {
    this->cb=&cb;
  }
  int call(int a)
  {
    return (*cb)(a);
  }
};
// Define member callback
class CB
{
public:
  int f(int a)
  {
    return a*a;
  }
};
CB cbObject;
Callback f = std::bind
  (
  std::mem_fn(&CB::f),
  cbObject,
  _1
  );
// Create caller
Caller c;
// Register callback
c.set(f);
// Invoke caller
int n=c.call(3);
9
Global function // Callback signature
using Callback =
  std::function<int (int a)>;
// Caller class
struct Caller
{
  const Callback* cb;
  void set(const Callback& cb)
  {
    this->cb=&cb;
  }
  int call(int a)
  {
    return (*cb)(a);
  }
};
// Define global callback
int f(int a);
int f(int a)
{
  return a*a;
}
// Create caller
Caller c;
// Register callback
c.set(f);
// Invoke caller
int n=c.call(4);
16
Closures
From enclosed function (f) read variable (m) defined in enclosing block n/a {
  m=1;
  // Capture m by value
  auto f = [m] (int x)
  {
    return (x + m);
  };
  n=f(1);
}
2
From enclosed function (f) read variable (m) defined in enclosing block n/a {
  m=1;
  // Capture m by reference
  auto f = [&m] (int x)
  {
    return (x + m);
  };
  n=f(1);
}
2
From enclosed function (g) read variable (m) defined in enclosing function (f) int f()
{
  int m=2;
  auto g = [&] (int x)
  {
    return (x + m);
  };
  return g(1);
}
n=f();
3
Return function (inc_f) that reads variable (m) defined in enclosing function (mk_twice_inc_f) std::function<int (int)> mk_twice_inc_f(int n)
{
  int m=n*2;
  // Must capture by value [=]
  // else n will not be valid
  // after mk_inc_f returns
  auto inc_f = [=] (int x)
  {
    return x+m;
  };
  return inc_f;
}
int incBy=2;
auto f = mk_twice_inc_f(incBy);
incBy=666;
n=f(0);
4
From enclosed function (f) write variable (m) defined in enclosing block n/a {
  m=1;
  // Must capture by reference
  // [&] for m to be writable
  auto f = [&] ()
  {
    m += 4;
  };
  f();
}
5
From enclosed function (g) write variable (m) defined in enclosing function (f) int f()
{
  int m=1;
  // Must capture by reference
  // [&] for m to be writable
  auto g = [&] ()
  {
    m += 5;
  };
  g();
  return m;
}
n=f();
6
Higher-order
Take function as input int inc_by_1_f(int x)
{
  return (x+1);
}
int twice_f(
  std::function<int (int)> f,
  int x)
{
  return f(f(x));
}
n=twice_f(inc_by_1_f,1);
3
Take function as input and output a function int inc_by_2_f(int x)
{
  return (x+2);
}
std::function<int (int)> mk_twice_f(
  std::function<int (int)> f)
{
  // Expected to need
  // capture by value
  // but ref seems ok
  return [&] (int x)
  {
    return f(f(x));
  };
}
auto f = mk_twice_f(inc_by_2_f);
n=f(1);
5