Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / 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}
#include <iostream>
int main()
{
std::cout << "hello world\n";
std::getchar();
return 0;
}
@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)...);}
};