Skip to content

Instantly share code, notes, and snippets.

View friemen's full-sized avatar

Falko Riemenschneider friemen

View GitHub Profile
;; parser combinators
(defn pred->parser
[pred]
(fn [[x & remaining :as input]]
(if (pred x)
[x remaining]
[::invalid input])))
(defn alt
;; walk tree-ish vector with Zippers in recursive fashion
(defn walktree
[t]
(letfn [(inner [loc numbers]
(let [node (z/node loc)
numbers (conj numbers (if (sequential? node) (first node) node))
loc (z/edit loc #(if (number? %)
(inc %)
(into [(inc (first %))] (rest %))))]
@friemen
friemen / gist:b2daf61c3d18a8d76184
Created July 14, 2015 18:45
Multi-path update
user> (def data {:x 110 ::old {:x 100}})
#'user/data
user> (update-in data [::old] merge {:x 110 :z 5})
{:user/old {:z 5, :x 110}, :x 110}
user> (-> data
(update-in [::old] merge {:x 110 :z 5})
(merge {:x 200}))
{:user/old {:z 5, :x 110}, :x 200}
user> (defn update-keep-old
[m k v]
@friemen
friemen / om-tut.core.md
Last active August 29, 2015 14:08
Om for boring enterprise-style UIs
@friemen
friemen / servint-core.clj
Last active August 29, 2015 14:07
Web Service Integration based on core.async
(ns servint.core
(:require [org.httpkit.server :as server]
[org.httpkit.client :as client]
[clojure.core.async :as async :refer [go >! <!]]
[compojure.core :refer [defroutes GET]]
[compojure.route :as route]
[compojure.handler :as handler]))
;; A complete webapp that demonstrates how to asynchronously
;; query web services
@friemen
friemen / decision_table.clj
Created October 5, 2014 13:23
Simple decision table implementation
(ns snippets.decision-table)
;; a naive decision table implementation
(defn decision-table
[predicates actions & values]
{:preds predicates
:actions actions
:rows (partition-all (+ (count predicates) (count actions)) values)})
@friemen
friemen / mapping.clj
Last active August 29, 2015 14:07
Data transformation
(ns snippets.mapping
(:require [clojure.string :as s]))
(defn as-vector
"Returns a vector from x
42 -> [42]
'(1 2 3) -> [1 2 3]
[:foo :bar] -> [:foo :bar]"
[x]
@friemen
friemen / core.clj
Last active August 29, 2015 14:01
How to access relational DBs
(ns dbgists.core
"Demonstrates working with a DB connection."
(:require [clojure.java.jdbc :as jdbc])
(:import [com.jolbox.bonecp BoneCPDataSource]))
;; to start H2 DB
#_ (do (require '[dbgists.h2 :as h2])
(h2/start-db))
@friemen
friemen / profiles.clj
Last active August 21, 2023 07:50
My Leiningen profiles.clj
;; put this into profiles.clj in ~/.lein folder
{:user {:jvm-opts ^:replace ["-Xmx6G" "-XX:-OmitStackTraceInFastThrow"]
:repl-options {:timeout 180000}
:plugins [[cider/cider-nrepl "0.35.1"]
[refactor-nrepl "3.9.0"]
[lein-ancient "1.0.0-RC3"]
[jonase/eastwood "0.3.14"]
[lein-try "0.4.3"]
[lein-cloverage "1.0.13"]
[lein-count "1.0.9"]
@friemen
friemen / helloweb_core.clj
Last active August 29, 2015 14:01
Starting with a minimalistic webapp.
(ns helloweb.core
(:require [org.httpkit.server :as httpkit]
[compojure.handler :as handler]
[ring.util.response :refer [redirect response]]
[ring.util.codec :refer [url-encode]]
[hiccup.page :refer [html5]]
[hiccup.form :as f]
[compojure.core :refer [defroutes GET POST]]
[compojure.route :as route]))