Skip to content

Instantly share code, notes, and snippets.

View njlr's full-sized avatar
🌏
F# ing

njlr njlr

🌏
F# ing
View GitHub Profile
int x = 2;
int y = 9;
int z = 4;
// f captures nothing
std::function<int(int)> f = [](int i) {
  return i + 1;
};
// g captures x, y and z
struct Functor {
// The context, or capture
  // For example, an int and an unsigned
  int i;
  unsigned N;
// The lambda
  int operator() (int j) const {
 // For example, a small math function
  return i * j + N;
@njlr
njlr / auto.cpp
Last active September 7, 2017 11:33
struct SomeClassWithAReallyLongName {
// ...
};
SomeClassWithAReallyLongName foo() {
SomeClassWithAReallyLongName x;
return x;
}
int main() {
Vector<1> v;
Vector<2> v;
Vector<3> u;
// etc...
template<int D>
struct Vector {
static constexpr unsigned N = D;
  int data[N];
Vector(int fill = 0) {
  for (int i = 0; i < N; ++i) {
  data[i] = fill;
  }
public final class Vector3 {
 
 public final float x;
  public final float y;
 public final float z;
 
  public Vector3(float x, float y, float z) {
  this.x = x;
  this.y = y;
  this.z = z;
Vector2 v = { 1, 2 };
Vector2 u = { 3, 4 };
Vector2 w = v + w; // Much better!
@njlr
njlr / vector2.cpp
Last active September 7, 2017 11:35
struct Vector2 {
 float x;
  float y;
};
inline Vector2 operator+(Vector2 const& lhs, Vector2 const& rhs) {
 return { lhs.x + rhs.x, lhs.y + lhs.y };
}
Vector2 v = new Vector2(1, 2);
Vector2 u = new Vector2(3, 4);
Vector2 w = v.add(w);
public final class Vector2 {
 
  public final float x;
  public final float y;
 
  public Vector2(float x, float y) {
  this.x = x;
  this.y = y;
  }