Skip to content

Instantly share code, notes, and snippets.

View celwell's full-sized avatar

Christopher Elwell celwell

View GitHub Profile
(-> test-results
(->> (map #(list (:id %) (:prefill_config %))))
flatten
(->> (apply hash-map))
keywordize-keys
(select-keys (map keyword current-form-ids)))
@celwell
celwell / Merge Sort in JavaScript.js
Last active January 1, 2019 02:05
Merge Sort in JavaScript, simple implementation
// helper fn used by merge_sort; merges two arrays into sorted order
var merge = function (left, right) {
let merged = [],
i = 0, // left array index counter
j = 0; // right array index counter
while (i < left.length && j < right.length) {
if (left[i] < right[j]) {
merged.push(left[i]);
i++;
@celwell
celwell / memoize.js
Created January 14, 2019 20:57
JS function "memoize": Takes a function, f, and returns a memoized version of f.
// Takes a function, f, and returns a memoized version of f.
// Note: only memoize pure, idempotent functions
var memoize = function (f) {
let store = new Map();
return function (...args) {
const k = JSON.stringify(args);
return (store.has(k) ?
store.get(k) :
store.set(k, f(...args)).get(k));
@celwell
celwell / stringify.js
Last active January 31, 2020 17:32
Recursive re-implementation of JSON.stringify
function stringify(obj, seenStack = []) {
if (obj instanceof Array) {
return "[" +
obj.reduce((acc, v, idx) => {
let sanitizedV = v;
if (typeof sanitizedV === 'string') {
sanitizedV = "\"" + sanitizedV + "\"";
}
return acc + (idx !== 0 ? "," : "") + stringify(sanitizedV, seenStack);
}, "") +
@celwell
celwell / best_hand.clj
Created February 7, 2020 22:15
Answer to 4Clojure #178 - Best Hand
;; http://www.4clojure.com/problem/178
(fn [card-strs]
(let [card-str->map (fn [[suit-char rank-char]]
{:suit ({\S :spade
\H :heart
\D :diamond
\C :club} suit-char)
:rank (case rank-char
\A 12
\K 11
@celwell
celwell / read_roman_numerals.clj
Created February 8, 2020 21:23
Solution to 4Clojure #92 - Read Roman Numerals
(fn [roman-str]
(let [letter-val {\0 0 ;; for padding last tuple
\I 1, \V 5, \X 10, \L 50
\C 100, \D 500, \M 1000}
letter-tuple->dec (fn [[l r]]
(* (letter-val l)
(if (< (letter-val l) (letter-val r))
-1
1)))]
(->> roman-str