Skip to content

Instantly share code, notes, and snippets.

View halgari's full-sized avatar

Timothy Baldridge halgari

View GitHub Profile
(ns rabbit-mq-test.core
(:require [clojure.core.async :refer [chan dropping-buffer]])
(:import [com.rabbitmq.client ConnectionFactory Connection Channel QueueingConsumer Consumer DefaultConsumer]
))
(set! *warn-on-reflection* true)
(defprotocol Lifecycle
(start [service])
(stop [service]))
(defmacro sq [x]
x)
(defmacro mmeta [x]
(meta x))
(mmeta (sq
(42)))
;; Channels consist of a maximum of 3 queues:
;; chan -> [pending-puts buffer pending-takes]
;; if a buffer is full or empty, the puts or takers will park, and a callback will
;; be put into the pending-puts/pending-takes queue. Once a value is available, a
;; callback will be taken from the queue and the callback will be dispatched.
;; The 1024 limit is on the pending-puts and pending-takes, not on the buffer
;; (the buffer can be any size the user requests).
;; This will work (all values can go directly into the buffer)
(def c (chan 10000))
(defun nrepl-eval-expression-at-point-in-repl ()
(interactive)
(let ((form (nrepl-expression-at-point)))
;; Strip excess whitespace
(while (string-match "\\`\s+\\|\n+\\'" form)
(setq form (replace-match "" t t form)))
(set-buffer "*nrepl*")
(goto-char (point-max))
(insert form)
(nrepl-return)))
@halgari
halgari / gist:7028120
Last active January 31, 2018 12:32
Async Agents via core.async
(use 'clojure.core.async)
(defprotocol ISendable
(channel [this]))
(defn async-agent [state]
(let [c (chan Long/MAX_VALUE) ;; <<-- unbounded buffers are bad, but this matches agents
a (atom state)]
(go-loop []
(when-let [[f args] (<! c)]
@halgari
halgari / gist:7048997
Created October 18, 2013 22:09
It had to be done
(ns crosstalk.core
(:require [eliza-clj.engine :refer :all]))
(def engines [(create-engine)
(create-engine)])
(reduce
(fn [s engine]
(let [v (process-input engine s)]
@halgari
halgari / gist:7160778
Created October 25, 2013 19:47
Using core.logic to query custom data sources.
(ns extend-core-logic.core
(:require [clojure.core.logic :refer :all]
[clojure.core.logic.protocols :refer [walk]]
[clojure.java.io :as jio]
[clojure.string :as string])
(:import [java.io BufferedReader StringReader]))
;; from: http://federalgovernmentzipcodes.us/
(defn load-db []
(let [data (java.io.BufferedReader. (java.io.StringReader. (slurp "/Users/tim/Downloads/free-zipcode-database.csv")))
@halgari
halgari / gist:7223421
Created October 29, 2013 22:00
Reify error
(ns main-ns)
(defprotocol BadReify
(do-stuff [this]))
(defn wrap-stuff [x]
(reify
BadReify
(do-stuff [this]
x)))
(let [take-c (chan 1)]
(go
(loop [pending []
tout (timeout 1000)]
(alt! [take-c] ([v] (let [new-pending (conj pending v)]
(if (> max-pending (count new-pending))
(do (thread (process-stuff pending))
(recur [] (timeout 1000)))
(recur new-pending tout))))
[tout] ([_]
(defun nrepl-eval-expression-at-point-in-repl ()
(interactive)
(let ((form (nrepl-expression-at-point)))
;; Strip excess whitespace
(while (string-match "\\`\s+\\|\n+\\'" form)
(setq form (replace-match "" t t form)))
(set-buffer (nrepl-find-or-create-repl-buffer))
(goto-char (point-max))
(insert form)
(nrepl-return)))