Skip to content

Instantly share code, notes, and snippets.

View tolitius's full-sized avatar

Anatoly tolitius

View GitHub Profile
@tolitius
tolitius / dom-selector.clj
Last active August 29, 2015 13:56
DOM selector observes all the changes made to the element, no matter the source
;; this is a ClojureScript REPL that is connected to a browser
;; (here is how it's connected: https://github.com/tolitius/wracer/blob/master/docs/README.md)
;; "sel1" here is a "dommy" library selector: e.g. similar to "$" in JQuery
wracer.repl=> (def $e-names (sel1 :.e-names))
#<[object HTMLDivElement]>
wracer.repl=> (inner-html $e-names)
<p class="e-name">Bing</p>
<p class="e-name">Google</p>
@tolitius
tolitius / Bean.java
Last active August 29, 2015 14:13
filters not null fields of a bean (e.g. has getters for all of the fields)
public class Bean {
private String a;
private Integer b;
private String c;
private Integer d;
public Bean( String a, Integer b, String c, Integer d ) {
this.a = a;
this.b = b;
@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"}]
@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 / 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 / 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 / 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)))
(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 / src.balanced-check.clj
Last active September 29, 2015 23:28
checks if a given code snippet parentheses are balanced
(ns balance-check.core)
(defn balanced? [s]
(-> (reduce (fn [[h & r :as stack] c]
(case c
\( (conj stack 42)
\) (if (= h 42)
r
(conj stack "unbalanced"))
stack))
@tolitius
tolitius / nosql-top-dogs.js
Created February 27, 2012 06:26
MongoDB Map/Reduce for Chariot Day
db.nosqlrep.insert( { _id : 1, votes : ['riak', 'couchdb', 'cassandra'] } );
db.nosqlrep.insert( { _id : 2, votes : ['redis', 'onetick', 'couchdb'] } );
db.nosqlrep.insert( { _id : 3, votes : ['redis', 'riak', 'couchdb'] } );
db.nosqlrep.insert( { _id : 4, votes : ['voltdb', 'mongodb', 'hazelcast'] } );
db.nosqlrep.insert( { _id : 5, votes : ['hazelcast', 'riak', 'redis'] } );
db.nosqlrep.insert( { _id : 6, votes : ['redis', 'cassandra', 'onetick'] } );
db.nosqlrep.insert( { _id : 7, votes : [] } );
print( "\ncommunity votes" )
print( "---------------" )