Skip to content

Instantly share code, notes, and snippets.

@rileylev
rileylev / Y.hpp
Created December 22, 2018 08:46
Y Combinator in C++2a
#include <utility>
template <class F>
struct Y{
F f;
Y(F _f) : f{_f} {}
template<class...arg_t>
auto operator()(arg_t&&...arg) {return f(*this,std::forward<arg_t>(arg)...);}
};
#include <iostream>
int main()
{
std::cout << "hello world\n";
std::getchar();
return 0;
}
@rileylev
rileylev / wm.hpp
Last active January 9, 2019 17:23
with macros
#define WM_GENSYM(name) GENSYM_##name##__COUNTER__
#define WM_WITH(initializer,body) {auto WM_GENSYM() = initializer; body}
@rileylev
rileylev / tinyurl.js
Created January 15, 2019 03:07
tinyurl
var tinyurl = "http://tinyurl.com/"
var urls = []
var encode = function(longUrl){
urls.push(longUrl)
return tinyurl+(urls.length-1)
};
var decode = function(shortUrl){
var index = Number(shortUrl.substring(tinyurl.length))
@rileylev
rileylev / dijkstra.clj
Created April 20, 2019 14:10
Dijkstra's algorithm in clojure
(defprotocol WEIGHTED_GRAPH
(nodes [graph])
(neighbors [graph node])
(weight [graph from to]))
(defn dijkstra
[start goal graph]
(let [nodes (nodes graph)
neighbors #(neighbors graph %)
weight #(weight graph %1 %2)]
@rileylev
rileylev / generator.scm
Created August 22, 2019 00:40
using prompts (delimited continuations) to build generators
(define yield (prompt-tag))
(define (count-helper)
(let loop ((n 0))
(abort-to-prompt yield n)
(loop (1+ n))))
(define this-step count-helper)
(define (count)
@rileylev
rileylev / gen-macro.scm
Last active August 22, 2019 21:41
generators with macros
(define-syntax-parameter yield
(lambda (stx)
(syntax-violation 'yield "~yield~ is undefined outside of a generator" stx)))
(define-syntax-rule (generator body ...)
(let ()
(define yield-tag (make-prompt-tag))
(define (yield% . returns)
(apply abort-to-prompt yield-tag returns))
(define (thunk)
@rileylev
rileylev / meanHittingTime.m
Last active October 13, 2019 23:14
Calculates the mean hitting time of a state in a homogenous markov chain
function K = fixedPoint(M)
[r c] = size(M);
assert(r==c);
I = eye(r);
K = -rref(I-M)(:,r);
K(r)=1;
assert(norm(M*K-K)<.0001*norm(K));
end
function p = projectFromPRn(X)
import re
from hash_map import HashMap
"""
This is the regular expression used to capture words. It could probably be endlessly
tweaked to catch more words, but this provides a standard we can test against, so don't
modify it for your assignment submission.
"""
rgx = re.compile("(\w[\w']*\w|\w)")
@rileylev
rileylev / no_deducing_this_macro.hpp
Created July 6, 2020 22:21
Deducing this, i wish: my current approach just uses a macro to stamp out all the required overloads. The quadruplication is code-generated.
#include <utility>
#define FORALL_CONSTREFS(mac) \
mac(const, &, ) mac(, &, ) mac(, &&, std::move) mac(const, &&, std::move)
class ExampleIntWrapper {
public:
# define OVERLOADS_PLEASE(const_, ref_, move_) \
int const_ ref_ data() const_ ref_ { return move_(data_); }
FORALL_CONSTREFS(OVERLOADS_PLEASE)