Skip to content

Instantly share code, notes, and snippets.

@scientific-coder
scientific-coder / core.clj
Last active October 7, 2018 15:27
Parsing some scala code to svg for Kingdomino deck updated for formatting, thx!
(ns kingdomino.core
(:require [instaparse.core :as insta])
(:gen-class))
(def size 100)
(def delta-sides 5)
(def ncols 2)
(def delta-dominos 10)
(def colors {:clay "#cc6600" :field "#ffff66" :water "#33ccff" :pasture "#00cc00" :forest "#669900" :mine "#5c5c3d"})
@scientific-coder
scientific-coder / chaos-polygons.cxx
Created May 4, 2016 00:34
generate pics of generalized chaos sierpinski-like figures on polygons
#include <iostream>
#include <tuple>
#include <cmath>
#include <cstdlib>
#include <iterator>
#include <vector>
#include <algorithm>
#include <string>
// STEPS=100;for i in $(seq $((3 * $STEPS)) $((8 * $STEPS))); do ./chaos-polygons $(bc -l <<< "scale=2;$i / $STEPS") > generated-cmax-100-$i.pgm; done
// for i in generated-cmax-100-*.pgm; do convert $i ${i%.pgm}.png; done
@scientific-coder
scientific-coder / core.clj
Created February 2, 2015 18:00
Toy bytecode compiler for integer arithmetic DSL
(ns clojr.core
(:require [instaparse.core :as insta])
(:import (clojure.lang DynamicClassLoader)))
(import '[clojure.asm ClassWriter Type Opcodes]
'[clojure.asm.commons Method GeneratorAdapter])
(def parser
(insta/parser
"prog = expr (<#'[\\n]+'> expr?)*
@scientific-coder
scientific-coder / FizzBuzz.cxx
Created October 24, 2014 15:34
FizzBuzz in C++ with some influence ☺
#include <iostream>
#include <iterator>
#include <string>
#include <tuple>
#include <functional>
#include "transducers.hxx"
#include "tuple_reader.hxx"
// usage: seq 1 20 |./FizzBuzz 3 Fizz 5 Buzz
@scientific-coder
scientific-coder / compose.hxx
Last active August 29, 2015 14:07
FizzBuzz
#include <functional>
// TODO : monoid
template<typename... Fn> struct composer;
template<> struct composer<> {
template<typename Data> Data operator()(Data d){ return d; }
};
template<typename F0, typename... Fn>
struct composer<F0, Fn...> : private composer<Fn...> {
F0 f;
composer(F0 f0, Fn const&... fn): composer<Fn...>(fn...), f(f0){}
@scientific-coder
scientific-coder / transducers.cxx
Last active August 29, 2015 14:05
first stab at transducers —map, filter and flatmap— in idiomatic (‽) C++, now with FizzBuzz !
#include <array>
#include <iostream>
#include <numeric>
#include <algorithm>
#include <tuple>
#include <sstream>
/* const correctness and pass by value vs const ref, move semantics
left as an exercise to the reader.
iterator range API prevents a flatmap as the flatmapped function
could not either return a value or a range.
@scientific-coder
scientific-coder / JIT-assembly-dump
Last active December 24, 2015 09:38
amap & areduce
Decoding compiled method 0x00007fe951138a90:
Code:
[Entry Point]
[Constants]
# {method} '<init>' '(Ljava/net/URLClassLoader;Ljava/lang/String;)V' in 'java/net/URLClassLoader$1'
# this: rsi:rsi = 'java/net/URLClassLoader$1'
# parm0: rdx:rdx = 'java/net/URLClassLoader'
# parm1: rcx:rcx = 'java/lang/String'
# [sp+0x20] (sp of caller)
0x00007fe951138bc0: mov 0x8(%rsi),%r10d
@scientific-coder
scientific-coder / fibonacci-algebraic.clj
Last active December 18, 2015 02:19
fibonacci using algebraic properties of matrix multiplication and fast power algorithm, adapted from this *great* book : "Elements of Programming" by Alexander Stepanov and Paul McJones http://www.elementsofprogramming.com/
(defn power [a n op]
"Compute a to the (positive int) power n using operator op, using
exponentiation by squaring."
(let [ power-accumulate-positive (fn [r a n op]
(let [r (if (odd? n) (op r a) r)]
(if (= 1 n)
r
(recur r (op a a) (quot n 2) op))))]
(if (odd? n)
(let [n (quot n 2)]
(ns rxjava-datomic.query
(:require [datomic.api :as d])
(:use [clojure.repl :only [pst]])
(:import [rx Observable]
datomic.Peer))
(defn query [qry & inputs]
(Observable/toObservable
(Peer/q qry (object-array inputs))))
(defn- optimal-plan-ordered [bids]
(let [optimal-plan-after (fn [flight]
(let [earliest (+ (:DEPART flight) (:DUREE flight))
after (drop-while #(< (:DEPART %) earliest) bids)]
(if (empty? after) {:gain 0 :path '()} (optimal-plan-ordered after))))
current-plan (fn [flight]
(let [{:keys [gain path]} (optimal-plan-after flight)]
{:gain (+ (:PRIX flight) gain) :path (conj path (:VOL flight))}))]
(reduce #(if (> (:gain %2) (:gain %1)) %2 %1) (map current-plan bids))))