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)
;; 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))
@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 []
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 / 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)]
@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))
(defn wrap-exception [f]
(fn [request]
(try (f request)
(catch Exception e
{:status 500
:body "Exception caught"}))))
[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