Skip to content

Instantly share code, notes, and snippets.

@ifesdjeen
ifesdjeen / pgp.md
Created June 17, 2013 09:58
How to use pgp authentication with leiningen

In order to setup leiningen with pgp, you have to create a ~/.lein/credentials.clj file and put following contents into it:

{
  #"https:\/\/your.nexus.server.address\/" {:username "your_username" :password "supersecret"}
}

After that you can run lein deploy.

@ifesdjeen
ifesdjeen / repeatedly.clj
Created June 13, 2013 10:08
Fast repeatedly
(defn repeatedly*
"Like `repeatedly` but faster and returns given collection type."
[coll n f]
(if-not (instance? clojure.lang.IEditableCollection coll)
(loop [v coll idx 0]
(if (>= idx n)
v
(recur (conj v (f)) (inc idx))))
(loop [v (transient coll) idx 0]
(if (>= idx n)
@ifesdjeen
ifesdjeen / new_gist_file
Created May 9, 2013 11:27
Non-confusing contains? function for vectors
(defn vec-contains?
[vec value]
(not (nil? (some #(= value %) vec))))
(vec-contains? ["a" "b" "c"] "a") ;; true
(vec-contains? ["a" "b" "c"] "e") ;; false
@ifesdjeen
ifesdjeen / keystrokes_dotplot.r
Created May 1, 2013 20:27
Render dot-plot of Keystrokes by minute
setwd("~/analytics/")
kp = read.csv("grouped_keys_combined")
colnames(kp) = c("Day", "Minute", "Strokes")
attach(kp)
parseTimestamp <- function(ts) as.POSIXct((ts + 120*60*1000) / 1000, origin="1970-01-01", tz="CEST")
kp$DayParsed <- parseTimestamp(kp$Day)
head(kp$DayParsed)
library("ggplot2")
@ifesdjeen
ifesdjeen / apply_to_values.clj
Created May 1, 2013 20:16
Apply to values (into hash)
(defn- apply-to-values [m f]
"Applies function f to all values in map m"
(into {} (for [[k v] m]
[k (f v)])))
@ifesdjeen
ifesdjeen / multimethods.clj
Created May 1, 2013 20:16
Clojure Multimethods
(defmulti even-odd
(fn [i]
(if (= 0 (mod i 2))
:even
:odd)))
(defmethod even-odd :even
[i]
(println "Even " i))
@ifesdjeen
ifesdjeen / with_tmp_file.clj
Created May 1, 2013 20:14
Do something within temporary directory
(defmacro with-temp-file
[f-sym & body]
`(let [prefix# (.toString (UUID/randomUUID))
postfix# (.toString (UUID/randomUUID))
~f-sym (java.io.File/createTempFile prefix# postfix#)]
(try
(do ~@body)
(finally
(.delete ~f-sym)))))
@ifesdjeen
ifesdjeen / pprint.clj
Created May 1, 2013 20:00
Pretty-print object methods
(require '[clojure.reflect :as r])
(use '[clojure.pprint :only [print-table]])
(print-table (:members (r/reflect"foo")))
@ifesdjeen
ifesdjeen / fn.clj
Last active December 16, 2015 21:09
Always think of Clojure functions, when you pass them around, as of values, not references. Whenever you change the root, previous function (one that you have already passed around), won't change. Only future calls will yield new value.
;; Declare a function
(defn fun1 [] (println 1))
;; And a hash that would capture that function
(def h {:key fun1})
;; => {:key #<user$fun1 user$fun1@5e540696>}
;; Now, check out the value of the key
(:key h)
;; => #<user$fun1 user$fun1@5e540696>
@ifesdjeen
ifesdjeen / AppDelegate.h
Last active December 16, 2015 06:29
How secure is your operating system? On Mac Os X all your keystrokes can be logged with a utility written by Objective C first-timer without any concerns.
#import <Cocoa/Cocoa.h>
static id monitorKeyDown;
@interface AppDelegate : NSObject <NSApplicationDelegate>
@property (assign) IBOutlet NSWindow *window;
@end