Skip to content

Instantly share code, notes, and snippets.

View tolitius's full-sized avatar

Anatoly tolitius

View GitHub Profile
@tolitius
tolitius / 1. raffle.clj
Last active October 17, 2015 03:12
top level "Component" functions
(ns raffle
(:require [raffle.system :refer [find-by-id]]
[clojure.tools.logging :refer [info]]))
(defn raffle-it []
(let [id (rand-int 42)]
(info "and the winner is... "
(find-by-id id))))
@tolitius
tolitius / ns-interns.repl.cljs
Last active December 4, 2015 00:40
cljs: ns-interns vs. ns-interns*
;; Bootstrapped ClojureScript
;; --------------------------
cljs.user=> (require '[cljs.js :as cljs])
cljs.user=> (def foo 42)
#'cljs.user/foo
cljs.user=> (cljs/eval-str (cljs/empty-state) "(ns-interns (quote cljs.user))" "interning" {:eval cljs/js-eval} identity)
{:ns cljs.user, :value {}}
@tolitius
tolitius / strip-aot.clj
Last active December 23, 2015 22:37
boot & lein '[[dependencies]]: stripping aot classifiers
;; useful for test / dev tasks
(defn dep-to-attrs [dep]
(into {} (map vec (partition 2 dep))))
(defn attrs-to-dep [attrs]
(->> attrs
(apply vector)
(apply concat)
vec))
@tolitius
tolitius / start-with.clj
Last active January 20, 2016 21:44
mount: swapping states with values
dev=> (mount/stop)
{:stopped ["#'app.example/nrepl" "#'app.www/nyse-app" "#'app.db/conn"]}
;; swapping nRPEL with 42
dev=> (mount/start-with {#'app.example/nrepl 42})
{:started ["#'app.conf/config" "#'app.db/conn" "#'app.www/nyse-app" "#'app.example/nrepl"]}
dev=> @#'app.example/nrepl
42

Mystique Experiment

Why

Playing around with a "system" concept, where a simple view of all the states can be created by (mount/system), and used.

This could probably be used for testing, although a simple :require does not seem all that frightening in tests.

Mutable, Whaaa!?

(defn connect [db-spec]
(jdbc/get-connection db-spec))
(defn disconnect [conn]
(.close conn))
@tolitius
tolitius / 1. uber-boot.clj
Last active February 5, 2016 17:01
ubermain + push
(deftask build []
(comp
(pom) ;; <<< names the file correctly + no need for --file in "(ubermain)" or "(push)"
(ubermain)
(sift :include #{#"stream-on.*.jar$"})
(target)))
(deftask deploy-snapshot []
(comp
(build)
@tolitius
tolitius / derefable-state.clj
Last active April 5, 2016 20:06
mount: catching not started
(defn deref-state [name]
(let [{:keys [status inst] :as state} (@meta-state name)]
(when-not (:started status)
(up name state (atom #{})))
@inst))
(defn throw-not-started [name]
(throw-runtime (str "can't use \"" name "\" state, since it was not started. "
"this would usually happen if you forget to start it explicitly "
"or to \":require\" this state or its namespace, "
@tolitius
tolitius / 1. kafka-thing.clj
Created April 30, 2016 21:24
mount: accessing states within jars
;; [tolitius:/tmp/ido]$ cat src/ido/core.clj
(ns ido.core
(:require [mount.core :refer [defstate]]))
(defstate kafka :start 42
:stop 0)
;; [tolitius:/tmp/ido]$
;; [tolitius:/tmp/ido]$ lein install
;; Created /private/tmp/ido/target/ido-0.1.0-SNAPSHOT.jar