Skip to content

Instantly share code, notes, and snippets.

@ramntry
ramntry / automemoization.hs
Last active Sep 25, 2022
Auto-memoization in Haskell by State monad
View automemoization.hs
import Control.Monad.State
import qualified Data.Map as Map
recall :: Ord a => a -> State (Map.Map a r) (Maybe r)
recall n = do
memory <- get
return (Map.lookup n memory)
memorize :: Ord a => a -> r -> State (Map.Map a r) ()
memorize n result = do
@ramntry
ramntry / simple_daytime_server.cpp
Created Oct 3, 2012
Simple Boost.Asio based synchronous multithreaded server
View simple_daytime_server.cpp
#include <ctime>
#include <string>
#include <iostream>
#include <boost/bind.hpp>
#include <boost/asio.hpp>
#include <boost/thread.hpp>
#include <boost/format.hpp>
#include <boost/smart_ptr.hpp>
View van-ecks-seq.cpp
#include <iostream>
#include <vector>
class Tracker {
public:
void record(int val, int index);
int get_last_index(int val);
std::pair<int, int> get_stats() const;
private:
@ramntry
ramntry / main.cpp
Created May 5, 2019
Very pretty triangle (OpenGL)
View main.cpp
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdocumentation"
#define GLEW_STATIC
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#pragma clang diagnostic pop
#include <iostream>
@ramntry
ramntry / float.cpp
Last active Aug 9, 2018
IEEE-754 single precision software implementation
View float.cpp
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <bitset>
#include <limits>
#include <cassert>
using namespace std;
struct single {
View slp.c
double foo(double *a, int n) {
double x = 0.0;
double y = 0.0;
for (int i = 0; i < n; i += 2) {
x += a[i];
y += a[i + 1];
}
return x * y;
}
View SCEV.md
  // |  Expression   |     ConstantRange      |       KnownBits       |
  // |---------------|------------------------|-----------------------|
  // | i8 4 * X      | [L: 0, U: 253)         | XXXX XX00             |
  // |               |   => Min: 0, Max: 252  |   => Min: 0, Max: 252 |
  // |               |                        |                       |
  // | i8 4 * X + 5  | [L: 5, U: 2) (wrapped) | YYYY YY01             |

// | (101) | => Min: 0, Max: 255 | => Min: 1, Max: 253 |

View isItPythonOrJavaScript.m
#import <Foundation/Foundation.h>
#include <stdio.h>
@interface NSString (MyPrintable)
- (const char *)toCString;
@end
@implementation NSString (MyPrintable)
// NSString is a library provided class that doesn't have
// a toCString method. We're dynamically extending the class
View set_difference_size.cpp
#include <algorithm>
#include <iostream>
#include <iterator>
#include <list>
template <typename T>
class Counter : public std::iterator<std::output_iterator_tag, T> {
size_t &Value;
public:
View test_cons.ll
%dag = type { { { i8, { i8 } }, { { i8, { i8 } }, { i8 } } }, { { i8, { i8 } }, { i8 } } }
define void @test_const(%dag* %dst) {
entry:
%updated = insertvalue
; Check that we're visiting constants with shared parts
; (deduplicated via LLVMContext, forming a proper DAG) correctly:
%dag {
{ { i8, { i8 } }, { { i8, { i8 } }, { i8 } } } {
{ i8, { i8 } } {