Skip to content

Instantly share code, notes, and snippets.

@mattbierner
mattbierner / fac.kep
Created December 10, 2013 17:28
Defunctionalized Javascript Factorial
var fac = let
facImpl = \n, sum ->
(n ? ['fac', n - 1, n * sum] : ['done', sum]),
trampoline = \k -> {
while (true) {
switch (k[0]) {
case 'done': return k[1];
case 'fac': k = facImpl(k[1], k[2]); break
}
@mattbierner
mattbierner / gist:7474429
Created November 14, 2013 21:14
Atum Line Highlighting Example
model.location.subscribe(\current -> {
if (current && current.start)
cm.removeLineClass(current.start.line - 1, 'background', 'active-line');
}, null, 'beforeChange');
model.location.subscribe(\x -> {
if (x)
cm.addLineClass(x.start.line - 1, 'background', 'active-line');
});
@mattbierner
mattbierner / gist:6891052
Last active December 25, 2015 00:49
Javascript placeholder currying
var _ = {};
var placeholder = function(f /*, ...*/) {
var bound = [].slice.call(arguments, 1);
return function(/*...*/) {
var indx = 0;
return f.apply(f, [].reduce.call(arguments, function(p, c) {
while (indx in bound) {
var val = bound[indx];
if (val === _)
@mattbierner
mattbierner / gist:6858684
Created October 6, 2013 20:28
Atum: Transactional Try Example
var transactionalTryStatement = function(body, handlerId, handler, finalizer) {
return bind(getState, function(state) { // Get the current state.
return tryStatement( // Then evaluate a try statement with an exception handler that
body, // restores the saved state and executes the passed in handler.
handlerId,
next(setState(state), handler),
finalizer);
});
};
@mattbierner
mattbierner / gist:6502727
Created September 9, 2013 23:09
Generate randomly ordered set of indices in a range.
function range(start, end) {
var indicies = [], out = [];
for (var i = start; i < end; ++i)
indicies.push(i);
while (indicies.length) {
var index = Math.floor(Math.random() * indicies.length);
out.push(indicies[index]);
indicies.splice(index, 1);
}
return out;
@mattbierner
mattbierner / gist:6145671
Last active January 14, 2023 17:12
C++ std::tuple map, foldl, and foldr
/*------------------------------------------------------------------------------
Common list operations (map, foldl, foldr) for C++ tuples.
Depends on Cons, Car, and Cdr for tuples from: https://gist.github.com/mattbierner/6145505
------------------------------------------------------------------------------*/
#include "tuple_ops.h"
/* Impl --------------------- */
@mattbierner
mattbierner / main.cpp
Last active January 14, 2023 17:12
C++ std::tuple cdr, car, cons
#include "tuple_ops.h"
template<typename T>
void print_tuple(const T& x)
{
std::cout << "Head:" << Car(x) << " Remaining:" << std::tuple_size<T>::value - 1 << std::endl;
}
int main(int argc, const char* argv[])
{
@mattbierner
mattbierner / gist:5882460
Last active December 19, 2015 02:19
Javascript build trie from array-like
var reduce = Function.prototype.call.bind(Array.prototype.reduce);
var trie = (function(){
var wordReduce = function(parent, l) {
return (parent[l] = (parent[l] || {}));
};
var wordsReduce = function(trie, word) {
var node = reduce(word, wordReduce, trie);
node[''] = null;
return trie;
@mattbierner
mattbierner / gist:3983634
Created October 30, 2012 22:54
Closure Compiler Git pre-commit Hook
#!/bin/sh
# Minify js files
COMPILER="java -jar compiler.jar"
OPTIONS="--compilation_level=ADVANCED_OPTIMIZATIONS"
SRC=lib
DEST=dist
@mattbierner
mattbierner / gist:3784785
Created September 25, 2012 22:11
Gen.js Example
// Source generator for fibonacci sequence
function fibonacci() {
var c = 0, d = 1;
return function(y, b) {
var next = c;
c = d;
d = next + d;
return y(next);
};
}