Skip to content

Instantly share code, notes, and snippets.

View ghoseb's full-sized avatar
🏋️‍♂️

Baishampayan Ghose ghoseb

🏋️‍♂️
View GitHub Profile
@ghoseb
ghoseb / flatten.clj
Created September 14, 2011 04:46
Flatten a nested collection using Tail Recursion.
(defn my-flatten
"Flatten a nested collection using Tail Recursion."
[coll]
(letfn [(step [[fst & more :as coll] res]
(if (seq coll)
(if (coll? fst)
(recur (concat fst more) res)
(recur more (cons fst res)))
(reverse res)))]
(step coll nil)))
@ghoseb
ghoseb / 97.clj
Created December 17, 2011 15:55
4clojure #97
(fn [n]
(nth
(iterate (fn [row]
(let [prow (partition 2 1 row)]
(concat [1] (map #(apply + %) prow) [1])))
[1])
(dec n)))
@ghoseb
ghoseb / col-str.clj
Created January 6, 2012 08:42
Convert numbers to Excel headers
(let [chr (zipmap (range) "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
encode (fn [n acc]
(if (< n 26)
(cons n acc)
(recur (dec (quot n 26)) (cons (rem n 26) acc))))]
(defn col-str
"Convert numbers to Excel headers."
[n]
(apply str (map chr (encode n '())))))
@ghoseb
ghoseb / fact.py
Created March 11, 2012 05:14
Some factorial implementations in Python
import operator
# naive recursive solution
def fact1(n):
if n <= 1:
return 1
return n * fact1(n - 1)
fact2 = lambda n: reduce(operator.mul, xrange(1, n + 1))
@ghoseb
ghoseb / scaffold.clj
Created March 16, 2012 12:54
Scaffold by Christophe Grand
(defn scaffold
"Print the ancestor method signatures of a given interface."
[iface]
(doseq [[iface methods] (->> iface
.getMethods
(map #(vector (.getName (.getDeclaringClass %))
(symbol (.getName %))
(count (.getParameterTypes %))))
(group-by first))]
(println (str " " iface))
; Copyright (c) Rich Hickey. All rights reserved.
; The use and distribution terms for this software are covered by the
; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
; which can be found in the file epl-v10.html at the root of this distribution.
; By using this software in any fashion, you are agreeing to be bound by
; the terms of this license.
; You must not remove this notice, or any other, from this software.
(set! *warn-on-reflection* true)
@ghoseb
ghoseb / query.js
Created July 10, 2012 10:49
Sample Query
{
"size": 10,
"from": 0,
"query": {
"filtered": {
"filter": {
"or": [{
"term": {
"publish_id": "1"
}
@ghoseb
ghoseb / gist:4134640
Created November 23, 2012 09:04 — forked from AlexBaranosky/gist:4134522
doseq-indexed
;; Example:
;; (doseq-indexed idx [name names]
;; (println (str idx ". " name)
(defmacro doseq-indexed [index-sym [item-sym coll] & body]
`(let [idx-atom# (atom 0)]
(doseq [~item-sym ~coll]
(let [~index-sym (deref idx-atom#)]
;; (require '[clojure.string :as str] '[clojure.java.shell :as shell] '[taoensso.timbre :as timbre])
(defn with-free-port!
"Attempts to kill any current port-binding process, then repeatedly executes
nullary `bind-port!-fn` (which must return logical true on successful
binding). Returns the function's result when successful, else throws an
exception. *nix only.
This idea courtesy of Feng Shen, Ref. http://goo.gl/kEolu."
[port bind-port!-fn & {:keys [max-attempts sleep-ms]
@ghoseb
ghoseb / README.md
Created January 18, 2013 16:00 — forked from candera/README.md

A little Clojure configuration reader

This is a handy bit of code I've written more than once. I thought I'd throw it in here so I can refer back to it later. Basically, it lets you read a config file to produce a Clojure map. The config files themselves can contain one or more forms, each of which can be either a map or a list. Maps are simply merged. Lists correspond to invocations of extension points, which in turn produces a map, which is merged along with everything else.

An Example

Consider the following files:

names.edn