Skip to content

Instantly share code, notes, and snippets.

View yayitswei's full-sized avatar

Wei Hsu yayitswei

  • San Francisco, CA
View GitHub Profile
(defn wrap-exception [f]
(fn [request]
(try (f request)
(catch Exception e
{:status 500
:body "Exception caught"}))))
@yayitswei
yayitswei / gist:9294380
Last active August 29, 2015 13:56
linked text boxes
(ns example.core
(:require-macros [cljs.core.async.macros :refer [go go-loop]])
(:require [om.core :as om :include-macros true]
[om.dom :as dom :include-macros true]
[clojure.string :as string]))
(def app-state (atom {:options {:a 1 :b 2 :c 3}}))
(defmulti changed-option (fn [k _] k))
@yayitswei
yayitswei / core.clj
Last active August 29, 2015 14:01
scanview data parser
(ns scanview.core
(:require [clojure.string :as string]
[clojure.data.csv :as csv]
[clojure.pprint :refer [pprint]]
[clojure.java.io :as io]))
(def regex #"[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?")
(defn trim-line? [s]
(when-let [extra-alpha-chars (re-seq #"[A-DF-df-z]" s)]
user=> (time (first (reduce concat (repeat 2000 [1]))))
"Elapsed time: 3.369 msecs"
1
user=> (time (first (reduce into (repeat 2000 [1]))))
"Elapsed time: 9.901 msecs"
1
@yayitswei
yayitswei / queue-test.clj
Last active August 29, 2015 14:08
Unexpected behavior using try/catch in a go block
(ns queue-test
(:require [clojure.core.async :as async :refer [>! <! chan go go-loop put!]]))
(defn process! [item]
{:pre [item]}
(println "processing:" item))
(defn init! []
(let [q (chan)]
(go-loop []
;; wrap-action should be called for all the defined cases, but not for the default case
(case v
1 (wrap-action (foo))
2 (wrap-action (bar))
3 (wrap-action (baz))
(qux))
(ns oauth.digest
(:import (javax.crypto Mac)
(javax.crypto.spec SecretKeySpec)))
(defn hmac
"Calculate HMAC signature for given data."
[^String key ^String data]
(let [hmac-sha1 "HmacSHA1"
signing-key (SecretKeySpec. (.getBytes key) hmac-sha1)
mac (doto (Mac/getInstance hmac-sha1) (.init signing-key))]
Algorithm HmacSHA1 not available
[Thrown class java.security.NoSuchAlgorithmException]
Restarts:
0: [QUIT] Quit to the SLIME top level
Backtrace:
0: javax.crypto.Mac.getInstance(DashoA13*..)
1: user$eval2253.invoke(NO_SOURCE_FILE:1)
2: clojure.lang.Compiler.eval(Compiler.java:5424)
phrygian:tracker wei$ cake repl
user=> (javax.crypto.Mac/getInstance "HmacSHA1")
java.security.NoSuchAlgorithmException: Algorithm HmacSHA1 not available (NO_SOURCE_FILE:0)
phrygian:tracker wei$ lein repl
"REPL started; server listening on localhost:35455."
user=> (javax.crypto.Mac/getInstance "HmacSHA1")
#<Mac javax.crypto.Mac@5759780d>
(defn map-keys [m f]
(into {} (for [[k v] m] [k (f v)])))
;; recursive group-by
(defn group-by-> [starting-target starting-fns]
(loop [target starting-target fns starting-fns]
(if (nil? fns) target
(map-keys (group-by (first fns) target) #(recur % (rest fns))))))
;; usage