Skip to content

Instantly share code, notes, and snippets.

View milesrout's full-sized avatar

Miles Rout milesrout

View GitHub Profile
@milesrout
milesrout / iterators.md
Last active August 29, 2015 14:18
Understanding C++ Iterators

C++'s iterators are a generalisation of pointers-into-arrays. So you can write this:

void sort_array() {
    int arr[10] = {4, 5, 1, 7, 6, 3, 9, 2, 8, 0};
    std::sort(arr, arr + 10); // std::sort<int*>
}

just as you can write

void sort_vector() {

@milesrout
milesrout / adhoc_mutex.c
Last active August 29, 2015 14:18
mutexes
#include <stdatomic.h>
int main()
{
static atomic_flag mutex = ATOMIC_FLAG_INIT;
static unsigned volatile counter = 0;
while (atomic_flag_test_and_set(&mutex));
++counter;
atomic_flag_clear(&mutex);
}
#include <stdatomic.h>
static atomic_int counter = ATOMIC_VAR_INIT(0);
void increment_counter() {
atomic_fetch_add_explicit(&counter, 1, memory_order_relaxed);
}
int get_counter() {
return atomic_load_explicit(&counter, memory_order_relaxed);
#include <stdatomic.h>
int count()
{
static atomic_flag mutex = ATOMIC_FLAG_INIT;
static unsigned volatile counter = 0;
unsigned volatile retval;
while (atomic_flag_test_and_set_explicit(&mutex, memory_order_acquire));
retval = ++counter;
I: location of cursor
initial state
+-------+-------+
| | |
+-------+ |
| I | |
+-------+ |
| | |
+-------+-------+
@milesrout
milesrout / 1.intro.md
Last active May 29, 2016 05:20
Alvin, a language similar to Haskell

In Haskell the trivial type is written as () and has kind ∗. The list type is written as [] and has kind ∗→∗, which means that it is sort of a 'metafunction' from type to type. Applying [] (kind ∗→∗) to Int (kind ∗) gives [] Int i.e. [Int] (kind ∗). The function type is written as (->) and has kind ∗→∗→∗ i.e. ∗→(∗→∗) which means it's a sort of metafunction from type to a function from type to type. Applying (->) (kind ∗→∗→∗) to Int (kind ∗) gives (->) Int (kind ∗→∗), and applying that to Float (kind ∗) gives (->) Int Float i.e. Int -> Float (kind ∗).

In Alvin the trivial type is written as Trivial. The list type is written as List. It is a template, taking a type and 'returning' a type. Applying List to Int gives List::value<Int>. The function type is written as Function. (->) Int is written as Function::value<Int> and Int -> Float is written as Function::value<Int>::value<Float>.

Haskell:

  • 1 is a value of type Int.
  • 1.0 is a value of typ
struct Super {
virtual Super* id() {
return this;
}
};
struct X : Super {};
struct Y : Super {};
template <typename A>
@milesrout
milesrout / 1.hs
Last active August 29, 2015 14:21
-- you can call a function like `map` with a function and a list of integers, e.g.
map (+1) [0,1,2] --> [1,2,3]
-- or on a list of strings
map (++", world!") ["Hello","Goodbye"] --> ["Hello, world!","Goodbye, world!"]
-- question: can we use this function on any type, or just built in types?
-- `map` has this type signature:
map :: (a -> b) -> [a] -> [b]
namespace std {
using numeric_range = struct { min,max,stride: int };
// provide an implementation of the Range concept for numeric_range
impl Range<numeric_range> {
using iterator = struct { min,stride: int };
using sentinel = struct { max: int };
let begin(nr: numeric_range) => iterator{nr.min, nr.stride};
let end(nr: numeric_range) => sentinel{nr.max};
import collections
import itertools
import praw
import time
# maximum 1000
SUBMISSION_LIMIT = 100
def flatten(it):
for x in it: