Skip to content

Instantly share code, notes, and snippets.

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

njlr njlr

🌏
F# ing
View GitHub Profile
@njlr
njlr / f3.cpp
Created September 7, 2017 10:37
int f() {
 Foo foo;
  return foo.bar(); // foo automatically destroyed
}
@njlr
njlr / f2.cpp
Created September 7, 2017 10:36
int f() {
 Foo* foo = new Foo();
  int x = foo->bar();
  delete foo; // How inconvenient! 
  return x;
}
@njlr
njlr / f.cpp
Created September 7, 2017 10:35
int f() {
  Foo* foo = new Foo();
  return foo->bar(); // Leak! 
}
@njlr
njlr / f.java
Created September 7, 2017 10:35
int f() {
 Foo foo = new Foo();
  return foo.bar();
}
// Pseudo-code
struct VirtualTerminal {
 string buffer; // Currently visible output
 
 string computeTransition(string const& prev, string const& next) {
  const string deleteLine = "\e[2K\r\e[1A";
  return repeat(count("\n", prev), deleteLine()) + next;
  }
 
  VirtualTerminal flip(std::string const& str) const {
running task: download
progress(75%): [xxxxxxxxx ]
Tasks Done: 1/5: [x ]
running task: download
progress(50%): [xxxxxx ]
Tasks Done: 1/5: [x ]
struct Text {
 Style style; 
  std::string text;
  Image render() const {
  std::vector<Pixel> pixels(text.size(), Pixel{ style });
  for (int i = 0; i < text.size(); ++i) {
  pixels[i].c = text[i]; 
  }
  return Image{ width, height, pixels };
struct Renderable {
 virtual Image render(unsigned const width) const = 0;
  virtual ~Renderable() {}
};
template<class T>
struct Model : Renderable {
  T data;
enum class Color { /* ... */ };
enum class Font { /* ... */ };
struct Style {
 Color bg;
  Color fg;
  Font font;
};
struct Pixel {