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 / gpt.clj
Last active September 22, 2023 07:38
streaming chatgpt in clojure
(ns app.gpt
(:require [clj-http.client :as http]
[clojure.repl]
[cheshire.core :as json]
[clojure.pprint :refer [pprint]]
[taoensso.timbre :as log]
[clojure.core.async :as a :refer [<! >! go]]
[clojure.string :as str]
[clojure.java.io :as io])
(:import [java.io InputStream]))
@yayitswei
yayitswei / clj-sse.clj
Created September 22, 2023 06:04 — forked from oliyh/clj-sse.clj
Clojure client for Server Sent Events (SSE)
(require '[clj-http.client :as http])
(require '[clojure.core.async :as a])
(require '[clojure.string :as string])
(require '[clojure.java.io :as io])
(import '[java.io InputStream])
(def event-mask (re-pattern (str "(?s).+?\r\n\r\n")))
(defn- parse-event [raw-event]
(->> (re-seq #"(.*): (.*)\n?" raw-event)
@yayitswei
yayitswei / clj-sse.clj
Created September 22, 2023 06:04 — forked from oliyh/clj-sse.clj
Clojure client for Server Sent Events (SSE)
(require '[clj-http.client :as http])
(require '[clojure.core.async :as a])
(require '[clojure.string :as string])
(require '[clojure.java.io :as io])
(import '[java.io InputStream])
(def event-mask (re-pattern (str "(?s).+?\r\n\r\n")))
(defn- parse-event [raw-event]
(->> (re-seq #"(.*): (.*)\n?" raw-event)
[INFO] +- ring:ring:jar:1.2.0:compile
[INFO] | +- ring:ring-devel:jar:1.2.0:compile
[INFO] | | +- clj-stacktrace:clj-stacktrace:jar:0.2.5:compile
[INFO] | | \- ns-tracker:ns-tracker:jar:0.2.1:compile
[INFO] | | +- org.clojure:tools.namespace:jar:0.1.3:compile
[INFO] | | \- org.clojure:java.classpath:jar:0.2.0:compile
[INFO] | +- ring:ring-jetty-adapter:jar:1.2.0:compile
[INFO] | | \- org.eclipse.jetty:jetty-server:jar:7.6.8.v20121106:compile
[INFO] | | +- org.eclipse.jetty.orbit:javax.servlet:jar:2.5.0.v201103041518:compile
[INFO] | | +- org.eclipse.jetty:jetty-continuation:jar:7.6.8.v20121106:compile
@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());