Skip to content

Instantly share code, notes, and snippets.

View yayitswei's full-sized avatar

Wei Hsu yayitswei

  • San Francisco, CA
View GitHub Profile
@yayitswei
yayitswei / gist:7454198
Last active December 28, 2015 05:59
find the change in a value over time
(defn find-case-insensitive
[db attr val]
(let [a (dq/qe-or-nil
'[:find ?account
:in $ ?pattern ?attr
:where
[?account ?attr ?name]
[(re-find ?pattern ?name)]]
db
(re-pattern (str "(?i)^" val "$")) attr)]
@yayitswei
yayitswei / gist:7395401
Created November 10, 2013 08:26
This snippet doesn't catch an exception in `(something-risky)`.
(loop [o (init-obj)]
(when-let [o (try (something-risky)
(catch Exception e (record-exception! e)))]
(recur o)))
(ns datomic-helpers
(:require [clojure.java.io :as io]
[clojure.walk :as walk]
[datomic.api :as d :refer (db)]))
;;; Expose Datomic vars here, for convenience
(def tempid d/tempid)
(def connect d/connect)
(def create-database d/create-database)
@yayitswei
yayitswei / balance.clj
Last active December 23, 2015 12:29
learning how to use the history db
(defn balance-history [id]
(d/q '[:find ?tx ?tx-time ?v
:in $ ?e ?a
:where [?e ?a ?v ?tx _]
[?tx :db/txInstant ?tx-time]]
(d/history (db/db)) id :account/balance))
@(d/transact conn [[:credit 17592186145525 100]])
(balance-history 17592186145525)
user=> (doc defn)
-------------------------
clojure.core/defn
([name doc-string? attr-map? [params*] prepost-map? body] [name doc-string? attr-map? ([params*] prepost-map? body) + attr-map?])
Macro
Same as (def name (fn [params* ] exprs*)) or (def
name (fn ([params* ] exprs*)+)) with any doc-string or attrs added
to the var metadata. prepost-map defines a map with optional keys
:pre and :post that contain collections of pre or post conditions.
nil
@yayitswei
yayitswei / gist:6550221
Created September 13, 2013 12:47
add a callback. what's an idiomatic way to do this in Clojure?
// com.google.common.util.concurrent.MoreExecutors
sendResult.broadcastComplete.addListener(new Runnable() {
@Override
public void run() {
System.out.println("Complete!");
}
}, MoreExecutors.sameThreadExecutor());
@yayitswei
yayitswei / seed.rb
Created September 12, 2013 06:09
generate a seed (BigInt) from an arbitrary string
require 'digest'
class String
def to_seed
(Digest::SHA256.hexdigest(self).to_i(16))
end
end
puts "abc".to_seed => 84342368487090800366523834928142263660104883695016514377462985829716817089965
puts "123123123".to_seed => 66573445395556389320569794094825521729047035552620196053537129277119858926575
@yayitswei
yayitswei / gist:6362139
Created August 28, 2013 04:28
core.async trouble
(defn fetch-user [username]
(go
(let [ch (chan)]
(letrpc [user (get-user-info username)] ;; shoreleave remote's letrpc macro
(>! ch user)
(close! ch))
ch)))
(defn display-user-info [username]
(go (let [u (<! (fetch-user username))]
@yayitswei
yayitswei / gist:6168124
Created August 6, 2013 20:06
summarize
; how would you write this function summarize?
(summarize [{:from "A" :to "B" :amount 3} {:from "B" :to "A" :amount 1}]) => {:from "A" :to "B" :amount 2}
; update-vec merges a vector based on the :id's of its contents
(update-vec [{:id 1 :data "1"} {:id 2 :data "2"}] {:id 3 :data "3"})
;=> [[{:id 1 :data "1"} {:id 2 :data "2"} {:id 3 :data "3"}]]
(update-vec [{:id 1 :data "1"} {:id 2 :data "2"}] {:id 2 :data "tonystark"})
;=> [[{:id 1 :data "1"} {:id 2 :data "tonystark"}]]