Skip to content

Instantly share code, notes, and snippets.

@shaunr0b
shaunr0b / dirty_attrs.rb
Last active January 14, 2016 04:37
Dirty Attribute Tracking
# Thanks to @jcarbo, @jwg2s, @jbender
# Rationale:
# Create classes of objects with a self-enforcing schemas,
# and the ability to track which fields have changed. Highly useful
# in modeling, validating, and serializing remote API endpoints,
# especially for PATCH updates.
# Features:
@shaunr0b
shaunr0b / cond->.clj
Last active August 29, 2015 14:07
use cond-> to conditionally add keys to a map
;; Use cond-> to conditionally add keys to a map
;; http://grimoire.arrdem.com/1.6.0/clojure.core/cond-%3E/
(let [a 3
b nil
c false
d "word"]
(cond->
{}
@shaunr0b
shaunr0b / fsm.clj
Last active August 29, 2015 14:07 — forked from Pet3ris/fsm.clj
(ns fsm
(:refer-clojure :exclude [==])
(:use [clojure.core.logic]))
;; Encoding a Finite State Machine and recognizing strings in its language in Clojure core.logic
;; We will encode the following FSM:
;;
;; (ok) --+---b---> (fail)
;; ^ |
;; in a cider repl
;; start system / webserver
(user/reset)
;; Start browser-connected repl
(require 'cljs.repl.browser)
(cemerick.piggieback/cljs-repl
:repl-env (cljs.repl.browser/repl-env :port 9000))
;; Visit http://localhost:3000 in Chrome.
@shaunr0b
shaunr0b / ari-firehose.clj
Last active August 29, 2015 14:05
Firehose
(def box (atom nil))
(def futures (atom nil))
{:a "1" :b "2"}
[:a "1"]
[:b "2"]
(defn my-fn [{input-map :key some-var :a} name [x y z] ]
@shaunr0b
shaunr0b / dropbox.clj
Last active August 29, 2015 14:04
Upload a file to dropbox abstraction
(defn save-file
"Saves a file to dropbox
dropbox component
data spittable data to save
file-label keyword file label
file-ext keyword file extension"
[dropbox data file-label file-ext]
(let [file-label (name file-label)
file-ext (name file-ext)
now (java.util.Date.)
(ns example.runner
(:require [taoensso.timbre :as timbre])))
(timbre/refer-timbre)
(defn run-service
"Apply each member of a collection to a function.
Collect and return simple profiling info,
success and failures."
(ns hello-world.core
(:require [org.httpkit.server :refer [run-server]]
[compojure.core :refer (GET defroutes)]
[compojure.route :as route]
[hiccup.core :refer (html)]
[hiccup.page :refer (html5)]
[ring.middleware.resource :refer (wrap-resource)]
[ring.middleware.file-info :refer (wrap-file-info)]))
(let [a (System/nanoTime)
b (Thread/sleep 2301)
c (System/nanoTime)]
(.toMillis java.util.concurrent.TimeUnit/NANOSECONDS
(- c a))) ; => 2301
; NUMBERS
(+ 5 5)
(* 5 5)
(/ 5 5)
(- 5 5)
(/ 2 3)
(/ 2.2 3.3)