Skip to content

Instantly share code, notes, and snippets.

@ifesdjeen
ifesdjeen / wrap_fn.clj
Created May 22, 2012 11:36
Wrapping functions in Clojure
(defn wrap-fn
"Wrap or replace some function with your own function"
[qualifier wrapper]
(alter-var-root
qualifier
(fn [original-fn]
(fn [& caller-arguments]
(wrapper caller-arguments original-fn)))))
;; Replace your function implementation:
@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)
@ifesdjeen
ifesdjeen / Haskell Simplification 1.hs
Last active February 11, 2018 13:05
I've got that piece of code that looks extremely overcomplicated to me. Even though every operation by itself is simple, the "sum" of operations is simply insane.
data Query = Query
data SomeObj = SomeObj
data IoOnlyObj = IoOnlyObj
data Err = Err
-- There's a decoder function that makes some object from String
decodeFn :: String -> Either Err SomeObj
decodeFn = undefined
-- There's a query, that runs against DB and returns array of strings
@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} clojars@clojars.org:"
(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))))