Skip to content

Instantly share code, notes, and snippets.

View candera's full-sized avatar

Craig Andera candera

View GitHub Profile
@candera
candera / example.sh
Last active January 26, 2020 00:43
Package ClojureScript in a Native EXE
# Update: This micro-blog is now also a guide on clojurescript.org:
# http://clojurescript.org/guides/native-executables
# Hello! This is a micro-post about how to produce native executables
# from ClojureScript source. The basic idea is to produce a
# JavaScript file using the ClojureScript compiler, and then use a
# Node.js tool called nexe (https://github.com/jaredallard/nexe) to
# compile that into an executable. The resulting file can be run
# without requiring a node install on the target machine, which can
# be handy.
(let [opacity-ch (async/chan (async/dropping-buffer 1))]
(async/go-loop [last-set (:opacity @app-state)
last-received nil]
(let [t (async/timeout 100)
[ch val] (async/alts! [t opacity-ch])
[new-set new-received] (cond
(= ch opacity-ch)
[last-set val]
(require '[clojure.math.combinatorics :as combo]
'[clojure.core.async :as async])
(def zeus-route
["Kadena"
"Pohang"
"Pusan"
"Kimhae"
"Sachon"
"Taegu"
(require '[scad-clj.scad :refer :all]
'[scad-clj.model :refer :all])
(defn degrees [d]
(-> d (/ 180.0) (* Math/PI)))
(def x-axis [1 0 0])
(def y-axis [0 1 0])
(def z-axis [0 0 1])
import time
class Utils:
@staticmethod
def mapmouse(val):
if abs(val) <= 13:
return val * 128
elif abs(val) <= 25:
return val * 256
@candera
candera / cpu.clj
Created July 7, 2014 16:07
Measure CPU utilization
(defn- cpu-stats
"Returns measures of the various types of CPU usage to date"
[]
(->> (-> (clojure.java.shell/sh "cat" "/proc/stat")
:out
(clojure.string/split #"\n")
first
(clojure.string/split #"\s"))
(remove clojure.string/blank?)
(drop 1)
@candera
candera / ssh-repl.org
Last active February 9, 2019 07:50
ssh-repl

Embedding an SSH-accessible REPL in a Clojure process

N.B. This is now a library, thanks to the efforts of the wonderful @mtnygard. And the README does a good job of making clear just how terrible an idea it is to actually do this. :)

As any Clojurist knows, the REPL is an incredibly handy development tool. It can also be useful as a tool for debugging running programs. Of course, this raises the question of how to limit access to the REPL to authorized parties. With the Apache SSHD library, you can embed an SSH server in any JVM process. It takes only a little code to hook this up to a REPL, and to limit access either by public key or

@candera
candera / transact-firehose.clj
Created March 7, 2014 19:30
transact-firehose
;; This has to be a separate function. The way we had it before, with
;; a loop inside a future, creates a closure that captures the head of
;; the sequence. Locals clearing here saves us from that.
(defn- enqueue-transactions
"Transact a (potentially infinite) sequence of transactions on `q`,
reporting status via `status`. Pause for `delay` (if non-nil)
between each transaction."
[conn [tx & more] q status delay]
(if (or (not tx) (:done @status))
(.put q (future :done))
@candera
candera / dopar.clj
Last active February 25, 2020 14:02
dopar
(defn dopar
"Given a (potentially infinite) sequence `coll`, uses core.async to
run `f` for side effects against each value in the collection.
Performs at most `concur` operations in parallel, and never enqueues
more than `lead` items ahead of the ones being consumed. If any call
to `f` throws an exception, it will be rethrown from this function.
Otherwise, returns nil. Optional timeout value is number of
milliseconds to wait for all operations to complete."
([coll f concur lead] (dopar coll f concur lead nil))
([coll f concur lead timeout-ms]