Skip to content

Instantly share code, notes, and snippets.

@johanatan
johanatan / log-opts.cljs
Created March 15, 2016 21:53
log-opts (bunyan helper)
(defn log-opts [logger level msg & params]
(let [amap (apply array-map params)
expected (count (re-seq #"%s|%d|%j" msg))
args (flatten [amap msg (if (not (empty? amap)) (take expected (vals amap)))])]
(.apply (aget logger level) logger (clj->js (remove nil? args)))))
@johanatan
johanatan / merge-sorted-lists.clj
Created March 11, 2016 23:09
Interview question - merge two sorted linked lists
;; merge two sorted linked lists
;; h1 => 2 => 3 => 5
;; h2 => 4 => 4 => 6 => 7
;; h => 2 => 3 => 4 => 4 => 5 => 6 => 7
(defn splitter [lst1 lst2]
(cond (empty? lst1) [lst2 nil nil]
(empty? lst2) [lst1 nil nil]
:else (let [[l1 l2] (if (< (first lst1) (first lst2)) [lst1 lst2] [lst2 lst1])]
@johanatan
johanatan / gist:607127a37610969af54e
Created December 27, 2015 19:32
listp in clojure
cljs.user=> (type '(1 2 3))
cljs.core/List
cljs.user=> (= clojure.core/List (type (cons 1 '(2))))
false
cljs.user=> (type (cons 1 '(2)))
cljs.core/Cons
@johanatan
johanatan / gist:6338182
Created August 26, 2013 04:48
Get files matching regex pattern in node.js (coffeescript).
getFilesMatching = (pattern) ->
splitted = pattern.split '/'
[dir, pattern] = [((_.initial splitted).join '/'), _.last splitted]
pattern = (pattern.replace '.', '\.').replace '*', '.*' # convert shell regex to PCRE
_.compact _.map (fs.readdirSync dir), (f) -> if f.match pattern then "#{dir}/#{f}"
@johanatan
johanatan / gist:5802821
Last active December 18, 2015 15:09
Implementation of bash command line history stack in 20 lines of coffeescript. Some tricky corner cases are covered.
class CommandHistory
constructor: ->
@stack = []
@curPos = -1
@pushShouldReplaceLast = false
put = (index, cmd) =>
if @pushShouldReplaceLast then @stack[index..index] = cmd
else @stack.push cmd
@curPos = index