Skip to content

Instantly share code, notes, and snippets.

Timothy Baldridge halgari

Block or report user

Report or block halgari

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View set-test.clj
(ns set-test
(:require [criterium.core :refer [bench quick-bench]])
(:import [clojure.lang IPersistentSet]))
(defn- bubble-max-key
"Move a maximal element of coll according to fn k (which returns a
number) to the front of coll."
[k coll]
(let [max (apply max-key k coll)]
(cons max (remove #(identical? max %) coll))))
View reduced_usage.clj
(defn r-some?
"Super fast version of clojure.core/some that does minimal allocation"
[pred coll]
(reduce
(fn [acc v]
(if-let [val (pred coll)]
(reduced val)
acc))
false
coll))
View gist:fff9b90c7786942b937ae21fbbcd9f00
(defn r-some?
"Super fast version of clojure.core/some that does minimal allocation"
[pred coll]
(reduce
(fn [acc v]
(if-let [val (pred coll)]
(reduced val)
acc))
false
View psudeo_fn_ptrs
enum FnNames {
PRINT,
ADD,
SOME_FN,
}
struct Fn {
enum FnNames name;
}
@halgari
halgari / gist:6309500
Created Aug 22, 2013
Load balancer using core.async
View gist:6309500
(ns async-examples.load-balancer
(:require [clojure.core.async :refer :all]))
;; Let's assume we have a DB, and it holds the following pairs of name/ages
(def db-store
{:john 42
:amy 33
:jill 3
@halgari
halgari / gist:7028120
Last active Jan 31, 2018
Async Agents via core.async
View gist:7028120
(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)]
View gist:71dd5cfd8234a982a11e48d2aac003b0
(def ^:dynamic *counters*)
(defn new-counter [name]
"Creates a pair of LongAdders"
...)
(defn get-counter [name]
(if-let [ctr (get *counter* name]
ctr
View xducer.clj
;;;
(->> actors
(map (partial get-data server-pool))
(filter #(> (:age %) 42))
(map :name)
set)
(into #{}
View gist:4136116
;; Example:
;; (doseq-indexed idx [name names]
;; (println (str idx ". " name)
(defmacro doseq-indexed-functional [index-sym [item-sym coll] & body]
`(doseq [[~item-sym ~index-sym]
(map vector ~coll (range))]
~@body))
@halgari
halgari / word-count.clj
Created Mar 2, 2017
Word Count (MMap)
View word-count.clj
;; On my box, wc processes the 11GB JSON file in 50sec
;; This code does it in 11sec
;; Compiled with lein uberjar, and run with java -server -jar word-count-STANDALONE.jar
(ns word-count.core
(:import (java.io RandomAccessFile File)
(java.nio.channels FileChannel$MapMode)
(java.nio ByteBuffer))
(:gen-class))
You can’t perform that action at this time.