Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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))))

tmux shortcuts & cheatsheet

start new:

tmux

start new with session name:

tmux new -s myname
@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))
@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)
(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 / deploy_jar.rb
Created October 17, 2013 12:33
If you're deploying 20 Clojure libs a day (and keep forgetting Clojars deploy syntax), you're going to love that script. Maybe there's a leiningen task for that, never tried.
#!/usr/bin/ruby
res = `lein do pom, jar`
jar = res.split("\n").last.gsub("Created ", "")
puts "Gonna deploy jar: #{jar}"
exec "scp pom.xml #{jar} [email protected]:"
@ifesdjeen
ifesdjeen / Haskell vs Clojure Tail Rec.clj
Last active August 29, 2015 13:56
Comparison of tailrec+pattern matching between Haskell and Clojure
(defn max-from-list
"Get the maximum from list using recursion"
[[head & tail]]
(if (empty? tail)
head
(let [max-in-tail (max-from-list tail)]
(if (> head max-in-tail)
head
max-in-tail))))
(defn almost=
"Non-strict equality"
[wat center tolerance]
(and (>= wat (- center tolerance))
(<= wat (+ center tolerance))))