Skip to content

Instantly share code, notes, and snippets.

View tolitius's full-sized avatar

Anatoly tolitius

View GitHub Profile
@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
@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 / 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 / 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))))
(ns poco)
(gen-class
:name org.stargate.PlainOldClojureObject
:state "state"
:init "init"
:constructors {[Boolean Boolean String] []}
:methods [[isHuman [] Boolean]
[isFound [] Boolean]
[planet [] String]])
@tolitius
tolitius / cauldron.clj
Last active August 29, 2015 14:20
processes cauldron events
(defn listen-to-events [ch transform signal]
(go-loop []
(-> (<! ch)
transform
signal)
(recur)))
@tolitius
tolitius / communicator.clj
Created April 30, 2015 18:08
component with channels
(defrecord Communicator-Channels []
component/Lifecycle
(start [component] (log/info "Starting Communicator Channels Component")
(assoc component
:query (chan)
:query-results (chan)
:tweet-missing (chan)
:missing-tweet-found (chan)
:persistence (chan)
:rt-persistence (chan)
@tolitius
tolitius / with-rest.clj
Last active August 29, 2015 14:16
scala / clojure: pattern matching with rest
repl> (let [[a b] [1 41 3 4 5 6 7 8]]
(+ a b))
42
;; anywhere, function params? sure!
(defn sum [[a b]] ;; define a function (inner brackets fetch first two elements)
(+ a b))
(sum [1 41 3 4 5 6 7 8 9]) ;; use it
@tolitius
tolitius / rxjava.clj
Last active August 29, 2015 14:16
RxJava Clojure example explained
(defn hello ;; a function named "hello"
[&rest] ;; that takes varagrs, hence "&" in a parameter list
;; could be (defn hello [name &rest]), which would mean
;; function "hello" takes one or more params
(-> ;; "->" is called a threading macro, all it does it executes
;; a first expression, takes its result and passes it to the
;; second expression as its first argument, and so forth
(Observable/from &rest) ;; (Class/method params) is a way to call a Java static method in Clojure
(.subscribe ;; ".subscribe" is a method of an object that "(Observable/from &rest)" returns
@tolitius
tolitius / destructuring-error-codes.clj
Created January 22, 2015 03:39
testing for two error codes with types
=> (def error-codes [[:code "ERROR" :type "title"] [:code "ERROR" :type "id"]])
=> error-codes
[[:code "ERROR" :type "title"] [:code "ERROR" :type "id"]]
=> (let [[[_ c1 _ t1] [_ c2 _ t2]] error-codes] [(distinct [c1 c2]) #{t1 t2}])
[("ERROR") #{"id" "title"}]