Skip to content

Instantly share code, notes, and snippets.

Avatar

Christopher Elwell celwell

View GitHub Profile
@celwell
celwell / read_roman_numerals.clj
Created Feb 8, 2020
Solution to 4Clojure #92 - Read Roman Numerals
View read_roman_numerals.clj
(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
@celwell
celwell / best_hand.clj
Created Feb 7, 2020
Answer to 4Clojure #178 - Best Hand
View best_hand.clj
;; 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 / stringify.js
Last active Jan 31, 2020
Recursive re-implementation of JSON.stringify
View stringify.js
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 / memoize.js
Created Jan 14, 2019
JS function "memoize": Takes a function, f, and returns a memoized version of f.
View memoize.js
// 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 / Merge Sort in JavaScript.js
Last active Jan 1, 2019
Merge Sort in JavaScript, simple implementation
View Merge Sort in JavaScript.js
// 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++;
View Thread-last and first.clj
(-> test-results
(->> (map #(list (:id %) (:prefill_config %))))
flatten
(->> (apply hash-map))
keywordize-keys
(select-keys (map keyword current-form-ids)))
You can’t perform that action at this time.