Skip to content

Instantly share code, notes, and snippets.

@missingfaktor
Created September 6, 2016 12:56
Show Gist options
  • Select an option

  • Save missingfaktor/63b99e83075c03f2dd7f16d2a73390a0 to your computer and use it in GitHub Desktop.

Select an option

Save missingfaktor/63b99e83075c03f2dd7f16d2a73390a0 to your computer and use it in GitHub Desktop.
(ns akar.talk-examples
(:require [akar.try-out :refer :all]
[clojure.xml :as xml])
(:import [java.util.concurrent ConcurrentHashMap]
[java.io ByteArrayInputStream]))
; Example 1
(def request-ids-pattern
#"rid=(.*)\|(.*)")
(defn request-ids [headers]
(let [request-ids-header (get headers "X-ABC-REQUEST-IDS")
[main correlation] (some->> request-ids-header (re-seq request-ids-pattern) first rest)]
{:main main
:correlation correlation}))
(request-ids {"X-ABC-REQUEST-IDS" "rid=2|3"})
; Example 2
(def cache
(ConcurrentHashMap.))
(defn respond-to-event [event]
(case (:evt-type event)
(:child-added :child-updated) (.put cache (:path event) (:data event))
:child-removed (.remove cache (:path event) (:data event))
nil))
(respond-to-event {:evt-type :child-added :path "a" :data 11})
; Example 3
(defn record-http-call-metrics [status-code]
(cond
(< 199 status-code 300) (println :ok)
(< 299 status-code 400) (println :xxx)
(< 399 status-code) (println :error)))
; Example 4
(defn seqsum [xs]
(if (empty? xs)
0
(+ (first xs)
(seqsum (rest xs)))))
; Example 5
(defn finden [xs pred]
(if (empty? xs)
nil
(let [x (first xs)]
(if (pred x)
x
(find (rest xs) pred)))))
; Example 6
; https://github.com/clojure/clojure/blob/clojure-1.7.0/src/clj/clojure/core.clj#L2940
; Example 7
; http://gettingclojure.wikidot.com/cookbook:xml-html
(def xml-doc
(xml/parse (ByteArrayInputStream. (.getBytes
"<?xml version=\"1.0\"?>
<calendar>
<holiday type=\"International\">
<name>International Lefthanders Day</name>
<date>
<month>August</month>
<day>13</day>
</date>
</holiday>
<holiday type=\"Personal\">
<name>Rover's birthday</name>
<date>
<month>October</month>
<day>12</day>
</date>
</holiday>
<holiday type=\"National\">
<name>Groundhog Day</name>
<date>
<month>February</month>
<day>2</day>
</date>
</holiday>
<holiday type=\"State\">
<name>Kamehameha Day</name>
<date>
<month>June</month>
<day>11</day>
</date>
</holiday>
</calendar>"))))
(defn holiday-name [holiday]
(first (:content (first (:content holiday)))))
(defn holiday-month [holiday]
(first (:content (first (:content (second (:content holiday)))))))
(defn holiday-day [holiday]
(first (:content (second (:content (second (:content holiday)))))))
(defn holiday-info [doc]
(for [elt (xml-seq doc) :when (= :holiday (:tag elt))]
[(holiday-name elt) (holiday-month elt) (holiday-day elt)]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment