Skip to content

Instantly share code, notes, and snippets.

@mhuebert
mhuebert / cljs-js-analysis-cache-usage.md
Last active September 9, 2020 11:59
Using existing namespaces from cljs.js

Let's say you want to use cljs.js to eval code using functions declared in your project.

Expressions that only use core functions are simple to evaluate:

(ns foo.try-eval
  (:require [cljs.js :as cljs]))

(def compiler-state (cljs/empty-state))
@ctford
ctford / lenses.clj
Created July 12, 2014 20:40
A Clojure lens implementation based on focus and fmap.
(ns shades.lenses)
; We only need three fns that know the structure of a lens.
(defn lens [focus fmap] {:focus focus :fmap fmap})
(defn view [x {:keys [focus]}] (focus x))
(defn update [x {:keys [fmap]} f] (fmap f x))
; The identity lens.
(defn fapply [f x] (f x))
(def id (lens identity fapply))
@gerritjvv
gerritjvv / gist:5866665
Last active May 24, 2022 21:46
Calling an external process from Clojure
(comment
This is the easiest and most concise way of calling an external process in Java. The inheritIO methods causes the command to output stdout and errout to the same place as your current process (which most of the times is the console), no need to create mechanisms for reading asynchronously from input streams to get at the information.
http://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html
)
(def ret (.waitFor (-> (ProcessBuilder. ["gzip" "-t" "g.txt.gz"]) .inheritIO .start)))
@semperos
semperos / bimap.clj
Created October 4, 2012 18:19
Bidirectional Map in Clojure (Christophe Grand)
;; Big thanks to Christophe Grand - https://groups.google.com/d/msg/clojure/L1GiqSyQVVg/m-WJogaqU8sJ
(defprotocol ReversibleMap
(rmap [m]))
(defn- rdissoc [d r v]
(if-let [[_ k] (find r v)] (dissoc d k) d))
(deftype Bimap [^clojure.lang.IPersistentMap direct reverse]
Object
(hashCode [x]
@mjg123
mjg123 / worker-queue.clj
Created October 21, 2011 22:12
Worker Queues in Clojure
(defn new-q [] (java.util.concurrent.LinkedBlockingDeque.))
(defn offer!
"adds x to the back of queue q"
[q x] (.offer q x) q)
(defn take!
"takes from the front of queue q. if q is empty, block until something is offer!ed into it"
[q] (.take q))