Skip to content

Instantly share code, notes, and snippets.

int f(int count, int data) {
count += calc_adjustment(data);
UMBRA_FREEZE(count) { g(count) }
}
// ==>
int f(int count, int data) {
count += calc_adjustment(data);
if(auto const& umbra_gensym1tmp = count; true)
UMBRA_IGNORE_SHADOW( //
@rileylev
rileylev / shadow.hpp
Last active May 3, 2022 05:07
Shadow, poison, and frezee
#include "hedley.h"
// https://www.fluentcpp.com/2019/08/30/how-to-disable-a-warning-in-cpp/
#include <initializer_list>
//#pragma GCC diagnostic error "-Wshadow"
#define PRAGMA_IGNORE_SHADOW_GCC \
_Pragma("GCC diagnostic ignored \"-Wshadow\"")
#define MSVC_DISABLE_WARNING(num) __pragma(warning(disable : num))
@rileylev
rileylev / gcd.py
Created July 14, 2021 20:20
stein's gcd algorithm
#!/usr/bin/env python
def is_even(x):
return x & 1
def gcd(x,y):
factors_of_2 = 0
while true:
if x==0:
return y << factors_of_2
@rileylev
rileylev / rels.clj
Last active May 3, 2021 15:31
a few clojure core.logic relations
(ns logic-data-transform.core
(:use [clojure.core.logic])
(:require [clojure.test :as test])
(:require [clojure.core.match :refer [match]]))
(defn assoco
([base & args]
(let [out (last args)
bindings (partition 2 (butlast args))]
(apply conjo base (conj (vec bindings) out)))))
@rileylev
rileylev / minmax_elt.cpp
Created March 18, 2021 17:37
a place for goto
template<class T, class Less = std::less<>>
constexpr auto minmax(T&& x, T&& y, Less less = {})
BODX(less(y, x) ? std::forward_as_tuple(y, x) : std::forward_as_tuple(x, y))
// s is for stable
template<class T, class Less = std::less<>>
constexpr auto smin(T&& x, T&& y, Less less = {})
BODX(std::get<0>(minmax(x, y, less)))
template<class T, class Less = std::less<>>
@rileylev
rileylev / FN.hpp
Last active October 9, 2021 03:50
A silly macro for a shorthand for lambdas
namespace FN_impl {
template<class T>
constexpr auto delay(auto x) {
return x;
}
struct no_arg_passed {
template<class T>
operator T() {
(defgeneric add (x y))
;;; assume we already defined vector and integer classes
(defmethod add ((x vector) (y vector))
....)
(defmethod add ((x integer) (y integer))
(+ x y))
@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)
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 / 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)