Skip to content

Instantly share code, notes, and snippets.

@rilerez
rilerez / Y.hpp
Created Dec 22, 2018
Y Combinator in C++2a
View Y.hpp
#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)...);}
};
View hello-world.cpp
#include <iostream>
int main()
{
std::cout << "hello world\n";
std::getchar();
return 0;
}
@rilerez
rilerez / wm.hpp
Last active Jan 9, 2019
with macros
View wm.hpp
#define WM_GENSYM(name) GENSYM_##name##__COUNTER__
#define WM_WITH(initializer,body) {auto WM_GENSYM() = initializer; body}
View tinyurl.js
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))
@rilerez
rilerez / dijkstra.clj
Created Apr 20, 2019
Dijkstra's algorithm in clojure
View dijkstra.clj
(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)]
@rilerez
rilerez / generator.scm
Created Aug 22, 2019
using prompts (delimited continuations) to build generators
View generator.scm
(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)
@rilerez
rilerez / gen-macro.scm
Last active Aug 22, 2019
generators with macros
View gen-macro.scm
(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)
@rilerez
rilerez / meanHittingTime.m
Last active Oct 13, 2019
Calculates the mean hitting time of a state in a homogenous markov chain
View meanHittingTime.m
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)
View wordcount.py
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)")
@rilerez
rilerez / no_deducing_this_macro.hpp
Created Jul 6, 2020
Deducing this, i wish: my current approach just uses a macro to stamp out all the required overloads. The quadruplication is code-generated.
View no_deducing_this_macro.hpp
#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)