Skip to content

Instantly share code, notes, and snippets.

@samn
samn / lock_less_monster.clj
Last active April 4, 2022 01:45
The Lock Less Monster
#_(
Let's say you have a threadpool doing work that requires access to some shared resource.
And this resource needs to be refreshed at times. E.g. an OAuth bearer token that can expire or be revoked.
If this resource were expensive to refresh, or subject to rate limiting (OAuth tokens are often both)
then it's desirable to refresh as little as possible.
It's undesirable, however, to mix complicated synchronization code for updating the resource in
with the business logic consuming it.
Enter the lock less monster, a lock free method for coordinating updates to a shared reference.
@willurd
willurd / web-servers.md
Last active May 13, 2024 01:24
Big list of http static server one-liners

Each of these commands will run an ad hoc http static server in your current (or specified) directory, available at http://localhost:8000. Use this power wisely.

Discussion on reddit.

Python 2.x

$ python -m SimpleHTTPServer 8000
@w01fe
w01fe / locals_clearing.clj
Created March 5, 2013 05:43
A bug in fine-grained locals clearing in Clojure 1.5?
(deftype FinalizeReporter [x]
Object
(finalize [this] (println "Finalizing" x))
clojure.lang.IDeref
(deref [this] x))
(defn leaker [n]
(with-open [_ (reify java.io.Closeable (close [this]))]
(let [s (map #(FinalizeReporter. %) (take 10 (iterate inc 0)))]
(when (> n -1)
@hugoduncan
hugoduncan / gist:4138017
Created November 24, 2012 01:49
lb being called on :clojure.core.logic/not-found
No implementation of method: :lb of protocol: #'clojure.core.logic/IInterval found for class: clojure.lang.Keyword
java.lang.IllegalArgumentException
Restarts:
0: [CONTINUE] Pass exception to program
1: [ABORT] Abort request.
2: [IGNORE] Do not enter debugger for this exception type
3: [IGNORE-MSG] Do not enter debugger for this exception message
4: [IGNORE-CATCH] Do not enter debugger for exceptions with catch location clojure.lang.LazySeq.*
5: [IGNORE-LOC] Do not enter debugger for exceptions with throw location clojure.core.*
@jasonrudolph
jasonrudolph / 00-about.md
Created September 21, 2012 18:42
Rough Notes from Strange Loop 2012
@frenchy64
frenchy64 / fbound.clj
Created August 10, 2012 13:52
F-bounded Polymorphism in Typed Clojure
; y has upper bound x, z has upper bound (Seqable y)
typed.core=> (ann foo (All [x [y :< x] [z :< (Seqable y)]]
[x y z -> Any]))
[typed.core/foo (All [x [y :< x] [z :< (clojure.lang.Seqable y)]] (Fn [x y z -> Any]))]
typed.core=> (declare foo)
#'typed.core/foo
;cf = check-form
typed.core=> (cf (foo 2 2 [2]))
Any
@swannodette
swannodette / gist:3217582
Created July 31, 2012 14:52
sudoku_compact.clj
;; based on core.logic 0.8-alpha2 or core.logic master branch
(ns sudoku
(:refer-clojure :exclude [==])
(:use clojure.core.logic))
(defn get-square [rows x y]
(for [x (range x (+ x 3))
y (range y (+ y 3))]
(get-in rows [x y])))
@jboner
jboner / latency.txt
Last active May 12, 2024 19:52
Latency Numbers Every Programmer Should Know
Latency Comparison Numbers (~2012)
----------------------------------
L1 cache reference 0.5 ns
Branch mispredict 5 ns
L2 cache reference 7 ns 14x L1 cache
Mutex lock/unlock 25 ns
Main memory reference 100 ns 20x L2 cache, 200x L1 cache
Compress 1K bytes with Zippy 3,000 ns 3 us
Send 1K bytes over 1 Gbps network 10,000 ns 10 us
Read 4K randomly from SSD* 150,000 ns 150 us ~1GB/sec SSD
(def users [{:name "Brian" :age 22} {:name "Ben" :age 19}])
;; Takes a "path", returns a function that takes an "object" and
;; returns a "costate"
(defn lens [p]
(fn [o]
{:get (get-in o p)
:set #(assoc-in o p %1)}))
(def myAgeLens (lens [0 :age]))
@lambda-fairy
lambda-fairy / Fold.hs
Created May 27, 2012 07:44
Data.List.Fold - fold lists in constant space
-- | Lock-step folding.
--
-- This module presents an elegant way of executing multiple left folds
-- on one list using constant memory. For example, the mean can be
-- expressed as:
--
-- @
-- average = foldLeft $ (/) <$> sumF <*> lengthF
-- @