Skip to content

Instantly share code, notes, and snippets.

(defmacro my-let
[bindings & body]
(assert (-> bindings count even?) "Bindings count can only be even.")
`((fn [~@(take-nth 2 bindings)]
~@body)
~@(take-nth 2 (rest bindings))))
@ifesdjeen
ifesdjeen / with-latch.clj
Last active December 21, 2015 18:19
Testing with countdown latch
(ns my-test-ns
(:require [clojure.test :refer :all])
(:import [java.util.concurrent CountDownLatch TimeUnit])
(defmacro with-latch
[countdown-from & body]
`(let [latch# (CountDownLatch. ~countdown-from)
~'latch latch#]
~@body
(.await latch# 5 TimeUnit/SECONDS)
@ifesdjeen
ifesdjeen / new_gist_file.clj
Last active December 21, 2015 06:18
In Control Structures of Let-over-lambda, they describe an idea of taking bindings and writing a macro that substitutes symbols for values. That's pretty much same thing. quote from the book itself: The expansion uses Labels special form to bind a function around the provided body. The function is named according to the symbol used in the named …
;; Writing something remotely reminding nlet
(defmacro body-and-bindings
[bindings & body]
`(let [~(first bindings) 1
~(second bindings) 2]
~@body))
;; Expands to:
(let* [a 1 b 2] (+ a b))

tmux shortcuts & cheatsheet

start new:

tmux

start new with session name:

tmux new -s myname
@ifesdjeen
ifesdjeen / string_head.clj
Created June 24, 2013 12:52
Clojure string tail
(defn ^String head
"Returns the first n characters of s."
[n ^String s]
(if (> (count s) n)
s
(.substring s (- 0 n))))
@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 / ring_middleware_order.clj
Created May 1, 2013 20:23
Ring middleware execution order
(defn handler
[]
(println "HANDLER"))
(defn wrap-1
[handler]
(fn [request]
(println 1)
(handler request)