Skip to content

Instantly share code, notes, and snippets.

@Deraen
Deraen / user.clj
Last active August 29, 2015 14:13
Set c.t.n refresh dirs, boot & lein. Ignores dirs where `.no-reload` file exists.
(ns user
(:require [clojure.tools.namespace.repl :as ns-tools]
[clojure.string :as string]
[clojure.java.io :as io]))
(ns-tools/disable-reload!)
(let [reload-dirs (->> (or (try
(require 'boot.core)
((resolve 'boot.core/get-env) :directories)
@Deraen
Deraen / random.clj
Created January 12, 2015 16:46
Random Clj notes
; Quoting symbols in collection before passing them to eval so that they are quoted when evaluated
(let [test #{'a 'b 'c}] `(println ~@(map #(eval `''~%) test)))
; => (clojure.core/println (quote a) (quote c) (quote b))
@Deraen
Deraen / pikaday.deps.cljs
Last active August 29, 2015 14:14
pikaday.deps.cljs
{:foreign-libs [{:file "cljsjs/common/pikaday.inc.js"
:provides ["org.pikaday"]}
{:file "cljsjs/common/pikaday.inc.js"
:requires ["org.moment"]
:provides ["org.pikaday.with-moment"]}]
:externs ["cljsjs/common/pikaday.ext.js"]}
@Deraen
Deraen / profile.cljs
Last active August 29, 2015 14:16
Silk Om routing
(ns foobar.ui.view.profile ...)
(defmethod r/route-change :profile-view
[data]
(go
(let [resp (<! (http/get "/api/users/current" {:ref "nav"}))]
(swap! app/app assoc :profile (-> (:body resp)
(f/->form-state domain/User))))
data))
@Deraen
Deraen / highlight.cljs
Last active August 29, 2015 14:16
Hightlighting search results
; Test
(time
(dotimes [i 10000]
(ac/highlight-string "foo bar hello world" ["oo" "ello"])))
; First try
; Speed: ~1200ms
; Problems: only matches term once
(defn highlight-match
; Reagent & re-frame
(defn form-group
[form real-el
{:keys [ks input label]
:or {size 6}
:as opts}]
(let [error (reaction (get-in (::errors @form) ks))]
(fn []
[:div.form-group
@Deraen
Deraen / handler.clj
Created March 12, 2015 16:37
Compojure api middlewares
(defapi app
(swagger-ui "/api-docs")
(swagger-docs)
(context "/js" []
(middlewares [(caching/cache-middleware caching/cache-30d)]
(resources "" {:root "public/js"})))
(context "/images" []
(middlewares [(caching/cache-middleware caching/cache-30d)]
(resources "" {:root "public/images"})))
@Deraen
Deraen / date.cljs
Last active August 29, 2015 14:17
Date formatting in Cljs
(defn zero-pad
([n] (if (<= 0 n 9) (str "0" n) (str n)))
([n pad]
(let [N (js/Math.pow 10 pad)]
(if (< n N)
(.slice (str (+ N n)) 1)
(str n)))))
; Formatter and regex could be generated maybe by using a macro
; Global def is undesired as that can'be optimized by GClosure
@Deraen
Deraen / fckxkb.c
Created May 23, 2015 19:43
Set XKB layout when new input devices are detected
// gcc -Wall -g -o fckxkb fckxkb.c -ludev
#include <libudev.h>
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <unistd.h>
#include <string.h>
int main (void)
@Deraen
Deraen / current-line.clj
Last active August 29, 2015 14:21
Find current line at runtime
(defn user-file-position
"Guesses the file position (basename and line number) that the user is
most likely to be interested in if a test fails."
[]
(nth (map #(list (.getFileName %) (.getLineNumber %))
(.getStackTrace (Throwable.)))
; Magic number
3))
(defmacro ^:private deprecate! [was is]