Skip to content

Instantly share code, notes, and snippets.

View janherich's full-sized avatar

Jan Herich janherich

View GitHub Profile
@janherich
janherich / gist:5133388
Last active December 14, 2015 18:59
Service example
(deftype DbUserService []
PEntityService
(find-entity [_ id auth]
(services-util/authorize-service-call
(authorize-find-call user-authorizator id auth)
(services-util/get-service-result
(validate-find-entity user-validator id)
(services-util/get-success-response
(handle-find-entity user-handler id)))))
(delete-entity [_ id auth]
(->> data
(filter (comp #(re-find #"^([0-9.]+)\s%$" %) #(get % 5)))
(sort-by (comp #(read-string (second (re-find #"^([0-9.]+)\s%$" %))) #(get % 5)) >)
(into []))
@janherich
janherich / gist:8405891
Last active January 3, 2016 03:59
explicit loop vs for
(def url-sequence (map #(str "http://www.mysite.com/list.php?pageID=" %) (range)))
(def download
(loop [urls url-sequence lst []]
(let [u (first urls)
r (rest urls)
resp (client/get u)
next (re-find #"(s?)title=\"next page\">Next >>" (:body resp))
segments ((html2data (:body resp)) :segment)
data (map segment2data segments)]
@janherich
janherich / gist:8456078
Last active January 3, 2016 11:29
Nested from flat
(def data {:b--name "B", :a--id 1, :b--id 2, :a--name "A"})
(defn transform [data delimiter]
(reduce (fn [acc [k v]]
(let [[f-k s-k] (-> k (name) (str/split delimiter))]
(assoc-in acc [(keyword f-k) (keyword s-k)] v))) {} data))
(transform data #"--") ;; {:a {:name "A", :id 1}, :b {:id 2, :name "B"}}
(defn transform [data delimiter]
@janherich
janherich / gist:8594342
Last active January 4, 2016 08:19
polymorphic finder
(require '[clojure.set :as set])
(require '[clojure.java.jdbc :as j])
(def mysql-db {:subprotocol "mysql"
:subname "//127.0.0.1:3306/clojure_test"
:user "clojure_test"
:password "clojure_test"})
(defn product-service [key]
(println (str "Finding product with key: " key))
@janherich
janherich / gist:8738585
Last active August 29, 2015 13:55
time bound channel
(defn create-cancel-channel
[channel-to-notify timeout-ms]
(let [cancel-channel (async/chan)
timeout-channel (async/timeout timeout-ms)]
(async/go
(let [[_ c] (async/alts! [cancel-channel timeout-channel])]
(when (= c timeout-channel)
(async/close! cancel-channel)
(async/close! channel-to-notify))))
cancel-channel))
@janherich
janherich / gist:8776271
Last active August 29, 2015 13:55
Input pipe channel
(ns async-util.input-pipe
(:require [clojure.core.async.impl.protocols :as impl]
[clojure.core.async :refer [<! >! close! go-loop]]))
(defprotocol InputPipe
(detach* [p]))
(defn input-pipe
"Creates and return input pipe of supplied channel with attached input channel."
[ch ch-i]
@janherich
janherich / gist:8905254
Last active August 29, 2015 13:56
SQL helper function
(ns db-util
(:require [clojure.string :as str]))
(def ^:private placeholders-for (comp (partial str/join ",") #(repeat % '?) count))
(defn in-statement
"Takes a prepared SQL statement and variable number of arguments, which may be
also collection values. Replace all occurences of IN (?) with spliced out values
such as IN (?,?,?) where number of placeholder characters is the same as count
of elements in corresponding argument which is assumed to be a collection.
@janherich
janherich / gist:11006018
Last active August 29, 2015 14:00
Demystifying Clojure
;; common points of confusion for people new to Clojure
;; why core functions apparently change the type of given collection ?
(map inc [1 2 3 4]) ;; when evaluated, prints (2 3 4 5), not [2 3 4 5]
(take 2 {:a 1 :b 2 :c 3}) ;; when evaluated, prints ([:a 1] [:b 2]), not {:a 1 :b 2}
@janherich
janherich / projects.json
Last active June 28, 2017 10:30
projects.json
[{
"id" : 392,
"title" : "Solved R&D - Internal Project",
"description" : "Developing the Solved Platform & Processes",
"topics" : {
"DISCIPLINE" : [ {
"id" : 1,
"name" : "General",
"avatar" : "",
"topicGroup" : "DISCIPLINE",