This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(loop [o (init-obj)] | |
(when-let [o (try (something-risky) | |
(catch Exception e (record-exception! e)))] | |
(recur o))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// com.google.common.util.concurrent.MoreExecutors | |
sendResult.broadcastComplete.addListener(new Runnable() { | |
@Override | |
public void run() { | |
System.out.println("Complete!"); | |
} | |
}, MoreExecutors.sameThreadExecutor()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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))] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; 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} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; 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"}]] |